import Ajv2020Import from "ajv/dist/2020.js";
import snapshotMetaSchema from "./schemas/task-state-snapshot-meta.v1.json" with { type: "json" };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Ajv2020Ctor = Ajv2020Import.default ?? Ajv2020Import;
let compiled = null;
function loadValidator() {
    if (compiled) {
        return compiled;
    }
    const ajv = new Ajv2020Ctor({ strict: false, allErrors: true });
    compiled = ajv.compile(snapshotMetaSchema);
    return compiled;
}
function formatAjvErrors(errors) {
    if (!errors?.length) {
        return ["validation failed"];
    }
    return errors.map((e) => {
        const p = e.instancePath?.length ? e.instancePath : "(root)";
        return `${p}: ${e.message ?? e.keyword ?? "error"}`;
    });
}
export function validateTaskStateGitSnapshotMeta(input) {
    const validate = loadValidator();
    if (!validate(input)) {
        return { ok: false, errors: formatAjvErrors(validate.errors) };
    }
    return { ok: true, data: input };
}
export function taskStateSnapshotMetaSchemaRelativePath() {
    return "src/modules/task-engine/task-state-git/schemas/task-state-snapshot-meta.v1.json";
}
