"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.classifyArtifactLifecycle = classifyArtifactLifecycle;
exports.isCompletedTaskLifecycle = isCompletedTaskLifecycle;
exports.isTaskReadyToImplement = isTaskReadyToImplement;
exports.createArtifactLifecycleFindings = createArtifactLifecycleFindings;
exports.readArtifactLifecycleState = readArtifactLifecycleState;
function classifyArtifactLifecycle(state) {
    if (state.artifactType !== "task_definition")
        return "not-applicable";
    if (state.status === "completed" && state.readiness === "completed")
        return "completed";
    if (state.readiness === "ready to implement")
        return "ready-to-implement";
    return "needs-evidence";
}
function isCompletedTaskLifecycle(state) {
    return classifyArtifactLifecycle(state) === "completed";
}
function isTaskReadyToImplement(state) {
    return classifyArtifactLifecycle(state) === "ready-to-implement";
}
function createArtifactLifecycleFindings(artifactPath, state) {
    if (classifyArtifactLifecycle(state) !== "needs-evidence")
        return [];
    return [{
            code: "TASK_NOT_READY",
            message: "The selected task has not passed the canonical readiness transition.",
            severity: "error",
            path: artifactPath,
            recovery: `day-shift task readiness-review --task-definition ${artifactPath} --format json`
        }];
}
function readArtifactLifecycleState(rawText) {
    return {
        artifactType: readFrontMatterScalar(rawText, "artifact_type"),
        status: readFrontMatterScalar(rawText, "status"),
        readiness: readFrontMatterScalar(rawText, "readiness"),
        implementationStatus: readFrontMatterScalar(rawText, "implementation_status"),
        reconciliationStatus: readFrontMatterScalar(rawText, "reconciliation_status")
    };
}
function readFrontMatterScalar(rawText, key) {
    const frontMatter = rawText.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/)?.[1] ?? "";
    const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
    const value = frontMatter.match(new RegExp(`^${escapedKey}:\\s*["']?([^"'\\r\\n]+)["']?\\s*$`, "m"))?.[1].trim();
    return value === undefined || value.length === 0 ? null : value;
}
