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

LrClassicSyncStatusModel.lua

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

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

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

local Rx = require 'Rx'

	-- WF.oz_client.managed_catalog.sync
local WFCatStorage = require 'WFCatStorage'
local LrClassicSyncStatus = require 'LrClassicSyncStatus'

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

local LrClassicSyncStatusModel = {}

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

local function makeWithFirstValidCatalog( wfCatalogStream, defaultValue )

	return function( fn, ... )

		local params = { ... }
		local nParams = select( '#', ... )

		return wfCatalogStream

			:flatMapLatest( function( wfCatalog )
				if not wfCatalog then
					return Rx.Observable.just( defaultValue )
				end

				return fn( wfCatalog, unpack( params, 1, nParams ) )
			end )

	end

end

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

function LrClassicSyncStatusModel.makeAssetSyncStatusModel( wfCatalogStream, assetIds )

	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )

	return {

		value =  f( LrClassicSyncStatus.getSyncStatusOfAssets, assetIds ),

	}

end

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

function LrClassicSyncStatusModel.makeSyncStatusModel( wfCatalogStream )
	
	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )
		
	return {
		value = f( LrClassicSyncStatus.getSyncStatusForAssets, {} )
	}

end

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

function LrClassicSyncStatusModel.makeAlbumSyncStatusModel( wfCatalogStream, albumIds )

	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )

	return {

		value =  f( LrClassicSyncStatus.getAlbumSyncStatusByAlbumIds, albumIds ),

	}

end

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

function LrClassicSyncStatusModel.makeSyncStatusAllAlbumModel( wfCatalogStream )
	
	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )
		
	return {
		value = f( LrClassicSyncStatus.getSyncStatusForAlbums, {} )
	}

end

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

function LrClassicSyncStatusModel.makeMissingProxyListModel( wfCatalogStream )

	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )
		
	return {
		value = f( LrClassicSyncStatus.getMissingProxyList, {} )
	}

end

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

function LrClassicSyncStatusModel.makeMissingAuxListModel( wfCatalogStream )

	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )

	return {
		value = f( LrClassicSyncStatus.getMissingAuxList, {} )
	}

end

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

function LrClassicSyncStatusModel.makePendingUploadList( wfCatalogStream )

	local f = makeWithFirstValidCatalog( wfCatalogStream, {} )

	return {
		value = f( LrClassicSyncStatus.getPendingUploadList, {} )
	}

end

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

return LrClassicSyncStatusModel
