/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { getConfigSubcomponents } from "./subcomponents.js";
import { coalesceConfigVariant } from "./coalesceConfigVariant.js";
import { entries } from "lodash-es";

//#region ../automated-config/dist/utils/getConfigVariants.js
/*! © 2026 Yahoo, Inc. UDS Default Config v0.0.0-development */
function getConfigVariants(config, variantKey = "variant") {
	const result = coalesceConfigVariant(config, variantKey);
	if (result.type === "no-variants") return {
		type: "no-variants",
		description: "No variants"
	};
	if (result.type === "no-sub-components") {
		if (getConfigSubcomponents(config)) throw new Error("A config with a root variants.variant defintion cannot have subcomponents");
		return {
			type: "no-sub-components",
			description: result.variant.description,
			variantNames: [...result.variant.options]
		};
	}
	const variantToSubcomponentNames = entries(result.variants).reduce((acc, [subComponentName, subComponentVariantDef]) => {
		for (const variant of subComponentVariantDef.options) {
			acc[variant] ??= [];
			if (!acc[variant].includes(subComponentName)) acc[variant].push(subComponentName);
		}
		return acc;
	}, {});
	return {
		type: "has-sub-components",
		description: Object.values(result.variants).map((c) => c.description).filter((c) => c)[0] ?? "",
		variantNames: Object.keys(variantToSubcomponentNames),
		variantToSubcomponentNames
	};
}

//#endregion
export { getConfigVariants };