import { basename } from "path";
import { parseGitHubRemoteUrl } from "../utils/github-remote.js";
import { slugify } from "../utils/worktree.js";
import { deriveProjectGroupingKey, deriveProjectGroupingName } from "./workspace-registry-model.js";
export function parseGitHubRepoFromRemote(remoteUrl) {
    return parseGitHubRemoteUrl(remoteUrl)?.repo ?? null;
}
export function parseGitHubRepoNameFromRemote(remoteUrl) {
    const githubRepo = parseGitHubRepoFromRemote(remoteUrl);
    if (!githubRepo) {
        return null;
    }
    return githubRepo.split("/").pop() || null;
}
export function deriveProjectSlug(cwd, remoteUrl = null) {
    const githubRepoName = remoteUrl ? parseGitHubRepoNameFromRemote(remoteUrl) : null;
    const sourceName = githubRepoName ?? basename(cwd);
    return slugify(sourceName) || "untitled";
}
export function buildWorkspaceGitMetadataFromSnapshot(input) {
    if (!input.isGit) {
        return {
            projectKind: "directory",
            projectDisplayName: input.directoryName,
            workspaceDisplayName: input.directoryName,
            gitRemote: null,
            isWorktree: false,
            projectSlug: deriveProjectSlug(input.cwd),
            repoRoot: null,
            currentBranch: null,
            remoteUrl: null,
        };
    }
    const isWorktree = input.mainRepoRoot !== null && input.repoRoot !== null && input.mainRepoRoot !== input.repoRoot;
    const projectKey = deriveProjectGroupingKey({
        cwd: input.repoRoot ?? input.cwd,
        remoteUrl: input.remoteUrl,
        mainRepoRoot: input.mainRepoRoot,
    });
    const projectDisplayName = projectKey.startsWith("remote:")
        ? deriveProjectGroupingName(projectKey)
        : input.directoryName;
    return {
        projectKind: "git",
        projectDisplayName,
        workspaceDisplayName: input.currentBranch ?? input.directoryName,
        gitRemote: input.remoteUrl,
        isWorktree,
        projectSlug: deriveProjectSlug(input.cwd, input.remoteUrl),
        repoRoot: input.repoRoot,
        currentBranch: input.currentBranch,
        remoteUrl: input.remoteUrl,
    };
}
//# sourceMappingURL=workspace-git-metadata.js.map