import { validateThemeJson } from "./theme-schema.js";
export function assertThemeNameIsValid(name) {
    if (name.includes("/")) {
        throw new Error(`Invalid theme name "${name}": theme names cannot contain "/" because it is reserved for automatic light/dark theme settings.`);
    }
}
export function parseThemeJson(label, json) {
    if (!validateThemeJson.Check(json)) {
        const errors = Array.from(validateThemeJson.Errors(json));
        const missingColors = new Set();
        const otherErrors = [];
        for (const error of errors) {
            if (error.keyword === "required" && error.instancePath === "/colors") {
                const requiredProperties = error.params.requiredProperties;
                for (const requiredProperty of requiredProperties ?? []) {
                    missingColors.add(requiredProperty);
                }
                continue;
            }
            const path = error.instancePath || "/";
            otherErrors.push(`  - ${path}: ${error.message}`);
        }
        let errorMessage = `Invalid theme "${label}":\n`;
        if (missingColors.size > 0) {
            errorMessage += "\nMissing required color tokens:\n";
            errorMessage += Array.from(missingColors)
                .sort()
                .map((color) => `  - ${color}`)
                .join("\n");
            errorMessage += '\n\nPlease add these colors to your theme\'s "colors" object.';
            errorMessage += "\nSee the built-in themes (dark.json, light.json) for reference values.";
        }
        if (otherErrors.length > 0) {
            errorMessage += `\n\nOther errors:\n${otherErrors.join("\n")}`;
        }
        throw new Error(errorMessage);
    }
    const themeJson = json;
    assertThemeNameIsValid(themeJson.name);
    return themeJson;
}
export function parseThemeJsonContent(label, content) {
    let json;
    try {
        json = JSON.parse(content);
    }
    catch (error) {
        throw new Error(`Failed to parse theme ${label}: ${error}`);
    }
    return parseThemeJson(label, json);
}
//# sourceMappingURL=theme-parse.js.map