"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.getVisibleRoles = getVisibleRoles;
exports.validateIds = validateIds;
exports.checkBulkPermission = checkBulkPermission;
exports.publishBulkClaimEvents = publishBulkClaimEvents;
const escalationService = __importStar(require("../../services/escalation"));
const userService = __importStar(require("../../services/user"));
const publish_1 = require("../../lib/events/publish");
// ── Private helpers ────────────────────────────────────────────────────────
async function getVisibleRoles(userId) {
    const isSuperAdminUser = await userService.isSuperAdmin(userId);
    if (isSuperAdminUser)
        return undefined;
    const userRoles = await userService.getUserRoles(userId);
    return userRoles.map((r) => r.role);
}
function validateIds(ids) {
    return Array.isArray(ids) && ids.length > 0;
}
async function checkBulkPermission(userId, ids) {
    const isSuperAdminUser = await userService.isSuperAdmin(userId);
    if (isSuperAdminUser)
        return { allowed: true };
    const roles = await escalationService.getEscalationRoles(ids);
    for (const role of roles) {
        const canManage = await userService.isGroupAdmin(userId, role);
        if (!canManage) {
            return { allowed: false, status: 403, error: `Insufficient permissions for role "${role}"` };
        }
    }
    return { allowed: true };
}
function publishBulkClaimEvents(ids, assignedTo) {
    for (const id of ids) {
        (0, publish_1.publishEscalationEvent)({
            type: 'escalation.claimed',
            source: 'api',
            workflowId: '',
            workflowName: '',
            taskQueue: '',
            escalationId: id,
            status: 'claimed',
            data: { assigned_to: assignedTo, bulk: true },
        });
    }
}
