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

WFCatImportManifestIdCache.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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.

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

	-- Rx
local Rx = require 'Rx'


local Observable = Rx.Observable
local Subject = Rx.Subject

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

local WFCatImportManifestIdCache = {
	_localGlobalCache = false,
		-- Actually holds the cache mapping local to global ids
		
	_idCache = false,
		-- Holds a cache of ids of assets, albums etc that exist in the managed catalog
		
	_initializationSubject = false,
		-- Notifies a listener when initialization is complete
	
	_initialized = false,
		-- is True after initialization
}

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

function WFCatImportManifestIdCache:__init( path )

	self._localGlobalCache = {}
	self._idCache = {}
	
	self._initializationSubject = Subject()
	
	local function completeInitialization()
		self._initialized = true
		self._initializationSubject:onNext( true )
	end
	
	-- Read existing ids if necesary. When finished, call
	completeInitialization()
	
end

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

function WFCatImportManifestIdCache:waitForInitialization()
	
	if self._initialized then
		return Observable.just( true )
	end
	
	return self._initializationSubject
end

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

function WFCatImportManifestIdCache:setOzIdForLocalId( set, localId, globalId )
	
	self._localGlobalCache[ set ] = self._localGlobalCache[ set ] or {}
	
	local cacheSet = self._localGlobalCache[ set ]
	
	cacheSet[ localId ] = globalId
	
end

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

function WFCatImportManifestIdCache:getOzIdForLocalId( set, localId )
	
	local cacheSet = self._localGlobalCache[ set ]
	
	if cacheSet then
		return cacheSet[ localId ]
	end
	
	return nil
end

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

function WFCatImportManifestIdCache:setIdExists( set, id )
	
	self._idCache[ set ] = self._idCache[ set ] or {}
	
	local cacheSet = self._idCache[ set ]
	
	cacheSet[ id ] = true
end

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

function WFCatImportManifestIdCache:doesIdExist( set, id )
	
	local cacheSet = self._idCache[ set ]
	
	if cacheSet then
		return cacheSet[ id ] or false
	end
	
	return false
	
end

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

function WFCatImportManifestIdCache:getAllForSet( set )
	
	local cacheSet = self._localGlobalCache[ set ]
	
	return cacheSet
end

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

return Rx.Class 'WFCatImportManifestIdCache'( WFCatImportManifestIdCache )
