// seekora-ui-sdk/packages/react/src/layout/validate-config.ts
import { SURFACE_VALUES, LAYOUT_VARIANT_VALUES, } from "@seekora-ai/ui-sdk-types";
import { getPrimitive } from "./primitive-registry";
export class LayoutConfigError extends Error {
    constructor(message) {
        super(`[LayoutConfig] ${message}`);
        this.name = "LayoutConfigError";
    }
}
const REQUIRED_SLOTS = {
    search: ["main"],
    collection: ["main"],
    // predictive trigger is optional — theme owns the input element, our layout
    // only renders the dropdown panel anchored to the theme input.
    predictive: ["panel"],
    featured: ["grid"],
    related: ["grid"],
    cart_drawer: ["items"],
};
const KNOWN_SLOTS = {
    search: ["sidebar", "header", "main"],
    collection: ["sidebar", "header", "main"],
    predictive: ["trigger", "panel"],
    featured: ["header", "grid"],
    related: ["header", "grid"],
    cart_drawer: ["header", "items", "footer"],
};
export function validateLayoutConfig(cfg) {
    if (!cfg || typeof cfg !== "object") {
        throw new LayoutConfigError("config must be an object");
    }
    const c = cfg;
    if (typeof c.surface !== "string" ||
        !SURFACE_VALUES.includes(c.surface)) {
        throw new LayoutConfigError(`surface must be one of: ${SURFACE_VALUES.join(", ")} — got ${JSON.stringify(c.surface)}`);
    }
    const surface = c.surface;
    if (typeof c.layout !== "string" ||
        !LAYOUT_VARIANT_VALUES.includes(c.layout)) {
        throw new LayoutConfigError(`layout must be one of: ${LAYOUT_VARIANT_VALUES.join(", ")} — got ${JSON.stringify(c.layout)}`);
    }
    for (const requiredSlot of REQUIRED_SLOTS[surface]) {
        const slot = c[requiredSlot];
        if (!Array.isArray(slot) || slot.length === 0) {
            throw new LayoutConfigError(`surface "${surface}" requires non-empty slot "${requiredSlot}"`);
        }
    }
    for (const slotName of KNOWN_SLOTS[surface]) {
        const slot = c[slotName];
        if (slot === undefined)
            continue;
        if (!Array.isArray(slot)) {
            throw new LayoutConfigError(`slot "${slotName}" must be an array`);
        }
        slot.forEach((s, i) => validateComponentSpec(s, `${surface}.${slotName}[${i}]`));
    }
    if (c.theme !== undefined) {
        if (typeof c.theme !== "object" ||
            c.theme === null ||
            Array.isArray(c.theme)) {
            throw new LayoutConfigError("theme must be an object");
        }
        for (const [k, v] of Object.entries(c.theme)) {
            if (!k.startsWith("--seekora-")) {
                throw new LayoutConfigError(`theme key "${k}" must start with "--seekora-"`);
            }
            if (typeof v !== "string") {
                throw new LayoutConfigError(`theme value for "${k}" must be a string`);
            }
        }
    }
}
function validateComponentSpec(spec, path) {
    if (!spec || typeof spec !== "object") {
        throw new LayoutConfigError(`${path}: must be an object`);
    }
    const s = spec;
    if (typeof s.component !== "string" || s.component.length === 0) {
        throw new LayoutConfigError(`${path}: component must be a non-empty string`);
    }
    if (!getPrimitive(s.component)) {
        throw new LayoutConfigError(`${path}: unknown component "${s.component}" — not in PRIMITIVE_REGISTRY`);
    }
    if (s.props !== undefined) {
        if (typeof s.props !== "object" ||
            s.props === null ||
            Array.isArray(s.props)) {
            throw new LayoutConfigError(`${path}: props must be an object`);
        }
    }
}
