import { resolve } from "node:path";
import { parseGitRevParsePath } from "../utils/git-rev-parse-path.js";
export function normalizeWorkspaceId(cwd) {
    const trimmed = cwd.trim();
    if (!trimmed) {
        return cwd;
    }
    return resolve(trimmed);
}
export function deriveWorkspaceId(cwd, checkout) {
    const worktreeRoot = checkout.worktreeRoot ? parseGitRevParsePath(checkout.worktreeRoot) : null;
    return 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 (mainRepoRoot) {
        return mainRepoRoot;
    }
    return options.cwd;
}
export function deriveProjectGroupingName(projectKey) {
    if (projectKey.startsWith("remote:")) {
        const remainder = projectKey.slice("remote:".length);
        const pathSegments = remainder.split("/").filter(Boolean).slice(1);
        if (pathSegments.length >= 2) {
            return pathSegments.slice(-2).join("/");
        }
        if (pathSegments.length === 1) {
            return pathSegments[0];
        }
        return 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.mainRepoRoot) {
        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.mainRepoRoot ? "worktree" : "local_checkout";
}
export function checkoutLiteFromGitSnapshot(cwd, git) {
    if (!git.isGit) {
        return {
            cwd,
            isGit: false,
            currentBranch: null,
            remoteUrl: null,
            worktreeRoot: null,
            isPaseoOwnedWorktree: false,
            mainRepoRoot: null,
        };
    }
    if (git.isPaseoOwnedWorktree && git.mainRepoRoot) {
        return {
            cwd,
            isGit: true,
            currentBranch: git.currentBranch,
            remoteUrl: git.remoteUrl,
            worktreeRoot: git.repoRoot ?? cwd,
            isPaseoOwnedWorktree: true,
            mainRepoRoot: git.mainRepoRoot,
        };
    }
    return {
        cwd,
        isGit: true,
        currentBranch: git.currentBranch,
        remoteUrl: git.remoteUrl,
        worktreeRoot: git.repoRoot ?? cwd,
        isPaseoOwnedWorktree: false,
        mainRepoRoot: git.mainRepoRoot,
    };
}
export async function detectStaleWorkspaces(input) {
    const staleWorkspaceIds = new Set();
    const existenceChecks = await Promise.all(input.activeWorkspaces.map(async (workspace) => ({
        workspace,
        exists: await input.checkDirectoryExists(workspace.cwd),
    })));
    for (const { workspace, exists } of existenceChecks) {
        if (!exists) {
            staleWorkspaceIds.add(workspace.workspaceId);
        }
    }
    return staleWorkspaceIds;
}
export function buildProjectPlacementForCwd(input) {
    const membership = classifyDirectoryForProjectMembership(input);
    return {
        projectKey: membership.projectKey,
        projectName: membership.projectName,
        checkout: membership.checkout,
    };
}
export function classifyDirectoryForProjectMembership(input) {
    const normalizedCwd = normalizeWorkspaceId(input.cwd);
    const checkout = {
        ...input.checkout,
        cwd: normalizedCwd,
    };
    const projectKey = deriveProjectGroupingKey({
        cwd: checkout.worktreeRoot ?? normalizedCwd,
        remoteUrl: checkout.remoteUrl,
        mainRepoRoot: checkout.mainRepoRoot,
    });
    return {
        cwd: normalizedCwd,
        checkout,
        workspaceId: deriveWorkspaceId(normalizedCwd, checkout),
        workspaceKind: deriveWorkspaceKind(checkout),
        workspaceDisplayName: deriveWorkspaceDisplayName({
            cwd: normalizedCwd,
            checkout,
        }),
        projectKey,
        projectName: deriveProjectGroupingName(projectKey),
        projectRootPath: deriveProjectRootPath({
            cwd: normalizedCwd,
            checkout,
        }),
        projectKind: deriveProjectKind(checkout),
    };
}
//# sourceMappingURL=workspace-registry-model.js.map