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

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'

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

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' )
end

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

function WFCatTrashUtils.getXmpDevelopIsUploaded( docValue )
	return derefOpt( docValue, '_localOnly', 'universal', 'trash', 'uploaded', '/rels/xmp/develop' )
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.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
