/**
 * SlugResolver
 *
 * Resolves unique slugs by checking against database and git branches.
 * On collision, appends a short random suffix to guarantee uniqueness
 * without arbitrary iteration limits.
 */
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 { randomBytes } from 'node:crypto';
import { injectable, inject } from 'tsyringe';
let SlugResolver = class SlugResolver {
    featureRepo;
    worktreeService;
    constructor(featureRepo, worktreeService) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
    }
    /**
     * Find a unique slug by checking the feature DB, local branches, and remote branches.
     * If the original slug conflicts, appends a short random hex suffix (e.g., -a3f1).
     */
    async resolveUniqueSlug(originalSlug, repositoryPath) {
        // First try the original slug as-is
        if (await this.isSlugAvailable(originalSlug, repositoryPath)) {
            return { slug: originalSlug, branch: `feat/${originalSlug}` };
        }
        // Collision — generate a random suffix
        const suffix = randomBytes(3).toString('hex'); // 6 hex chars, e.g. "a3f1b2"
        const slug = `${originalSlug}-${suffix}`;
        const branch = `feat/${slug}`;
        return {
            slug,
            branch,
            warning: `Branch "feat/${originalSlug}" already exists, using "${branch}" instead`,
        };
    }
    async isSlugAvailable(slug, repositoryPath) {
        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)
            return false;
        // Check if the git branch already exists (worktree, local, or remote)
        const branchInUse = await this.worktreeService.exists(repositoryPath, branch);
        if (branchInUse)
            return false;
        const branchExists = await this.worktreeService.branchExists(repositoryPath, branch);
        if (branchExists)
            return false;
        const remoteBranchExists = await this.worktreeService.remoteBranchExists(repositoryPath, branch);
        if (remoteBranchExists)
            return false;
        return true;
    }
};
SlugResolver = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __metadata("design:paramtypes", [Object, Object])
], SlugResolver);
export { SlugResolver };
