/**
 * Approve Agent Run Use Case
 *
 * Approves a paused agent run (waiting_approval status) and
 * spawns a new resume worker to continue graph execution.
 */
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 { injectable, inject } from 'tsyringe';
import { AgentRunStatus } from '../../../domain/generated/output.js';
let ApproveAgentRunUseCase = class ApproveAgentRunUseCase {
    agentRunRepository;
    processService;
    featureRepository;
    worktreeService;
    constructor(agentRunRepository, processService, featureRepository, worktreeService) {
        this.agentRunRepository = agentRunRepository;
        this.processService = processService;
        this.featureRepository = featureRepository;
        this.worktreeService = worktreeService;
    }
    async execute(id) {
        const run = await this.agentRunRepository.findById(id);
        if (!run) {
            return { approved: false, reason: 'Agent run not found' };
        }
        if (run.status !== AgentRunStatus.waitingApproval) {
            return {
                approved: false,
                reason: `Agent run is not waiting for approval (status: ${run.status})`,
            };
        }
        // Look up the feature to get the branch for worktree path derivation
        const feature = run.featureId ? await this.featureRepository.findById(run.featureId) : null;
        const repoPath = run.repositoryPath ?? '';
        const worktreePath = feature
            ? this.worktreeService.getWorktreePath(repoPath, feature.branch)
            : repoPath;
        const now = new Date();
        await this.agentRunRepository.updateStatus(id, AgentRunStatus.running, {
            approvalStatus: 'approved',
            updatedAt: now,
        });
        this.processService.spawn(run.featureId ?? '', id, repoPath, worktreePath, // specDir = worktree path (same as initial spawn)
        worktreePath, { resume: true, approvalMode: run.approvalMode });
        return { approved: true, reason: 'Approved and resumed' };
    }
};
ApproveAgentRunUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentRunRepository')),
    __param(1, inject('IFeatureAgentProcessService')),
    __param(2, inject('IFeatureRepository')),
    __param(3, inject('IWorktreeService')),
    __metadata("design:paramtypes", [Object, Object, Object, Object])
], ApproveAgentRunUseCase);
export { ApproveAgentRunUseCase };
