/**
 * PublishMonthlyRecapUseCase — spec 097, FR-32 / NFR-5.
 *
 * Fans a `RecapArtifact` out across the configured `IRecapPublisher`
 * adapters via `Promise.allSettled`. Each channel is individually gated
 * through `IContributorActionGate`, so a single denial never blocks the
 * other channels and a single failure does not poison the run.
 *
 * Returns a per-channel status map so the caller can surface partial
 * progress (e.g. "file ✓, discord ✓, github-discussion ⚠ rate-limited").
 */
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, injectAll } from 'tsyringe';
import { RecapChannel } from '../../../domain/generated/output.js';
const CHANNEL_GATE_KINDS = {
    [RecapChannel.File]: 'recap-publish-file',
    [RecapChannel.Discord]: 'recap-publish-discord',
    [RecapChannel.GithubDiscussion]: 'recap-publish-github-discussion',
};
let PublishMonthlyRecapUseCase = class PublishMonthlyRecapUseCase {
    gate;
    publishers;
    constructor(gate, publishers) {
        this.gate = gate;
        this.publishers = publishers;
    }
    async execute(input) {
        const settled = await Promise.allSettled(input.targets.map((target) => this.runChannel(input.artifact, target)));
        const outcomes = settled.map((result, idx) => {
            const target = input.targets[idx];
            if (result.status === 'fulfilled')
                return result.value;
            return {
                channel: target.channel,
                status: 'failed',
                error: errorMessage(result.reason),
            };
        });
        return { outcomes };
    }
    async runChannel(artifact, target) {
        const publisher = this.publishers.find((p) => p.channel === target.channel);
        if (!publisher) {
            return {
                channel: target.channel,
                status: 'skipped',
                reason: `No publisher registered for channel ${target.channel}`,
            };
        }
        const decision = await this.gate.gate({
            kind: CHANNEL_GATE_KINDS[target.channel],
            summary: `Publish recap "${artifact.title}" to ${target.channel}`,
            context: { recapId: artifact.recapId, channel: target.channel },
        });
        if (!decision.approved) {
            return { channel: target.channel, status: 'denied', rationale: decision.rationale };
        }
        const result = await publisher.publish(artifact, target);
        return { channel: target.channel, status: 'published', reference: result.reference };
    }
};
PublishMonthlyRecapUseCase = __decorate([
    injectable(),
    __param(0, inject('IContributorActionGate')),
    __param(1, injectAll('IRecapPublisher')),
    __metadata("design:paramtypes", [Object, Array])
], PublishMonthlyRecapUseCase);
export { PublishMonthlyRecapUseCase };
function errorMessage(reason) {
    if (reason instanceof Error)
        return reason.message;
    return String(reason);
}
