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

WFCapabilitiesCache.lua

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

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

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCapabilities = require 'WFCapabilities'

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

local WFCapabilitiesCache = {

	_cache = false
		-- (table) the cache

}

-- @mark WFCapabilitiesCache namespace

--[[!
	\class WFCapabilitiesCache
	\brief 	WFCapabilitiesCache provides a way of caching which capabilities are
	enabled and, more importantly, a method of enabling capabilities only for a
	specific instance of a class for a unit test rather than enabling a capability
	globally which creates a problem for running tests in parallel.

	For tests, allows a table of capabilities to be passed into
	setCapabilitiesForTests() and those values will now be the values. To disable
	a capability, pass in value false.

	Otherwise, for any capability queried via get() method, will make a one time
	fetch of the capability value from WFCapabilities and then caches the value
	if found, otherwise caches false and will not check the capability again.

	Has no support for capability filters.

	See WFCatBinaryUploader code and related tests for examples of how to use this
	class.

	See WFCapabilities.lua for more information on capabilities in general.
--]]

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

function WFCapabilitiesCache:__init( values )

	WFArguments.check( 'WFCapabilitiesCache', '?table' )

	self._cache = {}

	if values then
		self:setCapabilitiesForTests( values )
	end

end

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

function WFCapabilitiesCache:get( selector )

	WFArguments.check( 'WFCapabilitiesCache', '+string' )

	local value = self._cache[ selector ]

	-- false indicates the we already know the cability is not enabled

	if value or value == false then

		return value

	end

	value = WFCapabilities.getCapability( selector )

	if value == nil then

		value = false
			-- if nothings was returned mark as false in cache so as not to check again

	end

	self._cache[ selector ] = value

	return value

end

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

function WFCapabilitiesCache:setCapabilitiesForTests( values )

	WFArguments.check( 'WFCapabilitiesCache', 'table' )

	for key, value in pairs( values ) do

		self._cache[ key ] = value

	end

end

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

return Rx.Class 'WFCapabilitiesCache'( WFCapabilitiesCache )
