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

DebuggingStreams.lua

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

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

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

	-- WF.oz_client.managed_catalog.managed_catalog
local WFCatFileSpec = require "WFCatFileSpec"

--==============================================================================
-- makeAssetFileInfoStream
--==============================================================================

local KSTableUtils_derefOpt = require 'KSTableUtils'.derefOpt

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


local function maybeGetSelfLink( docValue )

	local linksInfo = KSTableUtils_derefOpt( docValue,
												'_serverProvidedMetadata',
												'links' )

	if not linksInfo then

		return

	end

	local maybeSelfLinkInfo = linksInfo[ "self" ]

	local selfLinkInfo = 
			type( maybeSelfLinkInfo ) == "table" and maybeSelfLinkInfo

	if not selfLinkInfo then

		return

	end

	return selfLinkInfo.href 

end

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

local function extractAssetFileInfo( docValue )

	if type( docValue ) ~= "table" then

		return

	end

	local localAssetId = KSTableUtils_derefOpt( docValue, "_localId" )

	local originalPath = WFCatFileSpec.buildOriginalPathFromDocValue( docValue )

	local proxyPath = WFCatFileSpec.buildProxyPathFromDocValue( docValue )

	local ozAssetId = KSTableUtils_derefOpt( docValue, "_id" )

	local ozAssetURI = maybeGetSelfLink( docValue )

	local ozCopyId = KSTableUtils_derefOpt( docValue, "copy", "copy_id" )

	return { localAssetId, originalPath, proxyPath, ozAssetId, ozAssetURI, ozCopyId }

end

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

local function makeAssetFileInfoStream( wfCatalog, assetId )

	return wfCatalog
			:docStore()
				:ongoingDocValue( assetId )
				:map( extractAssetFileInfo )

end

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

return {

	makeAssetFileInfoStream = makeAssetFileInfoStream,

}
