"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ltResolveEscalation = ltResolveEscalation;
exports.ltClaimEscalation = ltClaimEscalation;
exports.ltEnrichEscalationRouting = ltEnrichEscalationRouting;
exports.ltCreateEscalation = ltCreateEscalation;
const escalationService = __importStar(require("../../escalation"));
const logger_1 = require("../../../lib/logger");
/**
 * Resolve an escalation record. Called by the interceptor after
 * detecting a re-run (resolver data present in the envelope).
 * This makes resolution durable: if the server crashes after
 * signaling but before DB update, the workflow replays and
 * retries this activity.
 */
async function ltResolveEscalation(input) {
    const result = await escalationService.resolveEscalation(input.escalationId, input.resolverPayload);
    if (!result) {
        logger_1.loggerRegistry.warn(`[ltResolveEscalation] Escalation ${input.escalationId} already resolved or not found`);
    }
}
/**
 * Claim an escalation to a specific user. Used for auto-assignment
 * when the system creates an advisory escalation (e.g., rounds exhausted).
 */
async function ltClaimEscalation(input) {
    const result = await escalationService.claimEscalation(input.escalationId, input.userId, input.durationMinutes);
    if (!result) {
        logger_1.loggerRegistry.warn(`[ltClaimEscalation] Escalation ${input.escalationId} could not be claimed`);
    }
}
/**
 * Enrich an escalation record with signal routing metadata and workflow
 * context so the resolution API can signal the paused workflow directly
 * and the dashboard can display full context. Optionally auto-claims
 * the escalation to the initiating user.
 */
async function ltEnrichEscalationRouting(input) {
    const result = await escalationService.enrichEscalationRouting(input.escalationId, { signal_routing: { ...input.signalRouting, engine: 'durable' } }, {
        workflowType: input.signalRouting.workflowType,
        workflowId: input.signalRouting.workflowId,
        taskQueue: input.signalRouting.taskQueue,
        taskId: input.taskId,
    });
    if (!result) {
        logger_1.loggerRegistry.warn(`[ltEnrichEscalationRouting] Escalation ${input.escalationId} not found`);
        return;
    }
    // Auto-claim to the initiating user with a long window (4 hours)
    if (input.claimForUserId) {
        await escalationService.claimEscalation(input.escalationId, input.claimForUserId, 240);
    }
}
/**
 * Create an escalation record when a workflow needs human intervention.
 */
async function ltCreateEscalation(input) {
    const escalation = await escalationService.createEscalation({
        type: input.type,
        subtype: input.subtype,
        description: input.description,
        priority: input.priority,
        task_id: input.taskId,
        origin_id: input.originId,
        parent_id: input.parentId,
        role: input.role,
        envelope: input.envelope,
        metadata: input.metadata,
        escalation_payload: input.escalationPayload,
        workflow_id: input.workflowId,
        task_queue: input.taskQueue,
        workflow_type: input.workflowType,
        trace_id: input.traceId,
        span_id: input.spanId,
    });
    return escalation.id;
}
