/**
 * Cleanup Feature Worktree Use Case
 *
 * Orchestrates post-merge cleanup after a feature reaches SdlcLifecycle.Maintain:
 * - Unlinks the git worktree (preserving directory contents on disk)
 * - Deletes the local feature branch
 * - Deletes the remote feature branch (if it still exists)
 *
 * All cleanup steps are non-fatal: errors are caught and logged as warnings
 * so that a cleanup failure never surfaces as a feature run failure.
 */
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 { SdlcLifecycle } from '../../../domain/generated/output.js';
let CleanupFeatureWorktreeUseCase = class CleanupFeatureWorktreeUseCase {
    featureRepo;
    worktreeService;
    gitPrService;
    logger;
    constructor(featureRepo, worktreeService, gitPrService, logger) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
        this.gitPrService = gitPrService;
        this.logger = logger;
    }
    async execute(featureId) {
        const feature = await this.featureRepo.findById(featureId);
        if (!feature)
            return;
        // Idempotency guard: skip cleanup if already in Deleting lifecycle
        // (another code path — e.g. DeleteFeatureUseCase — already handles cleanup)
        if (feature.lifecycle === SdlcLifecycle.Deleting)
            return;
        // Step 1: Unlink the git worktree (directory contents are preserved on disk)
        const worktreePath = feature.worktreePath ??
            this.worktreeService.getWorktreePath(feature.repositoryPath, feature.branch);
        try {
            await this.worktreeService.remove(feature.repositoryPath, worktreePath, true);
        }
        catch (err) {
            this.logger.warn('[CleanupFeatureWorktreeUseCase] worktree remove failed', {
                err: err instanceof Error ? err.message : String(err),
            });
            // Prune stale worktree entries so the branch is no longer considered "in use"
            try {
                await this.worktreeService.prune(feature.repositoryPath);
            }
            catch (pruneErr) {
                this.logger.warn('[CleanupFeatureWorktreeUseCase] worktree prune failed', {
                    err: pruneErr instanceof Error ? pruneErr.message : String(pruneErr),
                });
            }
        }
        // Step 2: Delete the local feature branch
        try {
            await this.gitPrService.deleteBranch(feature.repositoryPath, feature.branch);
        }
        catch (err) {
            this.logger.warn('[CleanupFeatureWorktreeUseCase] local branch delete failed', {
                err: err instanceof Error ? err.message : String(err),
            });
        }
        // Step 3: Delete the remote feature branch if it still exists
        try {
            const remoteExists = await this.worktreeService.remoteBranchExists(feature.repositoryPath, feature.branch);
            if (remoteExists) {
                await this.gitPrService.deleteBranch(feature.repositoryPath, feature.branch, true);
            }
        }
        catch (err) {
            this.logger.warn('[CleanupFeatureWorktreeUseCase] remote branch delete failed', {
                err: err instanceof Error ? err.message : String(err),
            });
        }
    }
};
CleanupFeatureWorktreeUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __param(2, inject('IGitPrService')),
    __param(3, inject('ILogger')),
    __metadata("design:paramtypes", [Object, Object, Object, Object])
], CleanupFeatureWorktreeUseCase);
export { CleanupFeatureWorktreeUseCase };
