import { createNameId } from "mnemonic-id";
import { createWorktree, resolveExistingWorktreeForSlug, slugify, validateBranchSlug, } from "../utils/worktree.js";
import { resolveWorktreeCreationIntent, } from "./resolve-worktree-creation-intent.js";
export async function createWorktreeCore(input, deps) {
    const repoRoot = await resolveWorktreeRepoRoot(input, deps.workspaceGitService);
    const requestedWorktreeSlug = input.worktreeSlug
        ? normalizeWorktreeSlug(input.worktreeSlug)
        : undefined;
    let intentInput;
    if (input.action === "checkout") {
        intentInput = {
            action: "checkout",
            refName: input.refName,
            githubPrNumber: input.githubPrNumber,
            worktreeSlug: requestedWorktreeSlug,
        };
    }
    else if (input.githubPrNumber !== undefined) {
        intentInput = {
            githubPrNumber: input.githubPrNumber,
            refName: input.refName,
            worktreeSlug: requestedWorktreeSlug,
        };
    }
    else {
        const worktreeSlug = requestedWorktreeSlug ?? normalizeWorktreeSlug(createNameId());
        intentInput = {
            action: "branch-off",
            refName: input.refName,
            worktreeSlug,
        };
    }
    const intent = await resolveWorktreeCreationIntent(intentInput, repoRoot, {
        ...deps,
        resolveDefaultBranch: (root) => resolveDefaultBranch(root, deps),
    });
    let normalizedSlug;
    switch (intent.kind) {
        case "branch-off": {
            normalizedSlug = intent.branchName;
            break;
        }
        case "checkout-branch": {
            normalizedSlug = requestedWorktreeSlug ?? normalizeWorktreeSlug(intent.branchName);
            break;
        }
        case "checkout-github-pr": {
            normalizedSlug =
                requestedWorktreeSlug ?? normalizeWorktreeSlug(intent.localBranchName ?? intent.headRef);
            break;
        }
    }
    const existingWorktree = await resolveExistingWorktreeForSlug({
        slug: normalizedSlug,
        repoRoot,
        paseoHome: input.paseoHome,
        worktreesRoot: input.worktreesRoot,
    });
    if (existingWorktree) {
        return { worktree: existingWorktree, intent, repoRoot, created: false };
    }
    return {
        worktree: await createWorktree({
            cwd: repoRoot,
            worktreeSlug: normalizedSlug,
            source: intent,
            runSetup: input.runSetup ?? true,
            paseoHome: input.paseoHome,
            worktreesRoot: input.worktreesRoot,
        }),
        intent,
        repoRoot,
        created: true,
    };
}
async function resolveDefaultBranch(repoRoot, deps) {
    const baseBranch = deps.resolveDefaultBranch
        ? await deps.resolveDefaultBranch(repoRoot)
        : await deps.workspaceGitService?.resolveDefaultBranch(repoRoot);
    if (!baseBranch) {
        throw new Error("Unable to resolve repository default branch");
    }
    return baseBranch;
}
export async function resolveWorktreeRepoRoot(input, workspaceGitService) {
    if (!workspaceGitService) {
        throw new Error("Create worktree requires WorkspaceGitService");
    }
    return workspaceGitService.resolveRepoRoot(input.cwd);
}
function validateWorktreeSlug(slug) {
    const validation = validateBranchSlug(slug);
    if (!validation.valid) {
        throw new Error(`Invalid worktree name: ${validation.error}`);
    }
    return slug;
}
function normalizeWorktreeSlug(value) {
    return validateWorktreeSlug(slugify(value));
}
//# sourceMappingURL=worktree-core.js.map