/**
 * SendAgentMessageUseCase
 *
 * Application-layer entry point for publishing an {@link AgentMessage} to
 * the {@link IAgentMessageBus}. Enforces:
 *
 *  - The hub-and-spoke addressing rule (peer addressing rejected at the
 *    use-case boundary so the error is surfaced before any persistence).
 *  - The collaboration feature flag short-circuit (NFR-14): with the flag
 *    off, no message is written and consumers receive `enabled: false`.
 *
 * The use case generates the message id and timestamps so callers do not
 * need to know about the persistence shape. Payload may be passed as a
 * string (already-serialized) or a structured object (auto-stringified).
 */
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 { randomUUID } from 'node:crypto';
import { ALLOWED_AGENT_MESSAGE_TARGET_KINDS } from '../../ports/output/agents/agent-message-bus.interface.js';
import { PeerAddressingForbiddenError } from '../../../domain/errors/peer-addressing-forbidden.error.js';
let SendAgentMessageUseCase = class SendAgentMessageUseCase {
    bus;
    settings;
    constructor(bus, settings) {
        this.bus = bus;
        this.settings = settings;
    }
    async execute(input) {
        if (input.toKind === 'peer' || !ALLOWED_AGENT_MESSAGE_TARGET_KINDS.includes(input.toKind)) {
            throw new PeerAddressingForbiddenError(input.toKind);
        }
        const flagOn = await this.isCollaborationEnabled();
        if (!flagOn) {
            return { enabled: false };
        }
        const now = new Date();
        const message = {
            id: randomUUID(),
            appId: input.appId,
            featureId: input.featureId,
            fromAgentRunId: input.fromAgentRunId,
            fromActor: input.fromActor,
            toTarget: input.toTarget,
            toKind: input.toKind,
            messageKind: input.messageKind,
            payload: typeof input.payload === 'string' ? input.payload : JSON.stringify(input.payload),
            correlationId: input.correlationId,
            deliveredAt: undefined,
            createdAt: now,
            updatedAt: now,
        };
        await this.bus.publish(message);
        return { enabled: true, message };
    }
    async isCollaborationEnabled() {
        const settings = await this.settings.load();
        return settings?.featureFlags?.collaboration === true;
    }
};
SendAgentMessageUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentMessageBus')),
    __param(1, inject('ISettingsRepository')),
    __metadata("design:paramtypes", [Object, Object])
], SendAgentMessageUseCase);
export { SendAgentMessageUseCase };
