"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.classifyArtifactLifecycle = classifyArtifactLifecycle;
exports.isCompletedTaskLifecycle = isCompletedTaskLifecycle;
exports.isTaskReadyToImplement = isTaskReadyToImplement;
exports.createArtifactLifecycleFindings = createArtifactLifecycleFindings;
exports.resolveArtifactLifecycleTransitionPrecondition = resolveArtifactLifecycleTransitionPrecondition;
exports.readArtifactLifecycleState = readArtifactLifecycleState;
const ARTIFACT_LIFECYCLE_TRANSITION_PRECONDITIONS = Object.freeze([
    {
        actionId: "task readiness-authorize",
        findingCode: "TASK_NOT_READY",
        relevance: "warning",
        explanation: "The selected action is the canonical transition that resolves the task's pre-readiness lifecycle state."
    }
]);
function classifyArtifactLifecycle(state) {
    if (state.artifactType !== "task_definition")
        return "not-applicable";
    if (state.status === "completed" || state.status === "cancelled")
        return "completed";
    if (state.readiness === "ready_to_implement"
        || state.readiness === "ready to implement"
        || state.readiness === "completed")
        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 resolveArtifactLifecycleTransitionPrecondition(actionId, findingCode) {
    const normalizedActionId = actionId.trim();
    const normalizedFindingCode = findingCode.trim().toUpperCase();
    return ARTIFACT_LIFECYCLE_TRANSITION_PRECONDITIONS.find((entry) => entry.actionId === normalizedActionId && entry.findingCode === normalizedFindingCode) ?? null;
}
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;
}
