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

WFCatImportManifestProcessorUtils.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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 KSStringUtils = require "KSStringUtils"
local WFCatImportManifestConstants = require "WFCatImportManifestConstants"
local OZ_MAX_ALBUM_NAME_CHARS = 250

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

-- SPAM [ashj 2017-08-02]: This string is being referred from
-- WFRxDocStoreImp. If you are changing it there, please
-- change it here as well.

local ERROR_REVISION_WITHOUT_PARENT = "revision_without_parent"

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

local function readManifestVariable( manifestDatabase, variableName )
	
	return manifestDatabase:readData( 'getVariable', { name = variableName } )
		:map( function( results )
			if results[ 1 ] then
				return results[ 1 ].value
			end
		end )
	
end

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

local function maybeReadLrCatStoreProviderId( manifestDatabase )
	-- Read the lrCatStoreProviderId if we're migrating from Lightroom
	return readManifestVariable(
		manifestDatabase,
		WFCatImportManifestConstants.variableNames.lrCatStoreProviderId
	)
end

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

local function maybeReadManifestSourceId( manifestDatabase )
	
	return readManifestVariable(
		manifestDatabase,
		WFCatImportManifestConstants.variableNames.manifestSourceId
	)
end

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

local function findUniqueName( name, takenNames )

	local lowerNames = {}
	for n in pairs( takenNames ) do
		lowerNames[ KSStringUtils.lower( n ) ] = true
	end

	local i = 0
	--Updated the logic for Bug Fix- NMB 6606
	--Unique name length needs to be limited to 250 Characaters - Max length supported in LrCC
	--Otherwise, the truncation to 250 character after collision resolution leads to the same problem of matching names we initially dealt with
	local currName = KSStringUtils.UTF8Substring( name, 1, OZ_MAX_ALBUM_NAME_CHARS )
	while lowerNames[ KSStringUtils.lower( currName ) ] do
		
		i = i + 1
		local maxLengthCurrName = name .. '_' .. tostring( i )
		if (KSStringUtils.lengthOfUTF8String( maxLengthCurrName ) <= OZ_MAX_ALBUM_NAME_CHARS) then
			currName = maxLengthCurrName
		else
			local suffixlen = KSStringUtils.lengthOfUTF8String( tostring( i ) )
			maxLengthCurrName = KSStringUtils.UTF8Substring( maxLengthCurrName, 1, (OZ_MAX_ALBUM_NAME_CHARS - suffixlen -1) )
			currName = maxLengthCurrName .. '_' .. tostring( i )
		end
		
	end
	
	return currName
end

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

local function isDocAlreadyExistsError( err )

	err = tostring( err )
	return not not string.match( err, ERROR_REVISION_WITHOUT_PARENT )
end

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

return {
	readManifestVariable = readManifestVariable,
	
	maybeReadLrCatStoreProviderId = maybeReadLrCatStoreProviderId,
	maybeReadManifestSourceId = maybeReadManifestSourceId,
	
	findUniqueName = findUniqueName,
	isDocAlreadyExistsError = isDocAlreadyExistsError,
}
