module DurationHelper
  MINUTES_IN_HOUR = 60
  SECONDS_IN_MINUTE = 60

  def convert_duration(value, from:, to:)
    value_in_seconds = case from.to_s
                       when "seconds" then value 
                       when "minutes" then value * SECONDS_IN_MINUTE 
                       when "hours"   then value * SECONDS_IN_MINUTE * MINUTES_IN_HOUR
                       end

    result = case to.to_s
             when "seconds" then value_in_seconds
             when "minutes" then value_in_seconds / SECONDS_IN_MINUTE
             when "hours"   then value_in_seconds / SECONDS_IN_MINUTE / MINUTES_IN_HOUR
             end
  end
end
