export const HUMAN_GATE_STATUSES = [
    "awaiting_review",
    "awaiting_policy_approval",
    "awaiting_external_decision"
];
export const HUMAN_GATE_ENTER_ACTIONS = new Set([
    "await_review",
    "await_policy_approval",
    "await_external_decision"
]);
export const HUMAN_GATE_RESUME_ACTIONS = new Set(["resume_ready", "resume_work"]);
const ENTER_ACTION_TO_STATUS = {
    await_review: "awaiting_review",
    await_policy_approval: "awaiting_policy_approval",
    await_external_decision: "awaiting_external_decision"
};
export function isHumanGateStatus(status) {
    return HUMAN_GATE_STATUSES.includes(status);
}
export function humanGateStatusForEnterAction(action) {
    return ENTER_ACTION_TO_STATUS[action];
}
export function readHumanGateInput(args) {
    const owner = typeof args.humanGateOwner === "string" ? args.humanGateOwner.trim() : undefined;
    const requestedDecision = typeof args.requestedDecision === "string" ? args.requestedDecision.trim() : undefined;
    const reason = typeof args.humanGateReason === "string" ? args.humanGateReason.trim() : undefined;
    return {
        ...(owner ? { owner } : {}),
        ...(requestedDecision ? { requestedDecision } : {}),
        ...(reason ? { reason } : {})
    };
}
export function applyHumanGateMetadata(task, targetState, action, timestamp, args) {
    const base = { ...(task.metadata ?? {}) };
    const enterKind = humanGateStatusForEnterAction(action);
    if (enterKind && isHumanGateStatus(targetState)) {
        const gate = {
            ...readHumanGateInput(args),
            enteredAt: timestamp,
            gateKind: enterKind
        };
        return { ...task, metadata: { ...base, humanGate: gate } };
    }
    if (HUMAN_GATE_RESUME_ACTIONS.has(action) || targetState === "blocked" || targetState === "cancelled") {
        const { humanGate: _removed, ...rest } = base;
        return { ...task, metadata: Object.keys(rest).length > 0 ? rest : undefined };
    }
    return task;
}
export function humanGateAgeMs(gate, nowMs = Date.now()) {
    const entered = Date.parse(gate.enteredAt);
    return Number.isFinite(entered) ? Math.max(0, nowMs - entered) : 0;
}
export function humanGateResumeCommand(task) {
    const gate = task.metadata?.humanGate;
    const owner = gate?.owner ? ` for ${gate.owner}` : "";
    return `pnpm exec wk run run-transition '{"taskId":"${task.id}","action":"resume_work","policyApproval":{"confirmed":true,"rationale":"human gate cleared${owner}"}}'`;
}
