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

PixelProviderIdlePixelPathsModel.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
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 KSTableUtils = require "KSTableUtils"
local WFCloseSupport = require "WFCloseSupport"

	-- wf.oz_client.managed_catalog.pixel_provider
local PixelPathProviderRequestTypes = require 'PixelPathProviderRequestTypes'

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

local function makeWeakRef( v )

	local weakRef = KSTableUtils.makeWithMode( "kv" )

	if v ~= nil then

		weakRef[ v ] = v

	end

	return weakRef

end

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

local function derefWeakRef( weak )

	return next( weak )

end

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

local function make( wfCatalogPixelProviderProcessorStream, pixStreamId,
					longSide )

	local weakPixelProviderProcessor = makeWeakRef( nil )

	return {

		value = wfCatalogPixelProviderProcessorStream
					:map( 
						function( _, pixelProviderProcessor )

							weakPixelProviderProcessor = 
								makeWeakRef( pixelProviderProcessor )

							return string.format( "Captured pixelProvider for idlePixelPaths long side %s model for %s",
													longSide, pixStreamId )

						end ),

		events = {

			fetchPaths = function( _, localAssetIds )

				local pixelProviderProcessor = 
						derefWeakRef( weakPixelProviderProcessor )

				if not pixelProviderProcessor then

					return

				end

				pixelProviderProcessor
					:runUntilDone( 
						"abandonAllPixStreamRequests", 
							pixStreamId )

				pixelProviderProcessor
					:runUntilDone(
						"batchAddRequests",
							pixStreamId, localAssetIds,
							PixelPathProviderRequestTypes.names.idle,
							longSide )

			end,

		},

		close = function()

			local pixelProviderProcessor = 
					derefWeakRef( weakPixelProviderProcessor )

			weakPixelProviderProcessor = makeWeakRef( nil )

			if not pixelProviderProcessor or 
				WFCloseSupport.hasBeenOrWillBeClosed( pixelProviderProcessor ) then

				return

			end

			pixelProviderProcessor:runUntilDone( "abandonAllPixStreamRequests",
													pixStreamId )

		end,

	}

end

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

return {

	make = make,

}
