/**
 * FeatureAgentLifecyclePublisher
 *
 * Thin wrapper around {@link SendAgentMessageUseCase} so the feature-agent
 * worker can broadcast lifecycle messages (started, phase-changed, blocked,
 * completed) on the agent message bus without touching bus internals or
 * the feature-flag check (the use case owns both).
 *
 * Scope derivation: messages are scoped by `appId`. We try to resolve a
 * registered {@link Application} for the worker's repository path; if none
 * exists we use the repository path itself as the stable scope key. This
 * keeps cross-worktree messages consistent without requiring the Feature
 * model to know about Applications.
 *
 * Failures are swallowed: the lifecycle publisher must NOT crash a feature
 * agent if the bus is unavailable. The worker logs each lifecycle event
 * separately for crash diagnosis.
 */
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 { SendAgentMessageUseCase } from '../../../../application/use-cases/agents/send-agent-message.use-case.js';
import { AgentMessageKind } from '../../../../domain/generated/output.js';
let FeatureAgentLifecyclePublisher = class FeatureAgentLifecyclePublisher {
    sendAgentMessage;
    applicationRepo;
    constructor(sendAgentMessage, applicationRepo) {
        this.sendAgentMessage = sendAgentMessage;
        this.applicationRepo = applicationRepo;
    }
    async publishStarted(input) {
        await this.publish(input, AgentMessageKind.status, { event: 'started' });
    }
    async publishPhaseChanged(input) {
        await this.publish(input, AgentMessageKind.status, {
            event: 'phase-changed',
            phase: input.phase,
        });
    }
    async publishBlocked(input) {
        await this.publish(input, AgentMessageKind.blocked, {
            event: 'blocked',
            reason: input.reason,
        });
    }
    async publishCompleted(input) {
        await this.publish(input, AgentMessageKind.status, { event: 'completed' });
    }
    async publish(input, messageKind, payload) {
        try {
            const appId = await this.resolveAppId(input.repositoryPath);
            await this.sendAgentMessage.execute({
                appId,
                featureId: input.featureId,
                fromActor: `agent:${input.runId}`,
                fromAgentRunId: input.runId,
                toTarget: 'broadcast',
                toKind: 'broadcast',
                messageKind,
                payload,
            });
        }
        catch {
            // Swallow — bus outages must not crash the feature-agent worker.
        }
    }
    async resolveAppId(repositoryPath) {
        try {
            const app = await this.applicationRepo.findByPath(repositoryPath);
            if (app?.id)
                return app.id;
        }
        catch {
            // Fall through to path-based scope.
        }
        return repositoryPath;
    }
};
FeatureAgentLifecyclePublisher = __decorate([
    injectable(),
    __param(0, inject(SendAgentMessageUseCase)),
    __param(1, inject('IApplicationRepository')),
    __metadata("design:paramtypes", [SendAgentMessageUseCase, Object])
], FeatureAgentLifecyclePublisher);
export { FeatureAgentLifecyclePublisher };
