import Ajv2020Import from "ajv/dist/2020.js";
import envelopeSchema from "./schemas/task-state-event-envelope.v1.json" with { type: "json" };
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ajv/dist/2020 default export shape varies by bundler
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(envelopeSchema);
    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"}`;
    });
}
const ENVELOPE_ONLY_KEYS = new Set([
    "schemaVersion",
    "eventId",
    "sequence",
    "parentEventId",
    "recordedAt",
    "actor",
    "clientMutationId",
    "command",
    "workspace"
]);
function envelopeOnlyInput(input) {
    if (!input || typeof input !== "object" || Array.isArray(input)) {
        return input;
    }
    const out = {};
    for (const key of ENVELOPE_ONLY_KEYS) {
        if (key in input) {
            out[key] = input[key];
        }
    }
    return out;
}
export function validateTaskStateEventEnvelope(input) {
    const validate = loadValidator();
    const slice = envelopeOnlyInput(input);
    if (!validate(slice)) {
        return { ok: false, errors: formatAjvErrors(validate.errors) };
    }
    const extra = input && typeof input === "object" && !Array.isArray(input)
        ? Object.keys(input).filter((k) => !ENVELOPE_ONLY_KEYS.has(k))
        : [];
    if (extra.length > 0) {
        return { ok: false, errors: [`(root): unexpected properties for envelope-only validation: ${extra.join(", ")}`] };
    }
    return { ok: true, data: slice };
}
export function taskStateEventEnvelopeSchemaRelativePath() {
    return "src/modules/task-engine/task-state-events/schemas/task-state-event-envelope.v1.json";
}
