/**
 * Delete Repository Use Case
 *
 * Deletes a Repository and all its child features.
 * Each feature is properly cleaned up (agent runs cancelled, worktrees removed)
 * via DeleteFeatureUseCase before the repository is soft-deleted.
 *
 * Optionally removes the repository directory from disk when
 * {@link DeleteRepositoryOptions.deleteFromDisk} is true. The DB soft-delete
 * always runs first so the repository disappears from queries even if the
 * filesystem removal fails.
 */
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 { DeleteFeatureUseCase } from '../features/delete-feature.use-case.js';
let DeleteRepositoryUseCase = class DeleteRepositoryUseCase {
    repositoryRepo;
    featureRepo;
    deleteFeature;
    fileSystem;
    constructor(repositoryRepo, featureRepo, deleteFeature, fileSystem) {
        this.repositoryRepo = repositoryRepo;
        this.featureRepo = featureRepo;
        this.deleteFeature = deleteFeature;
        this.fileSystem = fileSystem;
    }
    async execute(id, options) {
        const repository = await this.repositoryRepo.findById(id);
        if (!repository) {
            throw new Error(`Repository not found: "${id}"`);
        }
        // Delete all child features (cancels agent runs, removes worktrees).
        // Include archived features so they don't survive as orphans.
        const features = await this.featureRepo.list({
            repositoryPath: repository.path,
            includeArchived: true,
        });
        for (const feature of features) {
            await this.deleteFeature.execute(feature.id);
        }
        await this.repositoryRepo.softDelete(id);
        if (options?.deleteFromDisk) {
            await this.fileSystem.removeDirectory(repository.path);
        }
    }
};
DeleteRepositoryUseCase = __decorate([
    injectable(),
    __param(0, inject('IRepositoryRepository')),
    __param(1, inject('IFeatureRepository')),
    __param(2, inject(DeleteFeatureUseCase)),
    __param(3, inject('IFileSystemService')),
    __metadata("design:paramtypes", [Object, Object, DeleteFeatureUseCase, Object])
], DeleteRepositoryUseCase);
export { DeleteRepositoryUseCase };
