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

WFCatSearchByExtension.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 KSTableUtils = require 'KSTableUtils'

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

local derefOpt = KSTableUtils.derefOpt

local isValidSearchableAssetDocument = assert( require 'WFCatIndexUtils'.isValidSearchableAssetDocument )

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

local WFCatSearchByExtension = {}

--[[
	Index definitions for finding assets by extension. The SQLite index only;
--]]

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

function WFCatSearchByExtension.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE extensions( extension TEXT, isCatalogDocument )]],

		useLocalId = true,
		
		hasOneRowPerDocument = true,
		
		relevantTypeSubtypeTuples = {
			{ 'asset', 'image' },
			{ 'asset', 'video' }
		},
		
		map = function( docValue, emit )

			local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )

			if not isSearchable or not searchableDoc then

				return

			end
			
			local isCatalogDocument = derefOpt( searchableDoc, "_spaceId" ) and 0 or 1
			
			local fileName = derefOpt( searchableDoc, 'importSource', 'fileName' )
			
			local extension = fileName:match( "%.([^%.]+)$" )
			
			if extension then
				extension = extension:lower()
				emit( extension, isCatalogDocument )
			end
			
		end,

	}

	indexStoreSchema:addIndex
		[[CREATE INDEX extensions_extension ON extensions( extension COLLATE i18n_collate, docId )]]
	indexStoreSchema:addIndex 
		[[CREATE INDEX extensions_isCatalogDocument ON extensions( isCatalogDocument )]]
	
end

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

function WFCatSearchByExtension.addSearchToSearchSchema( searchSchema )

	searchSchema:addSearchableData {
		term = 'extensions',
		dataType = 'extensionsTerm',
		sql = [[SELECT docId FROM extensions WHERE extension = ? ORDER BY docId]],
		query_params = {
			'extension',
		},
	}

end

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

function WFCatSearchByExtension.collectExtensionDetailsOfRequiredAssets( wfCatalog, assetLocalIds )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT extension
						FROM extensions
						INNER JOIN CARRAY( ?, ?, 'int32' ) AS assetLocalIds ON assetLocalIds.value == extensions.docId
						ORDER BY extension COLLATE i18n_collate ASC]],
		calm = 5,
		assetLocalIds,
		#assetLocalIds,
	}
	
end

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

function WFCatSearchByExtension.collectExtensionDetailsOfAllAssets( wfCatalog )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT extension
						FROM extensions
						WHERE isCatalogDocument = 1
						ORDER BY extension COLLATE i18n_collate ASC]],
		calm = 5,
	}
	
end

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

return WFCatSearchByExtension
