import Ajv2020Import from "ajv/dist/2020.js";
import manifestSchema from "./schemas/task-state-manifest.v1.json" with { type: "json" };
import { TASK_STATE_GIT_BRANCH, TASK_STATE_ROOT_DIR } from "./constants.js";
import { digestTaskStateCanonicalJson } from "./integrity.js";
// 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(manifestSchema);
    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 manifestBodyForDigest(manifest) {
    const { manifestDigest: _omit, ...rest } = manifest;
    return rest;
}
export function computeManifestDigest(manifest) {
    return digestTaskStateCanonicalJson(manifestBodyForDigest(manifest));
}
export function validateTaskStateGitManifest(input, options) {
    const validate = loadValidator();
    if (!validate(input)) {
        return { ok: false, errors: formatAjvErrors(validate.errors) };
    }
    const data = input;
    const errors = [];
    if (data.branch !== TASK_STATE_GIT_BRANCH) {
        errors.push(`branch: expected ${TASK_STATE_GIT_BRANCH}, got ${data.branch}`);
    }
    if (data.root !== TASK_STATE_ROOT_DIR) {
        errors.push(`root: expected ${TASK_STATE_ROOT_DIR}, got ${data.root}`);
    }
    if (options?.verifyManifestDigest && data.manifestDigest) {
        const expected = computeManifestDigest(data);
        if (data.manifestDigest !== expected) {
            errors.push(`manifestDigest: expected ${expected}, got ${data.manifestDigest}`);
        }
    }
    if (errors.length > 0) {
        return { ok: false, errors };
    }
    return { ok: true, data };
}
export function taskStateManifestSchemaRelativePath() {
    return "src/modules/task-engine/task-state-git/schemas/task-state-manifest.v1.json";
}
