"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.conditionLT = conditionLT;
const hotmesh_1 = require("@hotmeshio/hotmesh");
const interceptorActivities = __importStar(require("../interceptor/activities"));
const LT_ACTIVITY_QUEUE = 'lt-interceptor';
/**
 * Wait for a signal and resolve the associated escalation automatically.
 *
 * Wraps `Durable.workflow.condition()` with escalation lifecycle:
 * when the signal arrives (from the dashboard resolve endpoint),
 * the payload includes an injected `$escalation_id` field. This
 * helper strips it, calls `ltResolveEscalation` as a durable
 * activity, and returns the clean resolver payload.
 *
 * Usage (from within a workflow):
 * ```typescript
 * import { conditionLT } from '@hotmeshio/long-tail';
 *
 * export async function myWorkflow(envelope: LTEnvelope) {
 *   // Create an escalation with signal_id in metadata
 *   const signalId = `approval-${Durable.workflow.workflowId}`;
 *   await activities.ltCreateEscalation({
 *     type: 'approval',
 *     role: 'reviewer',
 *     metadata: { signal_id: signalId },
 *     // ...
 *   });
 *
 *   // Wait — the dashboard signals on resolve
 *   const decision = await conditionLT<{ approved: boolean }>(signalId);
 *   // decision.approved is clean — no $escalation_id
 * }
 * ```
 *
 * If the signal payload does not contain `$escalation_id` (e.g., signaled
 * manually), the function returns the payload as-is without calling
 * the resolve activity.
 */
async function conditionLT(signalId) {
    const raw = await hotmesh_1.Durable.workflow.condition(signalId);
    const escalationId = raw.$escalation_id;
    if (escalationId) {
        // Resolve the escalation as a durable activity (crash-safe)
        const { ltResolveEscalation } = hotmesh_1.Durable.workflow.proxyActivities({
            activities: interceptorActivities,
            taskQueue: LT_ACTIVITY_QUEUE,
            retry: { maximumAttempts: 3 },
        });
        // Strip $escalation_id before passing as resolver payload
        const { $escalation_id: _, ...resolverPayload } = raw;
        await ltResolveEscalation({
            escalationId,
            resolverPayload: resolverPayload,
        });
        return resolverPayload;
    }
    return raw;
}
