/**
 * SlugResolver
 *
 * Resolves unique slugs by checking against database and git branches.
 * Ensures no slug collision by appending numeric suffixes (-2, -3, etc.)
 */
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';
/** Maximum number of suffix attempts before giving up */
const MAX_SUFFIX = 10;
let SlugResolver = class SlugResolver {
    featureRepo;
    worktreeService;
    constructor(featureRepo, worktreeService) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
    }
    /**
     * Find a unique slug by checking both the feature DB and git branches.
     * If the original slug conflicts, appends -2, -3, etc. until a free one is found.
     */
    async resolveUniqueSlug(originalSlug, repositoryPath) {
        for (let suffix = 0; suffix <= MAX_SUFFIX; suffix++) {
            const slug = suffix === 0 ? originalSlug : `${originalSlug}-${suffix + 1}`;
            const branch = `feat/${slug}`;
            // Check if a feature with this slug already exists in the DB
            const existing = await this.featureRepo.findBySlug(slug, repositoryPath);
            if (existing) {
                if (suffix === 0)
                    continue; // try suffixed version
                continue;
            }
            // Check if the git branch already exists (leftover from deleted feature)
            const branchInUse = await this.worktreeService.exists(repositoryPath, branch);
            if (branchInUse)
                continue;
            if (suffix > 0) {
                return {
                    slug,
                    branch,
                    warning: `Branch "feat/${originalSlug}" already exists, using "${branch}" instead`,
                };
            }
            return { slug, branch };
        }
        throw new Error(`Could not find a unique slug for "${originalSlug}" after ${MAX_SUFFIX} attempts`);
    }
};
SlugResolver = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __metadata("design:paramtypes", [Object, Object])
], SlugResolver);
export { SlugResolver };
