"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listEscalations = listEscalations;
exports.getEscalation = getEscalation;
exports.claimEscalation = claimEscalation;
exports.releaseEscalation = releaseEscalation;
exports.resolveEscalation = resolveEscalation;
const picocolors_1 = __importDefault(require("picocolors"));
const client_1 = require("../client");
const format_1 = require("../format");
const COLUMNS = [
    { key: 'id', label: 'ID', width: 12, format: (v) => v?.slice(0, 12) || '' },
    { key: 'type', label: 'Type', width: 16 },
    { key: 'role', label: 'Role', width: 12 },
    { key: 'status', label: 'Status', width: 10, format: format_1.formatStatus },
    { key: 'priority', label: 'Pri', width: 4, align: 'right' },
    { key: 'created_at', label: 'Created', width: 12, format: format_1.formatTime },
];
async function listEscalations(opts) {
    const params = new URLSearchParams();
    if (opts.status)
        params.set('status', opts.status);
    if (opts.role)
        params.set('role', opts.role);
    if (opts.limit)
        params.set('limit', opts.limit);
    const data = await (0, client_1.apiFetch)(`/escalations?${params}`);
    (0, format_1.output)(data, data.escalations || [], COLUMNS, opts);
}
async function getEscalation(id, opts) {
    const data = await (0, client_1.apiFetch)(`/escalations/${id}`);
    if (opts.json) {
        console.log(JSON.stringify(data, null, 2));
        return;
    }
    console.log(`\n  ${picocolors_1.default.bold(data.type)} ${picocolors_1.default.dim(data.id)}`);
    console.log(`  Status: ${(0, format_1.formatStatus)(data.status)}  Priority: ${data.priority}  Role: ${data.role}`);
    if (data.description)
        console.log(`  ${data.description}`);
    if (data.assigned_to)
        console.log(`  Assigned: ${data.assigned_to}`);
    console.log(`  Created: ${(0, format_1.formatTime)(data.created_at)}  Updated: ${(0, format_1.formatTime)(data.updated_at)}`);
    console.log();
}
async function claimEscalation(id, opts) {
    const body = {};
    if (opts.duration)
        body.durationMinutes = parseInt(opts.duration, 10);
    const data = await (0, client_1.apiFetch)(`/escalations/${id}/claim`, {
        method: 'POST',
        body: JSON.stringify(body),
    });
    console.log(`\n  ${picocolors_1.default.green('✓')} Claimed ${picocolors_1.default.dim(id)}\n`);
}
async function releaseEscalation(id) {
    await (0, client_1.apiFetch)(`/escalations/${id}/release`, { method: 'POST' });
    console.log(`\n  ${picocolors_1.default.green('✓')} Released ${picocolors_1.default.dim(id)}\n`);
}
async function resolveEscalation(id, opts) {
    const resolverPayload = opts.data ? JSON.parse(opts.data) : {};
    await (0, client_1.apiFetch)(`/escalations/${id}/resolve`, {
        method: 'POST',
        body: JSON.stringify({ resolverPayload }),
    });
    console.log(`\n  ${picocolors_1.default.green('✓')} Resolved ${picocolors_1.default.dim(id)}\n`);
}
