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

WFCatTrashUtils.lua

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

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

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'

	-- WF.oz_client.shared.utils
local LrAssetUtils = nil -- required later to avoid circular dependencies

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

local WFCatTrashUtils = {}

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

local derefOpt = KSTableUtils.derefOpt

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

function WFCatTrashUtils.createFromInfo( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom' )
end

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

function WFCatTrashUtils.getCreateFromId( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'id' )
end

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

function WFCatTrashUtils.getCreateWithRevisionId( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createWithRevId' )
end

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

function WFCatTrashUtils.originalExistsOnOz( docValue )
	-- Return true if the asset this docValue was copied from existed on Oz.
	-- This works for both regular and trash documents.
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom' ) ~= nil
end

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

function WFCatTrashUtils.getMasterIsUploaded( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/master' )
end

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

function WFCatTrashUtils.getProxyIsUploaded( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/proxy_type/2560' )
		or derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/proxy_type/panorama' )
		or derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/proxy_type/720p' )
		or derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/proxy_type/1080p' )
end

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

--[[

	\brief check for assets copied as part of a delete or restore operation to
		see whether upload of xmp_develop is required or if it has already been
		uploaded to Oz and Oz will copy it as part of delete/restore operation.
		Even if the binary is not present in the links section it may be
		appropriate to wait for Oz to do the asynchronous copy at which point
		the link will sync down.

		Also compare the sha256 from the develop settings to the sha256 for the
		xmp develop stored in _localOnly.universal.trash and if they do not match
		return false.

	\param docValue (table) a complete document

	\return boolean
]]
function WFCatTrashUtils.getXmpDevelopIsUploaded( docValue )

	local sha256 = derefOpt( docValue, 'develop', 'xmpCameraRaw', 'sha256' )
	local trashInfo = derefOpt( docValue, '_localOnly', 'universal', 'trash' )

	return derefOpt( trashInfo, 'uploaded', '/rels/xmp/develop' ) and
		derefOpt( trashInfo, 'createFrom', 'links', '/rels/xmp/develop', 'sha256' ) == sha256

end

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

function WFCatTrashUtils.getAuxIsUploaded( docValue, auxId )
	local relsKey = string.format( '/rels/aux/%s', auxId )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', relsKey )
end

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

function WFCatTrashUtils.getOriginalAssetMasterLink( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', '/rels/master' )
end

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

function WFCatTrashUtils.getOriginalAssetXmpDevelopLink( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', '/rels/xmp/develop' )
end

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

function WFCatTrashUtils.getOriginalAssetAuxLink( docValue, auxId )
	local relsKey = string.format( '/rels/aux/%s', auxId )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', relsKey )
end
--------------------------------------------------------------------------------

function WFCatTrashUtils.getOriginalAssetXmpLink( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', '/rels/xmp' )
end

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

function WFCatTrashUtils.getOriginalAssetLinkForKey( docValue, linksKey )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', linksKey )
end

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

function WFCatTrashUtils.getOriginalAssetUrlForLinkKey( docValue, linksKey )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links', linksKey, 'href' )
end

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

function WFCatTrashUtils.getOriginalAssetLinks( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'createFrom', 'links' )
end

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

function WFCatTrashUtils.isDeleteTrash( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'isDeleteTrash' )
end

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

function WFCatTrashUtils.isDirectlyDelete( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'isDirectlyDelete' )
end

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

function WFCatTrashUtils.getTrashOrRestoredDocFromNewId( docStore, newId )

	-- The user may have locally deleted and restored the new source repeatedly.
	-- Find the latest version of the asset that isn't deleted.

	local getDocValue = Rx.BehaviorSubject( newId )

	return getDocValue:flatMap( function( docId )
		return docStore:docValue( docId )
	end )
	:filter( function( newDocValue )

		local newId = newDocValue._deleted and newDocValue._newId
		if newId then
			getDocValue:onNext( newId )
		end

		return not newId

	end )
	:take( 1 )

end

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

return WFCatTrashUtils
