import { resolve } from "node:path";
import { getCheckoutStatusLite } from "../utils/checkout-git.js";
export function normalizeWorkspaceId(cwd) {
    const trimmed = cwd.trim();
    if (!trimmed) {
        return cwd;
    }
    return resolve(trimmed);
}
export function deriveWorkspaceId(cwd, checkout) {
    if (checkout.isSeaworkOwnedWorktree && checkout.worktreeRoot) {
        return checkout.worktreeRoot;
    }
    return normalizeWorkspaceId(cwd);
}
export function deriveProjectGroupingKey(options) {
    const mainRepoRoot = options.mainRepoRoot?.trim();
    if (options.isSeaworkOwnedWorktree && mainRepoRoot) {
        return mainRepoRoot;
    }
    return options.cwd;
}
export function deriveProjectGroupingName(projectKey) {
    const githubRemotePrefix = "remote:github.com/";
    if (projectKey.startsWith(githubRemotePrefix)) {
        return projectKey.slice(githubRemotePrefix.length) || projectKey;
    }
    const segments = projectKey.split(/[\\/]/).filter(Boolean);
    return segments[segments.length - 1] || projectKey;
}
function deriveWorkspaceDirectoryName(cwd) {
    const normalized = cwd.replace(/\\/g, "/");
    const segments = normalized.split("/").filter(Boolean);
    return segments[segments.length - 1] ?? cwd;
}
export function deriveWorkspaceDisplayName(input) {
    if (isGitSubdirectory(input.checkout)) {
        return deriveWorkspaceDirectoryName(input.cwd);
    }
    const branch = input.checkout.currentBranch?.trim() ?? null;
    if (branch && branch.toUpperCase() !== "HEAD") {
        return branch;
    }
    return deriveWorkspaceDirectoryName(input.cwd);
}
export function deriveProjectRootPath(input) {
    if (input.checkout.isGit && input.checkout.isSeaworkOwnedWorktree) {
        return input.checkout.mainRepoRoot;
    }
    return input.cwd;
}
function isGitSubdirectory(checkout) {
    if (!checkout.isGit || checkout.isSeaworkOwnedWorktree) {
        return false;
    }
    if (!checkout.worktreeRoot) {
        return false;
    }
    return normalizeWorkspaceId(checkout.cwd) !== normalizeWorkspaceId(checkout.worktreeRoot);
}
export function deriveProjectKind(checkout) {
    if (!checkout.isGit || isGitSubdirectory(checkout)) {
        return "non_git";
    }
    return "git";
}
export function deriveWorkspaceKind(checkout) {
    if (!checkout.isGit || isGitSubdirectory(checkout)) {
        return "directory";
    }
    return checkout.isSeaworkOwnedWorktree ? "worktree" : "local_checkout";
}
export async function detectStaleWorkspaces(input) {
    const staleWorkspaceIds = new Set();
    for (const workspace of input.activeWorkspaces) {
        const dirExists = await input.checkDirectoryExists(workspace.cwd);
        if (!dirExists) {
            staleWorkspaceIds.add(workspace.workspaceId);
        }
    }
    return staleWorkspaceIds;
}
export async function buildProjectPlacementForCwd(input) {
    const normalizedCwd = normalizeWorkspaceId(input.cwd);
    const checkout = await getCheckoutStatusLite(normalizedCwd, { seaworkHome: input.seaworkHome })
        .then((status) => {
        if (!status.isGit) {
            return {
                cwd: normalizedCwd,
                isGit: false,
                currentBranch: null,
                remoteUrl: null,
                worktreeRoot: null,
                isSeaworkOwnedWorktree: false,
                mainRepoRoot: null,
            };
        }
        if (status.isSeaworkOwnedWorktree && status.mainRepoRoot) {
            return {
                cwd: normalizedCwd,
                isGit: true,
                currentBranch: status.currentBranch,
                remoteUrl: status.remoteUrl,
                worktreeRoot: status.worktreeRoot,
                isSeaworkOwnedWorktree: true,
                mainRepoRoot: status.mainRepoRoot,
            };
        }
        return {
            cwd: normalizedCwd,
            isGit: true,
            currentBranch: status.currentBranch,
            remoteUrl: status.remoteUrl,
            worktreeRoot: status.worktreeRoot,
            isSeaworkOwnedWorktree: false,
            mainRepoRoot: null,
        };
    })
        .catch(() => ({
        cwd: normalizedCwd,
        isGit: false,
        currentBranch: null,
        remoteUrl: null,
        worktreeRoot: null,
        isSeaworkOwnedWorktree: false,
        mainRepoRoot: null,
    }));
    const projectKey = deriveProjectGroupingKey({
        cwd: normalizedCwd,
        remoteUrl: checkout.remoteUrl,
        isSeaworkOwnedWorktree: checkout.isSeaworkOwnedWorktree,
        mainRepoRoot: checkout.mainRepoRoot,
    });
    return {
        projectKey,
        projectName: deriveProjectGroupingName(projectKey),
        checkout,
    };
}
//# sourceMappingURL=workspace-registry-model.js.map