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

WFCatUtils.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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 KSDate = require 'KSDate'
local KSTableUtils = require 'KSTableUtils'

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

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

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

local WFCatUtils = {}

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

function WFCatUtils.createBaseOverlayWithUserUpdatedField( id )

	check( '?string|number' )

	return {
		_id = id,
		userUpdated = KSDate.timeInIso8601Format()
	}

end

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

function WFCatUtils.createBaseOverlayWithUserCreatedFields( id )

	check( '?string|number' )
	
	local now = KSDate.timeInIso8601Format()
	
	return {
		_id = id,
		userCreated = now,
		userUpdated = now
	}

end

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

function WFCatUtils.addUserUpdatedFields( doc )
	
	check( 'table' )
	
	doc.userUpdated = KSDate.timeInIso8601Format()
	
	return doc
end

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

function WFCatUtils.addUserCreatedFields( doc )
	
	check( 'table' )
	
	local now = KSDate.timeInIso8601Format()
	
	if not doc.userCreated then
		doc.userCreated = now
	end
	
	doc.userUpdated = now
	
	return doc
end

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

function WFCatUtils.isUnsupportedFormat( doc )
	assert( require 'WFPlatformInfo'.hasACR, "ACR required for HEIC" )
	
	local fileName = derefOpt( doc, 'importSource', 'fileName' )
	fileName = fileName and string.lower( fileName )
	
	local isUnsupported = false
	local WFCrInfo = assert( require 'NmbAcr'.WFCrInfo )
	if not WFCrInfo.supportsHeic() and fileName and string.match( fileName, '.heic$' ) then
		isUnsupported = true
	end
	
	return isUnsupported
end

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

function WFCatUtils.isEdited( doc )

	check( 'table' )

	local createdDateTime = derefOpt( doc, 'userCreated' ) 
	local updatedDateTime = derefOpt( doc, 'develop', 'userUpdated' )

	local isEdited = updatedDateTime ~= nil and updatedDateTime ~= createdDateTime

	return isEdited
end

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

return WFCatUtils
