import { join } from "node:path";
import { getPaseoWorktreesRoot, isPaseoOwnedWorktreeCwd } from "../../utils/worktree.js";
import { archivePaseoWorktree, } from "../paseo-worktree-archive-service.js";
import { toWorktreeWireError } from "../worktree-errors.js";
export async function listPaseoWorktreesCommand(dependencies, input) {
    if (input.reason) {
        return dependencies.workspaceGitService.listWorktrees(input.cwd, { reason: input.reason });
    }
    return dependencies.workspaceGitService.listWorktrees(input.cwd);
}
export async function createPaseoWorktreeCommand(dependencies, input) {
    try {
        if (!dependencies.createPaseoWorktreeWorkflow) {
            throw new Error("Paseo worktree service is not configured");
        }
        const createdWorktree = await dependencies.createPaseoWorktreeWorkflow({
            ...input,
            runSetup: false,
            paseoHome: input.paseoHome ?? dependencies.paseoHome,
            worktreesRoot: input.worktreesRoot ?? dependencies.worktreesRoot,
        });
        return { ok: true, createdWorktree };
    }
    catch (error) {
        return {
            ok: false,
            error: toWorktreeWireError(error),
            cause: error,
        };
    }
}
export async function archivePaseoWorktreeCommand(dependencies, input) {
    const resolvedTarget = await resolveArchiveTarget(dependencies, input);
    const ownership = await isPaseoOwnedWorktreeCwd(resolvedTarget.targetPath, {
        paseoHome: dependencies.paseoHome,
        worktreesRoot: dependencies.worktreesRoot,
    });
    if (!ownership.allowed) {
        return {
            ok: false,
            code: "NOT_ALLOWED",
            message: "Worktree is not a Paseo-owned worktree",
            removedAgents: [],
        };
    }
    const repoRoot = ownership.repoRoot ?? resolvedTarget.repoRoot ?? null;
    const removedAgents = await archivePaseoWorktree(dependencies, {
        targetPath: resolvedTarget.targetPath,
        repoRoot,
        worktreesRoot: ownership.worktreeRoot,
        worktreesBaseRoot: dependencies.worktreesRoot,
        requestId: input.requestId,
    });
    return {
        ok: true,
        removedAgents,
    };
}
async function resolveArchiveTarget(dependencies, input) {
    const repoRoot = input.repoRoot ?? null;
    if (input.worktreePath) {
        return { targetPath: input.worktreePath, repoRoot };
    }
    if (input.worktreeSlug) {
        if (!repoRoot) {
            throw new Error("repoRoot is required when worktreeSlug is supplied");
        }
        return {
            targetPath: await resolveWorktreeSlugPath(dependencies, repoRoot, input.worktreeSlug),
            repoRoot,
        };
    }
    if (repoRoot && input.branchName) {
        const worktrees = await dependencies.workspaceGitService.listWorktrees(repoRoot);
        const match = worktrees.find((entry) => entry.branchName === input.branchName);
        if (!match) {
            throw new Error(`Paseo worktree not found for branch ${input.branchName}`);
        }
        return { targetPath: match.path, repoRoot };
    }
    throw new Error("worktreePath, worktreeSlug, or repoRoot+branchName is required");
}
async function resolveWorktreeSlugPath(dependencies, repoRoot, worktreeSlug) {
    const worktreesRoot = await getPaseoWorktreesRoot(repoRoot, dependencies.paseoHome, dependencies.worktreesRoot);
    return join(worktreesRoot, worktreeSlug);
}
//# sourceMappingURL=commands.js.map