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

WFCatMetadataSchema.lua

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

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

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

	-- WF.core.lua
local KSData = require 'KSData'

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'

	-- WF.web.json
local KSJsonDecoder = require 'KSJsonDecoder'

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

local kOzMetadataSchema

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

local function getOzMetadataSchema()
	if not kOzMetadataSchema then
		local schemaFile = KSPathUtils.getStandardFilePath "Resources"
		schemaFile = KSPathUtils.child( schemaFile, "xmp_schema.json" )

		local success, rawJsonKSData = pcall( KSData.memoryMappedContentsOfFile, schemaFile )
		assert( success, 'Cannot locate import schema' )
		local rawJson = tostring( rawJsonKSData )
		rawJsonKSData:close()

		kOzMetadataSchema = KSJsonDecoder.decodeWebSafeJson( rawJson )
		
		-- build reverse lookup tables for enums and special
		for _, section in pairs( kOzMetadataSchema ) do
			for field, fieldInfo in pairs( section ) do
				if type( fieldInfo ) == 'table' then
					local special = fieldInfo._special
					if special then
						local specialXmpToJson = {}
						for k,v in pairs( special ) do
							specialXmpToJson[ v ] = k
						end
						fieldInfo._specialXmpToJson = specialXmpToJson
					end
					local values = fieldInfo._values
					if values then
						local valuesXmpToJson = {}
						for k,v in pairs( values ) do
							valuesXmpToJson[ v ] = k
						end
						fieldInfo._valuesXmpToJson = valuesXmpToJson
					end
				end
			end
		end
	end
	return kOzMetadataSchema
end

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

return {
	getOzMetadataSchema = getOzMetadataSchema,
}



