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

WFCatSearchAndSortConstants.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

----------------------------------------------------------------------------]]--
local WFConfiguration = require "WFConfiguration"
------------------------------------------------------------

local WFCatSearchAndSortConstants = {}

----------------------------------------------------------
WFCatSearchAndSortConstants.General = {}
WFCatSearchAndSortConstants.General.NO_CAMERA_INFO = "NoCamera"

------------------------------------------------------------
 WFCatSearchAndSortConstants.OnlineSearchStatus =
	{
		NoOnlineRequest = "NoOnlineRequest",
		InProgress = "InProgress",
		Completed = "Completed",
		CompletedUnexpectedly = "CompletedUnexpectedly"
	}
	
------------------------------------------------------------	
WFCatSearchAndSortConstants.ONLINESEARCH_TEMP_TABLE_NAME = "Temp_Online_Search"
------------------------------------------------------------
local PAGE_SIZE = 200
------------------------------------------------------------
local  ONLINE_SEARCH_URL_STG2  = "https://adobesearch-stage.adobe.io/universal-search-lr/v2/search" -- main stage
--local  ONLINE_SEARCH_URL_STG2  = "https://adobesearch-fac-stage.adobe.io/universal-search-lr/v2/search" -- temp stage for facet search work
local  ONLINE_SEARCH_URL  = "https://adobesearch.adobe.io/universal-search-lr/v2/search"

local ONLINE_SEARCH_GATEWAY_URL_STG = "https://api-gateway-stage-ue1.adobe.io/universal-search-lr/v2/search"
local ONLINE_SEARCH_GATEWAY_URL = "https://api-gateway-ue1.adobe.io/universal-search-lr/v2/search"

local SEARCH_GATEWAY_HOST = "adobesearch.adobe.io"
local SEARCH_GATEWAY_HOST_STG = "adobesearch-fac-stage.adobe.io"

--------------------------------------------------------------------------------
local _useStage = false
-- explicitly set to false, when trying on stage in the config file
if WFConfiguration.searchConfiguration.useProduction == false then
	_useStage = true
end

--------------------------------------------------------------------------------
-- set to true if we need to use UE gateway only
local _useGateway = false 
if WFConfiguration.searchConfiguration.useGateway ~= nil then
	_useGateway = WFConfiguration.searchConfiguration.useGateway
end

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

WFCatSearchAndSortConstants.PAGE_SIZE = ( WFConfiguration.searchConfiguration.pageSize and tonumber( WFConfiguration.searchConfiguration.pageSize ) )
			or PAGE_SIZE

------------------------------------------------------------
WFCatSearchAndSortConstants.getSearchUrl = function( isStage )
	if  WFConfiguration.searchConfiguration.searchURL then
		return WFConfiguration.searchConfiguration.searchURL
	end
	
	if isStage and not _useStage then
		_useStage = true
	end
	
	if _useStage then
		return ( _useGateway and ONLINE_SEARCH_GATEWAY_URL_STG ) or ONLINE_SEARCH_URL_STG2
	else
		return ( _useGateway and ONLINE_SEARCH_GATEWAY_URL ) or ONLINE_SEARCH_URL
		
	end
	
end

------------------------------------------------------------
WFCatSearchAndSortConstants.getSearchRequestArgs = function( )
	local requestHeaders = {
			{ header = 'Content-Type', value = 'application/vnd.adobe.search-request+json' },
			{ header = 'X-Product', value =  'Nimbus/1.0' },
			{ header = 'X-Product-Location', value = 'Asset-search' },
			{ header = 'Accept', value = 'application/json' },
		}
		
	if _useGateway then
		local host = ( _useStage and SEARCH_GATEWAY_HOST_STG ) or SEARCH_GATEWAY_HOST
		local gatewayHost = { header = 'Host', value = host }
		table.insert( requestHeaders, gatewayHost )
	end
	
	-- as per communication from USS team, stage requests should have this additional
	-- header parameter when using /universal-search-lr endpoint
	if _useStage then
		local gatewayHost = { header = 'x-uss-server', value = 'uss1' }
		table.insert( requestHeaders, gatewayHost )
		
	end
	-- 'Authorization' and 'X-Api-Key' are taken care by WFCatuser
	local postArgs = {
		requestHeaders = requestHeaders,
		requestMethod = 'POST',
		bypassRateLimiter = true,
	}	

	return postArgs
end

------------------------------------------------------------
-- By default use the single-region URL. Allow config option to change to global URL.
local _useGlobalURL = false
if WFConfiguration.searchConfiguration.useGlobalURL ~= nil then
	_useGlobalURL = WFConfiguration.searchConfiguration.useGlobalURL
end

local AUTOCOMPLETE_GLOBAL_URL_STG = "https://adobesearch-stage.adobe.io/autocomplete/authcompletions"
local AUTOCOMPLETE_SINGLE_REGION_URL_STG = "https://adobesearch-ac-stage.adobe.io/autocomplete/authcompletions"

local AUTOCOMPLETE_GLOBAL_URL = "https://adobesearch.adobe.io/autocomplete/authcompletions"
local AUTOCOMPLETE_SINGLE_REGION_URL = "https://adobesearch-ac.adobe.io/autocomplete/authcompletions"

WFCatSearchAndSortConstants.getAutoCompleteEndpoint = function()
	
	if  WFConfiguration.searchConfiguration.autoCompleteEndpoint then
		return WFConfiguration.searchConfiguration.autoCompleteEndpoint
	end
	
	if _useStage then
		return ( _useGlobalURL and AUTOCOMPLETE_GLOBAL_URL_STG ) or AUTOCOMPLETE_SINGLE_REGION_URL_STG
	else
		return ( _useGlobalURL and AUTOCOMPLETE_GLOBAL_URL ) or AUTOCOMPLETE_SINGLE_REGION_URL
		
	end
	
end

WFCatSearchAndSortConstants.getAutoCompleteRequestArgs = function( )
	local requestHeaders = {
			{ header = 'cache-control', value = 'no-cache' },
		}
	
	
	-- temp API key until the actual ones are allow-listed
	-- [ anmurark: 2018/08/03 ] removing them now since the product ids are now allow-listed
	
--	local apiKey
--	if not _useStage then
--		apiKey = { header = 'X-Api-Key', value = 'AdobeSearchService1' }
--	else
--		apiKey = { header = 'X-Api-Key', value = 'lraceval' }
--	end
--	table.insert( requestHeaders, apiKey )
--	
	
	-- 'Authorization' is taken care by WFCatuser
	local postArgs = {
		requestHeaders = requestHeaders,
		requestMethod = 'GET',
	}	
	
	return postArgs
	
end

------------------------------------------------------------
-- create the auto-complete GET request URL based on the wiki:
-- https://wiki.corp.adobe.com/display/adobesearch/Autocomplete+for+Lightroom+RESTful+Web+Service+API+v1.0

WFCatSearchAndSortConstants.getAutoCompleteURL = function( searchParams )
	
	local localeCode = 'en_US'
	if searchParams['locale'] then
		if type( searchParams['locale'] ) == 'string' then
			localeCode = searchParams['locale']
		elseif type( searchParams['locale'] ) and #searchParams['locale'] > 0 then
			localeCode = searchParams['locale'][1]
		end
	
	end
	
	local albumIds
	if searchParams[ 'album_ids' ] and #searchParams[ 'album_ids' ] > 0 then
		for _, albumId in ipairs( searchParams[ 'album_ids' ] ) do
			if not albumIds then
				albumIds = tostring( albumId )
			else
				albumIds = albumIds .. ',' .. tostring( albumId )
			end
		end
	end
	
	local queryParams = {}
	queryParams['q[text]'] = searchParams[ 'text' ]
	queryParams['q[locale]'] = localeCode
	queryParams['limit'] = tostring( searchParams[ 'limit' ] )
	queryParams['scope'] = "lightroom"
	queryParams['album_ids'] = albumIds
	
	if _useStage then
		local additionalParams = WFConfiguration.searchConfiguration.optionalParams or {}
		for key, value in pairs( additionalParams ) do
			queryParams[ key ] = tostring(value)
		end
	end
	
	local url = WFCatSearchAndSortConstants.getAutoCompleteEndpoint()
	local queryParamString = require "WFURI".buildQueryString( queryParams )
	return url .. "?" .. queryParamString
	
end
--------------------------------------------------------------------------------
return WFCatSearchAndSortConstants