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

ADOBE CONFIDENTIAL
------------------
Copyright 2014 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 KSReactive = require( 'KSReactive' )
local WFUnknown = require( 'WFUnknown' )

local LrmTestUtils = require( 'LrmTestUtils' )

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

local RxBasicTestMapping = {}

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

function RxBasicTestMapping.makeMapModel( n )

	local varB = KSReactive.variableB( n )
	
	local resultB = varB:closingMapB( function( val )
	
		return val + 1
		
	end )
						
	return {
		value = resultB
	}
	
end

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

function RxBasicTestMapping.makeParentModel( n )

	return {
	
		_n = n,
		
		value = KSReactive.variableB( n ),
	
		models = {
		
			childModel = function( parent, m )
			
				return {
					value = KSReactive.variableB( m + parent._n )
				}
				
			end
		}
	}
		
end

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

function RxBasicTestMapping.makeFilterModel()

	local streamE = KSReactive.sourceE()
	
	local resultB = streamE
		:closingFilterE( function( value )
			return value % 2 == 0
		end )
		:closingHoldB( WFUnknown.value() )
	
	return {
	
		value = resultB,
		
		events = {
		
			postToStream = function( self, value )
			
				streamE:post( value )
				
			end,
		}
	}
	
end

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

function RxBasicTestMapping.makeIndexStoreModel( id )

	local docStore, indexStore = LrmTestUtils.setupTestDatabase()
	
	LrmTestUtils.insertBaseMockData( docStore )
	
	-- query data from database
	
	local resultB = indexStore
		:queryB( [[SELECT name, url FROM name WHERE docId == ?]], id )
		:closingMapB( function( row )
			
			if row and row[ 1 ] then
			
				return {
					id = id,
					name = row[ 1 ][ 1 ],
					url = row[ 1 ][ 2 ],
				}
				
			end
			
			return WFUnknown.value()
			
		end )
	
	return {
		_docStore = docStore,
		_indexStore = indexStore,
		
		value = resultB,
		
		close = function( self )
		
			self._docStore:close()
			self._indexStore:close()
			
		end
	}
	
end

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

return RxBasicTestMapping
