/**
 * CancelAgentQuestionUseCase
 *
 * Marks a pending {@link AgentQuestion} as `cancelled`. Rejects the
 * matching {@link IDeferredQuestionRegistry} entry so any in-process
 * awaiter (e.g. an SDK V2 `canUseTool` callback) fails-fast with
 * {@link AgentQuestionCancelledError} instead of waiting for an answer
 * that will never come.
 *
 * Feature-flag short-circuit: with `featureFlags.collaboration` off, the
 * use case is a no-op (NFR-14 byte-identical default).
 */
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 { AgentQuestionStatus } from '../../../domain/generated/output.js';
let CancelAgentQuestionUseCase = class CancelAgentQuestionUseCase {
    questionRepository;
    deferredRegistry;
    settings;
    constructor(questionRepository, deferredRegistry, settings) {
        this.questionRepository = questionRepository;
        this.deferredRegistry = deferredRegistry;
        this.settings = settings;
    }
    async execute(input) {
        const flagOn = await this.isCollaborationEnabled();
        if (!flagOn)
            return { enabled: false };
        const existing = await this.questionRepository.findById(input.appId, input.questionId);
        if (!existing)
            return { enabled: true };
        if (existing.status !== AgentQuestionStatus.pending) {
            return { enabled: true, question: existing };
        }
        const now = new Date();
        await this.questionRepository.updateStatus(input.appId, input.questionId, AgentQuestionStatus.cancelled, {
            answer: input.reason,
            answeredBy: input.cancelledBy,
            answeredAt: now,
        });
        if (this.deferredRegistry.has(input.questionId)) {
            this.deferredRegistry.reject(input.questionId, input.reason);
        }
        const updated = await this.questionRepository.findById(input.appId, input.questionId);
        return { enabled: true, question: updated ?? existing };
    }
    async isCollaborationEnabled() {
        const settings = await this.settings.load();
        return settings?.featureFlags?.collaboration === true;
    }
};
CancelAgentQuestionUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentQuestionRepository')),
    __param(1, inject('IDeferredQuestionRegistry')),
    __param(2, inject('ISettingsRepository')),
    __metadata("design:paramtypes", [Object, Object, Object])
], CancelAgentQuestionUseCase);
export { CancelAgentQuestionUseCase };
