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) {
    return checkout.worktreeRoot ?? normalizeWorkspaceId(cwd);
}
function deriveRemoteProjectKey(remoteUrl) {
    if (!remoteUrl) {
        return null;
    }
    const trimmed = remoteUrl.trim();
    if (!trimmed) {
        return null;
    }
    let host = null;
    let remotePath = null;
    const scpLike = trimmed.match(/^[^@]+@([^:]+):(.+)$/);
    if (scpLike) {
        host = scpLike[1] ?? null;
        remotePath = scpLike[2] ?? null;
    }
    else if (trimmed.includes("://")) {
        try {
            const parsed = new URL(trimmed);
            host = parsed.hostname || null;
            remotePath = parsed.pathname ? parsed.pathname.replace(/^\/+/, "") : null;
        }
        catch {
            return null;
        }
    }
    if (!host || !remotePath) {
        return null;
    }
    let cleanedPath = remotePath.trim().replace(/^\/+/, "").replace(/\/+$/, "");
    if (cleanedPath.endsWith(".git")) {
        cleanedPath = cleanedPath.slice(0, -4);
    }
    if (!cleanedPath.includes("/")) {
        return null;
    }
    const cleanedHost = host.toLowerCase();
    if (cleanedHost === "github.com") {
        return `remote:github.com/${cleanedPath}`;
    }
    return `remote:${cleanedHost}/${cleanedPath}`;
}
export function deriveProjectGroupingKey(options) {
    const remoteKey = deriveRemoteProjectKey(options.remoteUrl);
    if (remoteKey) {
        return remoteKey;
    }
    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) {
    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;
}
export function deriveProjectKind(checkout) {
    return checkout.isGit ? "git" : "non_git";
}
export function deriveWorkspaceKind(checkout) {
    if (!checkout.isGit) {
        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: checkout.worktreeRoot ?? normalizedCwd,
        remoteUrl: checkout.remoteUrl,
        isSeaworkOwnedWorktree: checkout.isSeaworkOwnedWorktree,
        mainRepoRoot: checkout.mainRepoRoot,
    });
    return {
        projectKey,
        projectName: deriveProjectGroupingName(projectKey),
        checkout,
    };
}
//# sourceMappingURL=workspace-registry-model.js.map