/**
 * 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
 */
import * as crypto from 'crypto';
export 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');
    }
}
//# sourceMappingURL=approval-service.js.map