"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.claimEscalation = claimEscalation;
exports.releaseEscalation = releaseEscalation;
const escalationService = __importStar(require("../../services/escalation"));
const userService = __importStar(require("../../services/user"));
const publish_1 = require("../../lib/events/publish");
/**
 * Claim a pending escalation for the authenticated user.
 *
 * Sets `assigned_to` and `assigned_until` on the escalation (soft lock).
 * Non-superadmin users must hold the escalation's role. Publishes a
 * `escalation.claimed` event.
 *
 * @param input.id — escalation UUID
 * @param input.durationMinutes — claim duration (default: 30)
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: { escalation, isExtension } }` or 403/404/409
 */
async function claimEscalation(input, auth) {
    try {
        const { id, durationMinutes } = input;
        const escalation = await escalationService.getEscalation(id);
        if (!escalation) {
            return { status: 404, error: 'Escalation not found' };
        }
        const isSuperAdminUser = await userService.isSuperAdmin(auth.userId);
        if (!isSuperAdminUser) {
            const userHasRole = await userService.hasRole(auth.userId, escalation.role);
            if (!userHasRole) {
                return {
                    status: 403,
                    error: `You must have the "${escalation.role}" role to claim this escalation`,
                };
            }
        }
        const result = await escalationService.claimEscalation(id, auth.userId, durationMinutes);
        if (!result) {
            return { status: 409, error: 'Escalation not available for claim' };
        }
        (0, publish_1.publishEscalationEvent)({
            type: 'escalation.claimed',
            source: 'api',
            workflowId: escalation.workflow_id || '',
            workflowName: escalation.workflow_type || '',
            taskQueue: escalation.task_queue || '',
            escalationId: id,
            status: 'claimed',
            data: { assigned_to: auth.userId },
        });
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Release a claimed escalation back to the pool.
 *
 * Only the user who holds the claim can release it. Publishes a
 * `escalation.released` event.
 *
 * @param input.id — escalation UUID
 * @param auth — authenticated user context
 * @returns `{ status: 200, data: { escalation } }` or 409
 */
async function releaseEscalation(input, auth) {
    try {
        const result = await escalationService.releaseEscalation(input.id, auth.userId);
        if (!result) {
            return { status: 409, error: 'Escalation not found or not claimed by you' };
        }
        (0, publish_1.publishEscalationEvent)({
            type: 'escalation.released',
            source: 'api',
            workflowId: result.workflow_id || '',
            workflowName: result.workflow_type || '',
            taskQueue: result.task_queue || '',
            escalationId: input.id,
            status: 'released',
            data: { released_by: auth.userId },
        });
        return { status: 200, data: { escalation: result } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
