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

ADOBE CONFIDENTIAL
------------------
Copyright 2021 Adobe Inc.
All Rights Reserved.

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

local Rx = require "Rx"

local KSSystemNotifications = require "KSSystemNotifications"
local KSPathUtils = require "KSPathUtils"

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

local LrDocStoreTypeMigratorProgressModel = {}

function LrDocStoreTypeMigratorProgressModel.makeModel( optDocStoreName )

	local docStoreName = optDocStoreName or 'Managed Catalog.mcat'

	local value = Rx.ReplaySubject( 1 )

	KSSystemNotifications.onNotificationDo( 'docStoreTypeMigratorProgress', function( ... )
		local progress = arg[ 1 ]
		if KSPathUtils.leafName( progress.dbPath ) == docStoreName then
			-- broadcast is targeted at this docStore
			if progress.progress >= progress.totalCount then
				value:onCompleted()
			else
				-- The LrD progressbar UI insists on a minimum totalCount of 250.
				-- We receive a number between 0 and 999

				value:onNext( progress )
			end
		end
	end )

	return {
		value = value
	}

end

return LrDocStoreTypeMigratorProgressModel