import { handleAutoSetup } from './git/branch-ops.js';
export function slugify(text) {
    return text
        .toLowerCase()
        .normalize('NFD')
        .replace(/[\u0300-\u036f]/g, '')
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-+|-+$/g, '')
        .slice(0, 60);
}
export function generateSpecId(existingSpecs) {
    const maxNum = existingSpecs.reduce((max, spec) => {
        const match = /^SPEC-(\d+)$/.exec(spec.id);
        const num = match?.[1] ? parseInt(match[1], 10) : 0;
        return Math.max(max, num);
    }, 0);
    return `SPEC-${String(maxNum + 1).padStart(3, '0')}`;
}
const BRANCH_PREFIXES = {
    feature: 'feat',
    refactor: 'refactor',
    bugfix: 'fix',
    infra: 'infra',
    docs: 'docs',
    project: 'project',
};
export function generateBranchName(specId, slug, type) {
    /* v8 ignore next -- fallback only triggers with unknown spec types */
    return `${BRANCH_PREFIXES[type] ?? 'feat'}/${specId.toLowerCase()}-${slug}`;
}
/** Try auto-setup git branch. Returns result or undefined on failure. */
export async function tryAutoSetupGit(projectId, specId) {
    /* v8 ignore start -- requires real git repo with branches */
    try {
        const r = await handleAutoSetup(projectId, specId);
        if (!r.isError && r.content[0]?.type === 'text') {
            const data = JSON.parse(r.content[0].text);
            if (typeof data.newBranch !== 'string') {
                return undefined;
            }
            return { data, branch: data.newBranch };
        }
    }
    catch {
        /* git unavailable — silently continue */
    }
    return undefined;
    /* v8 ignore stop */
}
//# sourceMappingURL=create-spec-helpers.js.map