import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import AjvImport from "ajv";
const SCHEMA_PATH = path.join(fileURLToPath(new URL("../../..", import.meta.url)), "schemas/skill-pack-manifest.schema.json");
function createAjv() {
    const Ctor = AjvImport;
    /** strict:false — bundled schema uses `$schema` draft URL meta; Ajv 8 does not load that ref in strict mode. */
    return new Ctor({ allErrors: true, strict: false });
}
let validateManifest;
function getValidator() {
    if (!validateManifest) {
        const raw = readFileSync(SCHEMA_PATH, "utf8");
        const schema = JSON.parse(raw);
        delete schema.$schema;
        delete schema.$id;
        validateManifest = createAjv().compile(schema);
    }
    return validateManifest;
}
export function validateSidecarJson(data) {
    const v = getValidator();
    if (!v(data)) {
        const errs = (v.errors ?? []);
        const msg = errs.map((e) => `${e.instancePath || "/"} ${e.message ?? ""}`.trim()).join("; ");
        return { ok: false, message: msg || "manifest schema validation failed" };
    }
    return { ok: true, manifest: data };
}
