import { runShell } from "./shell.js";
export async function isGitAvailable() {
    const result = await runShell("git", ["--version"], { timeout: 5000 });
    return !result.failed;
}
export async function isGitRepo(cwd) {
    const result = await runShell("git", ["rev-parse", "--git-dir"], { cwd, timeout: 5000 });
    return !result.failed;
}
export async function getCurrentBranch(cwd) {
    const result = await runShell("git", ["branch", "--show-current"], { cwd, timeout: 5000 });
    if (result.failed)
        return null;
    return result.stdout.trim() || null;
}
export async function gitWorktreeAdd(path, branch, cwd) {
    const result = await runShell("git", ["worktree", "add", path, "-b", branch], { cwd, timeout: 15000 });
    return !result.failed;
}
export async function gitWorktreeRemove(path, cwd) {
    const result = await runShell("git", ["worktree", "remove", "--force", path], { cwd, timeout: 15000 });
    return !result.failed;
}
export async function listWorktrees(cwd) {
    const result = await runShell("git", ["worktree", "list", "--porcelain"], { cwd, timeout: 5000 });
    if (result.failed)
        return [];
    return result.stdout.split("\n").filter((l) => l.startsWith("worktree ")).map((l) => l.replace("worktree ", ""));
}
export async function removeWorktree(path, cwd) {
    await runShell("git", ["worktree", "remove", "--force", path], { cwd, timeout: 15000 });
}
export async function getGitStatus(cwd) {
    const result = await runShell("git", ["status", "--porcelain"], { cwd, timeout: 5000 });
    if (result.failed)
        return { clean: false, changes: -1 };
    const lines = result.stdout.split("\n").filter((l) => l.trim().length > 0);
    return { clean: lines.length === 0, changes: lines.length };
}
