/**
 * Delete Feature Use Case
 *
 * Orchestrates feature deletion including:
 * - Cancelling any running/pending agent runs
 * - Removing the git worktree
 * - Deleting the feature record
 *
 * Business Rules:
 * - Throws if the feature does not exist
 * - Cancels running/pending agent runs before deletion
 * - Gracefully handles worktree removal failures
 */
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 DeleteFeatureUseCase = class DeleteFeatureUseCase {
    featureRepo;
    worktreeService;
    processService;
    runRepo;
    constructor(featureRepo, worktreeService, processService, runRepo) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
        this.processService = processService;
        this.runRepo = runRepo;
    }
    async execute(featureId) {
        // 1. Find feature (exact or prefix match)
        const feature = (await this.featureRepo.findById(featureId)) ??
            (await this.featureRepo.findByIdPrefix(featureId));
        if (!feature) {
            throw new Error(`Feature not found: "${featureId}"`);
        }
        // 2. Cancel running/pending agent run if present
        if (feature.agentRunId) {
            const run = await this.runRepo.findById(feature.agentRunId);
            if (run && (run.status === AgentRunStatus.running || run.status === AgentRunStatus.pending)) {
                // Kill the OS process if still alive
                if (run.pid && this.processService.isAlive(run.pid)) {
                    try {
                        process.kill(run.pid);
                    }
                    catch {
                        // Process may have already exited
                    }
                }
                await this.runRepo.updateStatus(run.id, AgentRunStatus.cancelled);
            }
        }
        // 3. Remove worktree (ignore errors if already removed)
        const worktreePath = this.worktreeService.getWorktreePath(feature.repositoryPath, feature.branch);
        try {
            await this.worktreeService.remove(worktreePath);
        }
        catch {
            // Worktree might already be removed - that's fine
        }
        // 4. Delete feature record
        await this.featureRepo.delete(feature.id);
        return feature;
    }
};
DeleteFeatureUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __param(2, inject('IFeatureAgentProcessService')),
    __param(3, inject('IAgentRunRepository')),
    __metadata("design:paramtypes", [Object, Object, Object, Object])
], DeleteFeatureUseCase);
export { DeleteFeatureUseCase };
