module Pagy::GearboxExtra

def setup_pages_var

Setup @pages and @last based on the :gearbox_items variable (not used by Pagy::Countless)
def setup_pages_var
  return super if !@vars[:gearbox_extra] || @vars[:items_extra]
  gearbox_items = @vars[:gearbox_items]
  # This algorithm is thousands of times faster than the one in the geared_pagination gem
  @pages = @last = (if @count > (sum = gearbox_items.sum)
                      [((@count - sum).to_f / gearbox_items.last).ceil, 1].max + gearbox_items.count
                    else
                      pages     = 0
                      remainder = @count
                      while remainder.positive?
                        pages     += 1
                        remainder -= gearbox_items[pages - 1]
                      end
                      [pages, 1].max
                    end)
end