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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 Adobe Systems Incorporated
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 WFRxScheduler = require 'WFRxScheduler'

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

local NmbForceReindexing = {}

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

function NmbForceReindexing.makeModel( wfCatalogStream )

	local reindexingProgress = wfCatalogStream
	
		:filter( function( wfCatalog )
			return wfCatalog ~= nil
		end )
		
		:take( 1 )
		
		:flatMap( function( wfCatalog )
		
			-- The following merges two progress reporter streams. One progress
			-- reporter stream tracks reindexing progress, and the other tracks
			-- doc store initialization progress. Reindexing will not start
			-- until the doc store is fully initialized, so the following
			-- reactive chain will produce progress events for the doc store,
			-- and then switch to progress events for the index store. For clients,
			-- we expect this to appear as two separate house-keeping progress bars.
			-- Unfortunately, combining this data into a single progress bar is 
			-- difficult, if not impossible, because we don't know what needs to be
			-- reindexed until after the doc store has been initialized.
			
			-- Note that we skip the last data event from the doc store initialization
			-- progress reporter. This is to avoid clients prematurely removing the
			-- house-keeping dialog when they see the first "100%" progress event.
			
			return wfCatalog:indexStore():getReindexingProgress()
				:merge( wfCatalog:docStore():getInitializationProgress():skipLast( 1 ) )
				:calm( 1, WFRxScheduler.sharedScheduler() )
				
		end )

	return {
		value = reindexingProgress
	}

end

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

return NmbForceReindexing
