/**
 * Get Branch Sync Status Use Case
 *
 * Returns how many commits a feature branch is ahead/behind the base branch.
 *
 * Flow: resolve feature → determine cwd (worktree or repo root) →
 * sync remote tracking ref → get branch sync status.
 */
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';
let GetBranchSyncStatusUseCase = class GetBranchSyncStatusUseCase {
    featureRepo;
    gitPrService;
    worktreeService;
    constructor(featureRepo, gitPrService, worktreeService) {
        this.featureRepo = featureRepo;
        this.gitPrService = gitPrService;
        this.worktreeService = worktreeService;
    }
    async execute(featureId) {
        const feature = (await this.featureRepo.findById(featureId)) ??
            (await this.featureRepo.findByIdPrefix(featureId));
        if (!feature) {
            throw new Error(`Feature not found: "${featureId}"`);
        }
        if (!feature.branch) {
            throw new Error(`Feature "${featureId}" has no branch`);
        }
        const cwd = await this.resolveCwd(feature.repositoryPath, feature.branch);
        const hasRemote = await this.gitPrService.hasRemote(cwd);
        if (!hasRemote) {
            throw new Error('Repository has no remote — branch sync is not available');
        }
        const baseBranch = await this.gitPrService.getDefaultBranch(feature.repositoryPath);
        // Sync the remote tracking ref so ahead/behind counts are current
        await this.gitPrService.syncMain(cwd, baseBranch);
        const { ahead, behind } = await this.gitPrService.getBranchSyncStatus(cwd, feature.branch, baseBranch);
        return { ahead, behind, baseBranch };
    }
    async resolveCwd(repositoryPath, branch) {
        const hasWorktree = await this.worktreeService.exists(repositoryPath, branch);
        if (hasWorktree) {
            return this.worktreeService.getWorktreePath(repositoryPath, branch);
        }
        return repositoryPath;
    }
};
GetBranchSyncStatusUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IGitPrService')),
    __param(2, inject('IWorktreeService')),
    __metadata("design:paramtypes", [Object, Object, Object])
], GetBranchSyncStatusUseCase);
export { GetBranchSyncStatusUseCase };
