/**
 * FeatureAgentGateQuestionPublisher
 *
 * Thin wrapper around {@link AskAgentQuestionUseCase} so the feature-agent
 * worker can emit ONE {@link AgentQuestion} of kind `blocking` whenever
 * an approval gate transitions an `AgentRun` to `waiting_approval`
 * (spec 093, task 20).
 *
 * The unified inbox (web + CLI) reads `agent_questions` rows for both
 * interactive-mode AskUserQuestion AND background-mode gate transitions
 * — this publisher is the background-mode write path. It does NOT change
 * the existing approval-gate state machine; it only emits a parallel
 * record so the inbox covers both modes.
 *
 * Scope derivation mirrors {@link FeatureAgentLifecyclePublisher}: try
 * to resolve a registered Application for the worker's repository path
 * and fall back to the repository path itself when none exists.
 *
 * Failures are swallowed: a question-emit error MUST NOT crash the
 * feature-agent worker. The lifecycle publisher's `publishBlocked` call
 * is the canonical signal for SSE clients; this publisher is additive.
 */
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 { AskAgentQuestionUseCase } from '../../../../application/use-cases/agents/ask-agent-question.use-case.js';
import { AgentQuestionAnswerer, AgentQuestionKind } from '../../../../domain/generated/output.js';
let FeatureAgentGateQuestionPublisher = class FeatureAgentGateQuestionPublisher {
    askAgentQuestion;
    applicationRepo;
    constructor(askAgentQuestion, applicationRepo) {
        this.askAgentQuestion = askAgentQuestion;
        this.applicationRepo = applicationRepo;
    }
    async publishWaitingApproval(input) {
        try {
            const appId = await this.resolveAppId(input.repositoryPath);
            const promptObject = {
                event: 'waiting_approval',
                node: input.interruptNode,
                runId: input.runId,
                featureId: input.featureId,
            };
            const result = await this.askAgentQuestion.execute({
                appId,
                featureId: input.featureId,
                agentRunId: input.runId,
                kind: AgentQuestionKind.blocking,
                prompt: JSON.stringify(promptObject),
                options: ['approve', 'reject'],
                answerer: AgentQuestionAnswerer.either,
            });
            // The blocking AskAgentQuestion registers a Deferred awaiter. The
            // background gate flow does NOT await it — the answer flows
            // through ApproveAgentRunUseCase / RejectAgentRunUseCase. Attach a
            // no-op catch so the eventual timeout/cancel never surfaces as an
            // unhandled rejection in the worker process.
            if (result.awaiter) {
                result.awaiter.catch(() => undefined);
            }
        }
        catch {
            // Bus / DB outage 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;
    }
};
FeatureAgentGateQuestionPublisher = __decorate([
    injectable(),
    __param(0, inject(AskAgentQuestionUseCase)),
    __param(1, inject('IApplicationRepository')),
    __metadata("design:paramtypes", [AskAgentQuestionUseCase, Object])
], FeatureAgentGateQuestionPublisher);
export { FeatureAgentGateQuestionPublisher };
