/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
//#region ../utils/dist/serializeToJS.js
/*! © 2026 Yahoo, Inc. UDS Utils v0.0.0-development */
const VALID_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
function escapeString(str) {
	return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
}
function serializeKey(key) {
	if (VALID_IDENTIFIER.test(key)) return key;
	return `'${escapeString(key)}'`;
}
const RGB_KEY_ORDER = [
	"type",
	"r",
	"g",
	"b"
];
const BREAKPOINT_ORDER = {
	sm: 0,
	md: 1,
	lg: 2,
	xl: 3,
	"2xl": 4
};
function isBreakpointObject(keys) {
	return keys.length > 0 && keys.every((k) => k in BREAKPOINT_ORDER);
}
function sortObjectKeys(obj) {
	const keys = Object.keys(obj);
	if (obj.type === "rgb") {
		const ordered = RGB_KEY_ORDER.filter((k) => keys.includes(k));
		const remaining = keys.filter((k) => !RGB_KEY_ORDER.includes(k));
		remaining.sort((a, b) => a.localeCompare(b, void 0, { numeric: true }));
		return [...ordered, ...remaining];
	}
	if (isBreakpointObject(keys)) return keys.sort((a, b) => BREAKPOINT_ORDER[a] - BREAKPOINT_ORDER[b]);
	keys.sort((a, b) => a.localeCompare(b, void 0, { numeric: true }));
	if (keys.includes("type")) return ["type", ...keys.filter((k) => k !== "type")];
	return keys;
}
function serializeValue(value, indent, baseIndent) {
	if (value === null) return "null";
	switch (typeof value) {
		case "string": return `'${escapeString(value)}'`;
		case "number":
		case "boolean": return String(value);
	}
	const nextIndent = indent + baseIndent;
	if (Array.isArray(value)) {
		if (value.length === 0) return "[]";
		return `[\n${value.map((item) => `${nextIndent}${serializeValue(item, nextIndent, baseIndent)},`).join("\n")}\n${indent}]`;
	}
	const obj = value;
	const keys = sortObjectKeys(obj);
	if (keys.length === 0) return "{}";
	return `{\n${keys.map((key) => `${nextIndent}${serializeKey(key)}: ${serializeValue(obj[key], nextIndent, baseIndent)},`).join("\n")}\n${indent}}`;
}
/**
* Serializes a JSON-serializable value to JS source code with:
* - Single quotes for strings
* - Trailing commas on every property and array element
* - 2-space indentation
* - Alphabetically sorted object keys at all nesting levels
*
* The output matches the repo's Prettier config (singleQuote, trailingComma: "all")
* so that running Prettier on the result is a no-op.
*/
function serializeToJS(value) {
	return serializeValue(value, "", "  ");
}
//#endregion
exports.serializeToJS = serializeToJS;
