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

NmbLocalRenditionDigest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2021 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

local WFConfiguration = require 'WFConfiguration'

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

local _neverDownload = not not WFConfiguration.PixelProvider.NeverDownload

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

-- must defer this import to avoid recursive require loops
local PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo

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

local function haveSuitableDownloadableInvalidRendition( 
				renderInfo, localAssetId, renderSize )

	if _neverDownload then

		return false

	end

	if not PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo then

		PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo =
			assert( require "PixelProviderSharedBlobCache".getBestMatchInvalidRenditionForRenderInfo,
					"missing PixelProviderSharedBlobCache.getBestMatchInvalidRenditionForRenderInfo" )

	end

	local maybeDownloadableInvalidRenditionSizeName = 
		PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo(
				renderInfo, localAssetId, renderSize )

	return maybeDownloadableInvalidRenditionSizeName ~= nil

end

--------------------------------------------------------------------------------
-- must defer import due to circular dependencies
local PixelProviderSharedTaskUtils_getValidOriginalDownloadInfoFromRenderInfo 

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

local function renderInfoHasDownloadableOriginal( renderInfo )

	PixelProviderSharedTaskUtils_getValidOriginalDownloadInfoFromRenderInfo =
		PixelProviderSharedTaskUtils_getValidOriginalDownloadInfoFromRenderInfo or
		assert( require "PixelProviderSharedTaskUtils".getValidOriginalDownloadInfoFromRenderInfo,
			"PixelProviderSharedTaskUtils.getValidOriginalDownloadInfoFromRenderInfo is missing!" )

	local maybeHref = 
		PixelProviderSharedTaskUtils_getValidOriginalDownloadInfoFromRenderInfo( 
			renderInfo )

	return maybeHref ~= nil

end

--------------------------------------------------------------------------------
-- must defer import due to circular dependencies
local PixelProviderSharedTaskUtils_renderInfoHasDownloadableProxy

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

local function canOnlyDownloadInvalidRenditions( renderInfo )

	local downloadableInvalidRenditions = renderInfo.invalid_renditions or {}

	if #downloadableInvalidRenditions == 0 then

		-- there are no invalid renditions, so we can't download
		return false

	end

	-- we have invalid downloadable renditions from this point on. Try to see
	-- if there something else we could use

	local downloadableRenditions = renderInfo.renditions or {}

	if #downloadableRenditions > 0 then

		-- we always prefer valid renditions even if low res, so we won't
		-- download invalid ones
		return false

	end

	PixelProviderSharedTaskUtils_renderInfoHasDownloadableProxy =
		PixelProviderSharedTaskUtils_renderInfoHasDownloadableProxy or
		assert( require "PixelProviderSharedTaskUtils".renderInfoHasDownloadableProxy,
				"missing PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy" )

	if PixelProviderSharedTaskUtils_renderInfoHasDownloadableProxy( renderInfo ) then

		-- we can download a proxy and use that to render, so we won't download
		-- invalid renditions
		return false

	end

	if renderInfoHasDownloadableOriginal( renderInfo ) then

		-- we can download the original and use that to render, so we won't
		-- download invalid renditions
		return false

	end

	-- we have invalid renditions and they are currently the only option for
	-- pixels
	return true

end

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

return {

	renditionDownloadsProhibited = function() return _neverDownload end,

	haveSuitableDownloadableInvalidRendition = 
		haveSuitableDownloadableInvalidRendition,

	renderInfoHasDownloadableOriginal = renderInfoHasDownloadableOriginal,

	canOnlyDownloadInvalidRenditions = canOnlyDownloadInvalidRenditions,

}
