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

LrmKeywordsIndexDefinitions.lua

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

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

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

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local KSTableUtils = require 'KSTableUtils'

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

local check = assert( WFArguments.check )
local derefOpt = assert( KSTableUtils.derefOpt )

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

-- @mark LrmKeywordsIndexDefinitions namespace

local LrmKeywordsIndexDefinitions = {}

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

function LrmKeywordsIndexDefinitions.generateIndexSchema( indexStoreSchema )

	check( 'WFRxIndexStoreSchema' )

	-- table keywords

	indexStoreSchema:addTable  {

		oneRowPerDocument = false,

		sql = [[CREATE TABLE keywords( keyword TEXT )]],
		
		updateLazily = false,
		
		relevantTypeSubtypeTuples = {
			{ 'asset', 'image' },
			{ 'asset', 'video' }
		},
		
		map = function( docValue, emit )

			if docValue and docValue.type == 'asset'
					and ( docValue.subtype == 'image' or docValue.subtype == 'video' )
					and not docValue._deleted then

				local manualKeywords = derefOpt( docValue, 'xmp', 'dc', 'subject' )

				if  manualKeywords then

					for k, v in pairs( manualKeywords ) do
						emit( k )
					end

				end

			end

		end,

	}

	indexStoreSchema:addIndex [[CREATE INDEX keywords_keyword ON keywords( keyword COLLATE i18n_collate)]]
	
end

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

return LrmKeywordsIndexDefinitions
