--[[----------------------------------------------------------------------------

WFCatFormatterUtils.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems IncorporatedAll Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'

--==============================================================================

local WFCatFormatterUtils = {}

--------------------------------------------------------------------------------

function WFCatFormatterUtils.extractNumeratorDenominator( val )
	
	if not val then return end

	local _, numerator, denominator

	if type( val ) == 'string' then
		_, _, numerator, denominator = string.find( val, "(-?[0-9]+)/([0-9]+)")
	end

	if type( val ) == 'table' then
		numerator, denominator = val[ 1 ], val[ 2 ]
	end

	if numerator then

		numerator = tonumber( numerator )
		denominator = tonumber( denominator )

		if numerator > 0 and denominator > 0 then

			-- Fix 2436050 : For fractions that are sufficiently close to 1/3 or 2/3

			local realValueX100 = math.floor( 30000 * numerator / denominator )

			if realValueX100 == 10000 then
				return 1, 3
			elseif realValueX100 == 20000 then
				return 2, 3
			end

		end

		return numerator, denominator

	end

	return nil

end

------------------------------------------------------------------------------

function WFCatFormatterUtils.convertToNumericValue( val )

	if type( val ) == "number" then

		return val

	end

	if type( val ) == "table" then

		local numerator = val[ 1 ]
		local denominator = val[ 2 ]

		if type( numerator ) == "number"  and type( denominator ) == "number" and denominator ~= 0 then

			return numerator / denominator

		end

	end

end

--------------------------------------------------------------------------------

function WFCatFormatterUtils.newSuite( formatters )
	
	local function add( f )
		if f.ids then
			for _, id in ipairs( f.ids ) do
				local copy = KSTableUtils.shallowcopy( f )
				copy.ids = nil
				copy.id = id
				add( copy )
			end
		else
			formatters[ #formatters + 1 ] = f
		end
	end
	
	
	local function unused( id )
		formatters[ #formatters + 1 ] = {
			id = id,
		}
	end
	
	return formatters, add, unused
	
end

--==============================================================================

return WFCatFormatterUtils
