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

NmbDocValueToRenderInfoTest.lua

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

ADOBE CONFIDENTIAL
___________________

Copyright 2020 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.
------------------------------------------------------------------------------]]

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

local WFTableMerge = require "WFTableMerge"
local WFFileUtils = require "WFFileUtils"
local WFPlatformInfo = require "WFPlatformInfo"
local KSPathUtils = require "KSPathUtils"
local KSTableUtils = require "KSTableUtils"

    -- WF.oz_client.managed_catalog
local NmbDocValueToRenderInfo = require 'NmbDocValueToRenderInfo'

local derefOpt = assert( KSTableUtils.derefOpt )

local testPath = KSPathUtils.getStandardFilePath( 'testdata' )
local volumeGuid = WFFileUtils.getVolumeGuidForPath( testPath )

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

local function mergeAll( ... )

	local ret = {}

	for i = 1, select( "#", ... ) do

		ret = WFTableMerge.applyOverlay( ret, select( i, ... ) )

	end

	return ret

end

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

local _docValueWithoutSpaceID = {}

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

local _docValueWithSpaceID = { _spaceId = "yass" }

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

local _docValueWithDownloadableMaster =
	{ 
		_serverProvidedMetadata = {
			links = {
				[ "/rels/master" ] =  {
					href = "yass"
				}
			}
		}
	}

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

local _docValueWithLocalMaster =
	{
		_localOnly = {
			files = {
				original = {
					master = {
						relativePath = "yaas",
						volumeGuid = volumeGuid,
					}
				}
			}
		}
	}

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

local _docValueWithDownloadableProxy =
	{ 
		_serverProvidedMetadata = {
			links = {
				[ "/rels/proxy_type/2560" ] =  {
					href = "yass"
				}
			}
		}
	}

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

local _docValueWithLocalProxy =
	{
		_localOnly = {
			files = {
				proxy = {
					[ "2560" ] = {
						relativePath = "yaas",
						volumeGuid = volumeGuid,
					}
				}
			}
		}
	}

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

local _haveRenderableSourceFile = 
		{ _docValueWithDownloadableMaster, _docValueWithLocalMaster, 
			_docValueWithDownloadableProxy, _docValueWithLocalProxy }

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

local _docValueWithSharedEditable =
	{ _serverProvidedMetadata = { editable = "yass" } }

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

local function rxTestDisallowLocalEditsForDocValue( rxTest )

	rxTest:log( "testing non shared without renderable path" )

	rxTest
		:expectEqual(
			NmbDocValueToRenderInfo.disallowLocalEditsForDocValue(
				_docValueWithoutSpaceID ),
			true )

	rxTest:log( "testing shared without renderable path" )

	rxTest
		:expectEqual(
			NmbDocValueToRenderInfo.disallowLocalEditsForDocValue(
				_docValueWithSpaceID ),
			true )


	for haveRenderableSourceFileNum, docValueWithRenderableSourceFile in pairs( _haveRenderableSourceFile ) do

		rxTest:logf( "testing non shared renderable path %s", haveRenderableSourceFileNum )

		rxTest
			:expectEqual(
				NmbDocValueToRenderInfo.disallowLocalEditsForDocValue(
					mergeAll( _docValueWithoutSpaceID, docValueWithRenderableSourceFile ) ),
				false )

		rxTest:logf( "testing shared with renderable path %s", haveRenderableSourceFileNum )

		
		local shouldBlockEdit = derefOpt( docValueWithRenderableSourceFile, '_serverProvidedMetadata' ) ~= nil
			-- For assets that have been touched by Oz, rely on the `editable` field. Otherwise, enable editing
		
		rxTest
			:expectEqual(
				NmbDocValueToRenderInfo.disallowLocalEditsForDocValue(
					mergeAll( _docValueWithSpaceID, docValueWithRenderableSourceFile ) ),
				shouldBlockEdit )

		rxTest:logf( "testing editable shared with renderable path %s", haveRenderableSourceFileNum )

		rxTest
			:expectEqual(
				NmbDocValueToRenderInfo.disallowLocalEditsForDocValue(
					mergeAll( _docValueWithSpaceID, _docValueWithSharedEditable, docValueWithRenderableSourceFile ) ),
				false )

	end

end

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

return WFRxTestCase.wrapSuite( {

	rxTestDisallowLocalEditsForDocValue = WFPlatformInfo.isDesktop and rxTestDisallowLocalEditsForDocValue or nil,

} )
