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

ADOBE CONFIDENTIAL
------------------
Copyright 2023 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'

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.oz_client
local WFCatUtils = require 'WFCatUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFOzSyncConstants = require 'WFOzSyncConstants'

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

local WFCatUtilsTest = {}

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

local function createFakeWFCatalog( rxTest )
	
	local optArgs = {}
	
	if WFPlatformInfo.isMobile() then
		optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
	end

	return WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, optArgs )

end

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

function WFCatUtilsTest.rxTestCatalogIsAssociatedWithThisAccount( rxTest )

	local wfCatalog = createFakeWFCatalog( rxTest )

	-- Should return true for the global catalog, but not actual cache anything:

	local backupOzCatalogId = wfCatalog.ozCatalogId

	wfCatalog.ozCatalogId = function() return WFOzSyncConstants.kGlobalDocumentId end
		-- Mock out the ozCatalogId function to make it look like the global catalog

	rxTest:expectSingleValue( WFCatUtils.catalogIsAssociatedWithThisAccount( wfCatalog ), true, ':ignore:', ':ignore:' )

	rxTest:expectSingleValue( wfCatalog:docStore():localDocValue( 'accountCheck' ), {
			_empty = true,
			_id = "accountCheck",
		} )

	wfCatalog.ozCatalogId = backupOzCatalogId
		-- Restore real function

	-- Should be true if there's no previous association

	rxTest:expectSingleValue( WFCatUtils.catalogIsAssociatedWithThisAccount( wfCatalog ), true, ':ignore:', ':ignore:' )

	-- Associate the catalog with different account

	local accountCheckDocument = {
		_id = 'accountCheck',
		accountId = KSIdGenerator.uuidInLowercaseHex()
	}

	rxTest:waitFor( wfCatalog:docStore():setLocalDocValue( accountCheckDocument ) )

	rxTest:expectSingleValue( WFCatUtils.catalogIsAssociatedWithThisAccount( wfCatalog ), false, ':ignore:', ':ignore:' )

	-- Should still return false, even if it's a global catalog:

	backupOzCatalogId = wfCatalog.ozCatalogId

	wfCatalog.ozCatalogId = function() return WFOzSyncConstants.kGlobalDocumentId end
		-- Mock out the ozCatalogId function to make it look like the global catalog

	rxTest:expectSingleValue( WFCatUtils.catalogIsAssociatedWithThisAccount( wfCatalog ), false, ':ignore:', ':ignore:' )

	wfCatalog.ozCatalogId = backupOzCatalogId
		-- Restore real function

	-- Associate the catalog with the correct account

	accountCheckDocument = {
		_id = 'accountCheck',
		accountId = wfCatalog:ozUserId()
	}

	rxTest:waitFor( wfCatalog:docStore():setLocalDocValue( accountCheckDocument ) )

	rxTest:expectSingleValue( WFCatUtils.catalogIsAssociatedWithThisAccount( wfCatalog ), true, ':ignore:', ':ignore:' )

end

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

return WFRxTestCase.wrapSuite( 'WFCatUtilsTest', WFCatUtilsTest )
