import { promises as fs } from "node:fs";
import { homedir } from "node:os";
import path from "node:path";
const WINDOWS_DRIVE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
/**
 * Lists immediate child directories of an absolute path anywhere on the host
 * filesystem. Unlike the workspace-scoped file explorer, this intentionally
 * allows browsing outside any project root so users can navigate to and open a
 * project from anywhere on the machine (including other drives on Windows).
 */
export async function browseHostDirectory(params = {}) {
    const includeHidden = params.includeHidden ?? false;
    const requested = params.path?.trim();
    if (!requested) {
        return listDefaultLocation(includeHidden);
    }
    const absolutePath = path.resolve(expandHome(requested));
    return listDirectory(absolutePath, includeHidden);
}
async function listDefaultLocation(includeHidden) {
    if (process.platform === "win32") {
        return listWindowsDrives();
    }
    return listDirectory(homedir(), includeHidden);
}
async function listWindowsDrives() {
    const drives = await Promise.all(WINDOWS_DRIVE_LETTERS.map(async (letter) => {
        const root = `${letter}:\\`;
        try {
            await fs.access(root);
            return root;
        }
        catch {
            return null;
        }
    }));
    const entries = drives
        .filter((drive) => drive !== null)
        .map((drive) => ({
        name: drive,
        path: drive,
        kind: "drive",
    }));
    return {
        path: null,
        parent: null,
        entries,
    };
}
async function listDirectory(absolutePath, includeHidden) {
    const stats = await fs.stat(absolutePath);
    if (!stats.isDirectory()) {
        throw new Error("Requested path is not a directory");
    }
    const dirents = await fs.readdir(absolutePath, { withFileTypes: true });
    const resolvedEntries = await Promise.all(dirents.map(async (dirent) => {
        if (!includeHidden && dirent.name.startsWith(".")) {
            return null;
        }
        const childPath = path.join(absolutePath, dirent.name);
        const isDirectory = await resolveIsDirectory(dirent, childPath);
        if (!isDirectory) {
            return null;
        }
        return {
            name: dirent.name,
            path: childPath,
            kind: "directory",
        };
    }));
    const entries = resolvedEntries.filter((entry) => entry !== null);
    entries.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }));
    return {
        path: absolutePath,
        parent: computeParent(absolutePath),
        entries,
    };
}
async function resolveIsDirectory(dirent, childPath) {
    if (dirent.isDirectory()) {
        return true;
    }
    if (!dirent.isSymbolicLink()) {
        return false;
    }
    try {
        const stats = await fs.stat(childPath);
        return stats.isDirectory();
    }
    catch {
        return false;
    }
}
function computeParent(absolutePath) {
    const parent = path.dirname(absolutePath);
    if (parent === absolutePath) {
        // Already at a filesystem root (e.g. "/" or "C:\\").
        // On Windows we surface the drive list as the parent via an empty path.
        return process.platform === "win32" ? "" : null;
    }
    return parent;
}
function expandHome(value) {
    if (value === "~" || value.startsWith("~/") || value.startsWith("~\\")) {
        return path.join(homedir(), value.slice(1));
    }
    return value;
}
//# sourceMappingURL=service.js.map