/**
 * Supervisor-backed Contributor Action Gate (spec 097, NFR-5).
 *
 * Concrete adapter for {@link IContributorActionGate} that funnels every
 * contributor pipeline side-effect through {@link ISupervisorAgent}. The
 * supervisor is the single chokepoint where autonomy / approval policy
 * is applied — this adapter only translates the narrow contributor port
 * into the generic supervisor evaluation envelope.
 *
 * Verdict mapping:
 *   - `approve` / `advise` → approved (autonomous or advisory greenlight)
 *   - `reject`            → denied (policy explicitly blocked it)
 *   - `escalate`          → denied (needs a human; gate must not silently fire)
 *
 * Per the port contract, this adapter MUST resolve (never throw). Any
 * supervisor failure is captured as a denial with the error in the
 * rationale so callers can short-circuit and audit-log the outage.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { SupervisorAutonomy, SupervisorScopeType, SupervisorVerdict, } from '../../../domain/generated/output.js';
const CONTRIBUTOR_GATE_ACTOR = 'contributor-onboarding';
const SUPERVISOR_TARGET = 'supervisor';
/** Default policy used until a persisted contributor policy is wired. */
function defaultContributorPolicy(now) {
    const iso = now.toISOString();
    return {
        id: 'contributor-onboarding-default',
        createdAt: iso,
        updatedAt: iso,
        scopeType: SupervisorScopeType.global,
        enabled: true,
        autonomyLevel: SupervisorAutonomy.advisory,
    };
}
let SupervisorContributorActionGate = class SupervisorContributorActionGate {
    supervisor;
    constructor(supervisor) {
        this.supervisor = supervisor;
    }
    async gate(input) {
        const now = new Date();
        const sourceEventId = `contributor-action-${input.kind}-${now.getTime()}`;
        const event = {
            kind: 'message',
            scopeType: SupervisorScopeType.global,
            messageId: sourceEventId,
            messageKind: input.kind,
            fromActor: CONTRIBUTOR_GATE_ACTOR,
            toTarget: SUPERVISOR_TARGET,
            payload: JSON.stringify({
                summary: input.summary,
                context: input.context ?? null,
            }),
            sourceEventId,
        };
        try {
            const decision = await this.supervisor.evaluate({
                event,
                policy: defaultContributorPolicy(now),
            });
            const approved = decision.verdict === SupervisorVerdict.approve ||
                decision.verdict === SupervisorVerdict.advise;
            return { approved, rationale: decision.rationale };
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            return {
                approved: false,
                rationale: `supervisor evaluation failed: ${message}`,
            };
        }
    }
};
SupervisorContributorActionGate = __decorate([
    injectable(),
    __param(0, inject('ISupervisorAgent')),
    __metadata("design:paramtypes", [Object])
], SupervisorContributorActionGate);
export { SupervisorContributorActionGate };
