/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { generateSchemaKey } from "./generateSchemaKey.js";
import { isVariantConfigWithComponentStates, isVariantConfigWithProperties } from "./variantConfigGuards.js";
import { getArbitraryFixture, validateArbitraryValue } from "../../../fixtures/src/util.js";

//#region ../automated-config/dist/utils/buildConfigSchema.js
/*! © 2026 Yahoo, Inc. UDS Default Config v0.0.0-development */
/** Resolves the fixture type for a property's variant by finding which value set contains the default value */
function findFixtureType(property, variantKey) {
	const defaultValue = property.defaults[variantKey];
	const index = property.values.findIndex((arr) => arr.includes(defaultValue));
	const fixtureType = property.typeOfFixture[index === -1 ? 0 : index];
	const arbitraryFixture = getArbitraryFixture(fixtureType);
	if (arbitraryFixture && !validateArbitraryValue(arbitraryFixture, defaultValue)) throw new Error(`Invalid default value "${defaultValue}" for property "${property.name}" (variant "${variantKey}"): does not satisfy ${fixtureType} constraints`);
	return fixtureType;
}
/** Builds component schema from ComponentConfig at runtime */
function buildConfigSchema(config) {
	const result = {};
	const defaultsObj = {};
	for (const variantKey in config.variants) defaultsObj[variantKey] = config.variants[variantKey].default;
	result.defaults = defaultsObj;
	result.variables = {};
	for (const variantKey in config.variants) {
		const variantConfig = config.variants[variantKey];
		for (const variantOption of variantConfig.options) if (isVariantConfigWithComponentStates(variantConfig)) {
			const componentStates = variantConfig.componentStates;
			for (const componentStateKey in componentStates) {
				const componentState = componentStates[componentStateKey];
				for (const componentStateOption of componentState.options) for (const layerKey in componentState.layers) {
					const layer = componentState.layers[layerKey];
					const schemaKey = generateSchemaKey({
						variantKey,
						variantValue: variantOption,
						componentStateKey,
						componentStateValue: componentStateOption,
						layer: layerKey
					});
					result.variables[schemaKey] = {};
					for (const propertyKey in layer.properties) {
						const property = layer.properties[propertyKey];
						const variableObject = { [propertyKey]: {} };
						if (property?.skipRestState !== true) variableObject[propertyKey] = { rest: {
							type: findFixtureType(property, variantOption),
							valueType: "alias",
							value: property.defaults[variantOption]
						} };
						if (property.pseudoStates) for (const pseudoState of property.pseudoStates) variableObject[propertyKey][pseudoState] = {
							type: findFixtureType(property, variantOption),
							valueType: "alias",
							value: property.defaults[variantOption]
						};
						result.variables[schemaKey] = {
							...result.variables[schemaKey],
							...variableObject
						};
					}
				}
			}
		} else if (isVariantConfigWithProperties(variantConfig)) {
			const layers = variantConfig.layers;
			for (const layerKey in layers) {
				const layer = layers[layerKey];
				const schemaKey = generateSchemaKey({
					variantKey,
					variantValue: variantOption,
					layer: layerKey
				});
				result.variables[schemaKey] = {};
				for (const propertyKey in layer.properties) {
					const property = layer.properties[propertyKey];
					const variableObject = { [propertyKey]: {} };
					if (property?.skipRestState !== true) variableObject[propertyKey] = { rest: {
						type: findFixtureType(property, variantOption),
						valueType: "alias",
						value: property.defaults[variantOption]
					} };
					if (property.pseudoStates) for (const pseudoState of property.pseudoStates) variableObject[propertyKey][pseudoState] = {
						type: findFixtureType(property, variantOption),
						valueType: "alias",
						value: property.defaults[variantOption]
					};
					result.variables[schemaKey] = {
						...result.variables[schemaKey],
						...variableObject
					};
				}
			}
		}
	}
	if (config.subComponents) {
		const { subComponents } = config;
		for (const subComponentKey in subComponents) for (const variantKey in subComponents[subComponentKey].variants) {
			const variantConfig = subComponents[subComponentKey].variants[variantKey];
			result.defaults[variantKey] = variantConfig.default;
			for (const variantOption of variantConfig.options) if (isVariantConfigWithComponentStates(variantConfig)) {
				const componentStates = variantConfig.componentStates;
				for (const componentStateKey in componentStates) {
					const componentState = componentStates[componentStateKey];
					for (const componentStateOption of componentState.options) for (const layerKey in componentState.layers) {
						const layer = componentState.layers[layerKey];
						const schemaKey = generateSchemaKey({
							variantKey,
							variantValue: variantOption,
							componentStateKey,
							componentStateValue: componentStateOption,
							layer: layerKey,
							subComponentName: subComponentKey
						});
						result.variables[schemaKey] = {};
						for (const propertyKey in layer.properties) {
							const property = layer.properties[propertyKey];
							const variableObject = { [propertyKey]: {} };
							if (property?.skipRestState !== true) variableObject[propertyKey] = { rest: {
								type: findFixtureType(property, variantOption),
								valueType: "alias",
								value: property.defaults[variantOption]
							} };
							if (property.pseudoStates) for (const pseudoState of property.pseudoStates) variableObject[propertyKey][pseudoState] = {
								type: findFixtureType(property, variantOption),
								valueType: "alias",
								value: property.defaults[variantOption]
							};
							result.variables[schemaKey] = {
								...result.variables[schemaKey],
								...variableObject
							};
						}
					}
				}
			} else if (isVariantConfigWithProperties(variantConfig)) {
				const layers = variantConfig.layers;
				for (const layerKey in layers) {
					const layer = layers[layerKey];
					const schemaKey = generateSchemaKey({
						variantKey,
						variantValue: variantOption,
						layer: layerKey,
						subComponentName: subComponentKey
					});
					result.variables[schemaKey] = {};
					for (const propertyKey in layer.properties) {
						const property = layer.properties[propertyKey];
						const variableObject = { [propertyKey]: {} };
						if (property?.skipRestState !== true) variableObject[propertyKey] = { rest: {
							type: findFixtureType(property, variantOption),
							valueType: "alias",
							value: property.defaults[variantOption]
						} };
						if (property.pseudoStates) for (const pseudoState of property.pseudoStates) variableObject[propertyKey][pseudoState] = {
							type: findFixtureType(property, variantOption),
							valueType: "alias",
							value: property.defaults[variantOption]
						};
						result.variables[schemaKey] = {
							...result.variables[schemaKey],
							...variableObject
						};
					}
				}
			}
		}
	}
	return result;
}

//#endregion
export { buildConfigSchema, findFixtureType };