"use strict";
/**
 * ApprovalService -- REST API-friendly wrapper around ApprovalQueue.
 *
 * Bridges the gap between the HMAC-based ApprovalQueue and the
 * dashboard REST API. Handles token generation for authenticated
 * users so the dashboard doesn't need to know the HMAC secret.
 *
 * @connexum/ai-governance
 */
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.ApprovalService = void 0;
const crypto = __importStar(require("crypto"));
class ApprovalService {
    queue;
    secret;
    constructor(config) {
        this.queue = config.queue;
        this.secret = config.approvalSecret;
    }
    /**
     * Submit a new approval request.
     */
    submit(agent, tool, inputSummary, reason) {
        return this.queue.submit(agent, tool, inputSummary, reason);
    }
    /**
     * Resolve an approval from the dashboard.
     *
     * Generates the HMAC token internally so dashboard API handlers
     * don't need access to the approval secret.
     */
    resolveFromDashboard(decision) {
        const approvalToken = this.generateToken(decision.requestId, decision.status);
        const queueDecision = {
            requestId: decision.requestId,
            status: decision.status,
            resolvedBy: `human:${decision.userId}`,
            note: decision.note,
            approvalToken,
        };
        return this.queue.resolve(queueDecision);
    }
    /**
     * Resolve multiple approvals at once (bulk approve/deny from dashboard).
     */
    bulkResolve(decisions) {
        const resolved = [];
        const errors = [];
        for (const decision of decisions) {
            try {
                resolved.push(this.resolveFromDashboard(decision));
            }
            catch (err) {
                errors.push({ requestId: decision.requestId, error: err.message });
            }
        }
        return { resolved, errors };
    }
    /**
     * Get all pending approval requests.
     */
    getPending() {
        return this.queue.getPending();
    }
    /**
     * Check the status of a specific request.
     */
    check(requestId) {
        return this.queue.check(requestId);
    }
    /**
     * Get approval queue statistics.
     */
    getStats() {
        return this.queue.getStats();
    }
    /**
     * Get all escalated requests.
     */
    getEscalated() {
        return this.queue.getEscalated();
    }
    /**
     * Generate the HMAC approval token for a given request + status.
     * This is the bridge: the dashboard calls this service, which
     * has the secret, so the dashboard API handler doesn't need it.
     */
    generateToken(requestId, status) {
        return crypto.createHmac('sha256', this.secret)
            .update(`${requestId}:${status}`)
            .digest('hex');
    }
}
exports.ApprovalService = ApprovalService;
//# sourceMappingURL=approval-service.js.map