/**
 * Shared metadata for human-visible kit exports (YAML/JSON) that must not be treated as runtime truth.
 * Canonical task/planning state lives in SQLite until the git-backed event log ships (Phase 114+).
 */
export const KIT_EXPORT_ENVELOPE_SCHEMA_VERSION = 1;
export function buildKitExportEnvelopeV1(args) {
    const seq = Number(args.sourceSequence);
    if (!Number.isInteger(seq) || seq < 0) {
        throw new Error(`kit export envelope: sourceSequence must be a non-negative integer (got ${args.sourceSequence})`);
    }
    return {
        schemaVersion: KIT_EXPORT_ENVELOPE_SCHEMA_VERSION,
        authoritative: false,
        generatedAt: args.generatedAt ?? new Date().toISOString(),
        sourceSequence: seq,
        sourceKind: args.sourceKind.trim(),
        role: "sqlite-projection-export"
    };
}
/** YAML mapping block placed at the top of maintainer export files. */
export function formatKitExportEnvelopeYamlBlock(envelope) {
    const esc = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
    return [
        "kit_export_envelope:",
        `  schema_version: ${envelope.schemaVersion}`,
        "  authoritative: false",
        `  generated_at: "${esc(envelope.generatedAt)}"`,
        `  source_sequence: ${envelope.sourceSequence}`,
        `  source_kind: "${esc(envelope.sourceKind)}"`,
        `  role: "${esc(envelope.role)}"`,
        ""
    ].join("\n");
}
export function wrapJsonExportWithEnvelopeV1(envelope, payload) {
    return { kitExportEnvelope: envelope, payload };
}
/** Parse `source_sequence` from a workspace-status DB export YAML body (structured block preferred). */
/** Accept wrapped JSON exports (`kitExportEnvelope` + `payload`) or legacy bare payloads. */
export function unwrapKitJsonExportPayload(parsed) {
    if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
        return parsed;
    }
    const rec = parsed;
    if (rec.kitExportEnvelope && rec.payload !== undefined) {
        return rec.payload;
    }
    return parsed;
}
export function readSourceSequenceFromExportYaml(body) {
    const block = body.match(/^\s*source_sequence:\s*([0-9]+)\s*$/m);
    if (block) {
        const n = Number(block[1]);
        return Number.isInteger(n) && n >= 0 ? n : null;
    }
    const legacy = body.match(/^# workspace_revision:\s*([0-9]+)\s*$/m);
    if (legacy) {
        const n = Number(legacy[1]);
        return Number.isInteger(n) && n >= 0 ? n : null;
    }
    return null;
}
