require 'unit_conversion/constants'
require 'unit_conversion/errors'
require 'unit_conversion/formatters'
require 'unit_conversion/parsed_number'
require 'unit_conversion/parsed_unit'
require 'unit_conversion/conversions'
require 'unit_conversion/value_unit'

module UnitConversion
  class << self

    def parse(value_string, unit_string = nil)
      ValueUnit.for(value_string, unit_string)
    end

    def convert(quantity, factor, input_unit, output_unit, density = nil)
      unit_value = parse(quantity, input_unit).scale(factor)

      if output_unit.present?
        unit_value = unit_value.convert(output_unit, density)
      end

      unit_value.pretty_value
    end

    # Given an awkward measurement such as '18 9/10 oz' or '5/24 cup' or '0.09434 cup',
    # it will round the rational and scale the unit to give a more reasonable, useful measurement, ie
    # 19 oz, 3 Tbsp, 1 1/2 Tbsp
    def auto_unit(quantity, units)
      unit = parse(quantity, units).auto_unit

      [unit.pretty_value, unit.unit.to_s]
    end

  end
end