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

PixelPathProviderRequestTypes.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All 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
local WFFunctionUtils = require "WFFunctionUtils"
local KSTableUtils = require 'KSTableUtils'

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

-- these are in priority order

local PixelPathProviderRequestTypesArray = {

	"highest", 
		-- used by active loupe download

	"active_high_res_loupe_rendition", 
		-- used for high resolution thumbnails in active loupe view

	"cache_only", 
		-- for requests that don't render or download, just do a local
		-- filesystem check. In theory these have almost no performance
		-- overhead and should be processed early.
					
	"grid_visible", 
		-- for fetching renditions for visible grid items

	"download_soon", 
		-- for dowloading loupe external settings 
		
	"prefetch_high_res_loupe_rendition", 
		-- used prefetching for high resolution thumbnails 

	"grid_prefetch",
		-- for prefetching renditions for grid

	"loupe_next", 
		-- prefetching local negative paths for loupe apron

	"ensure_local_original_exists", 
		-- for downloading local originals for export, offline use

	"idle", 
		-- for offscreen grid renditions and idle downloads of local
		-- proxies/originals

	"lowest", 
		-- lowest priority.

}

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

local nameToPriorityMap

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

local function getPriority( name )

	nameToPriorityMap = nameToPriorityMap or 
			KSTableUtils.invert( PixelPathProviderRequestTypesArray )

	return nameToPriorityMap[ name ]

end

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

local function getName( priority )

	return PixelPathProviderRequestTypesArray[ priority ]

end

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

local function addToIdentityMap( map, name )

	map[ name ] = name

	return map

end

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

local _names = WFFunctionUtils.reduce( {}, addToIdentityMap, 
									PixelPathProviderRequestTypesArray )

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

local function comparePriorities( p1, p2 )

	if type( p1 ) ~= "number" or type( p2 ) ~= "number" then

		error( string.format( "bad priorities passed to comparePriorities: %s(%s) and %s(%s)",
									p1, type( p1 ), p2, type( p2 ) ) )

	end

	if p1 > p2 then

		return -1

	elseif p1 < p2 then

		return 1

	end

	return 0

end

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

return {

	getPriority = getPriority,

	getName = getName,

	names = _names,

	priorities = PixelPathProviderRequestTypesArray,

	comparePriorities = comparePriorities,

}
