
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="c88f5612-1a66-5562-95f4-d5965c102d56")}catch(e){}}();
import { spawnSync } from "node:child_process";
const WINDOWS_DIRECTORY_PICKER_SCRIPT = [
    "$ErrorActionPreference = 'Stop'",
    "Add-Type -AssemblyName System.Windows.Forms",
    "$dialog = New-Object System.Windows.Forms.FolderBrowserDialog",
    "$dialog.Description = 'Select a project folder'",
    "$dialog.ShowNewFolderButton = $false",
    "if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { [Console]::Out.Write($dialog.SelectedPath) }",
].join("; ");
function parseChildProcessErrorCode(error) {
    if (!error || typeof error !== "object" || !("code" in error)) {
        return null;
    }
    const code = error.code;
    return typeof code === "string" ? code : null;
}
function defaultRunCommand(command, args) {
    return spawnSync(command, args, {
        encoding: "utf8",
        stdio: ["ignore", "pipe", "pipe"],
    });
}
function runDirectoryPickerCommand(candidate, runCommand) {
    const result = runCommand(candidate.command, candidate.args);
    const errorCode = parseChildProcessErrorCode(result.error);
    if (errorCode === "ENOENT") {
        return { kind: "unavailable" };
    }
    if (result.error) {
        const message = result.error.message || String(result.error);
        throw new Error(`Could not open directory picker via ${candidate.command}: ${message}`);
    }
    if (result.signal) {
        throw new Error(`Directory picker command ${candidate.command} terminated by signal: ${result.signal}`);
    }
    if (result.status !== 0) {
        const stderr = typeof result.stderr === "string" ? result.stderr.trim() : "";
        if (stderr) {
            throw new Error(`Could not open directory picker via ${candidate.command}: ${stderr}`);
        }
        return { kind: "cancelled" };
    }
    const selectedPath = typeof result.stdout === "string" ? result.stdout.trim() : "";
    if (!selectedPath) {
        return { kind: "cancelled" };
    }
    return { kind: "selected", path: selectedPath };
}
export function pickDirectoryPathFromSystemDialog(options = {}) {
    const platform = options.platform ?? process.platform;
    const cwd = options.cwd ?? process.cwd();
    const runCommand = options.runCommand ?? defaultRunCommand;
    if (platform === "darwin") {
        const result = runDirectoryPickerCommand({
            command: "osascript",
            args: ["-e", 'POSIX path of (choose folder with prompt "Select a project folder")'],
        }, runCommand);
        if (result.kind === "selected") {
            return result.path;
        }
        if (result.kind === "cancelled") {
            return null;
        }
        throw new Error('Could not open directory picker. Command "osascript" is not available.');
    }
    if (platform === "linux") {
        const candidates = [
            {
                command: "zenity",
                args: ["--file-selection", "--directory", "--title=Select project folder"],
            },
            {
                command: "kdialog",
                args: ["--getexistingdirectory", cwd, "Select project folder"],
            },
        ];
        for (const candidate of candidates) {
            const result = runDirectoryPickerCommand(candidate, runCommand);
            if (result.kind === "unavailable") {
                continue;
            }
            if (result.kind === "selected") {
                return result.path;
            }
            return null;
        }
        throw new Error('Could not open directory picker. Install "zenity" or "kdialog" and try again.');
    }
    if (platform === "win32") {
        const candidates = [
            {
                command: "powershell",
                args: ["-NoProfile", "-STA", "-Command", WINDOWS_DIRECTORY_PICKER_SCRIPT],
            },
            {
                command: "pwsh",
                args: ["-NoProfile", "-STA", "-Command", WINDOWS_DIRECTORY_PICKER_SCRIPT],
            },
        ];
        for (const candidate of candidates) {
            const result = runDirectoryPickerCommand(candidate, runCommand);
            if (result.kind === "unavailable") {
                continue;
            }
            if (result.kind === "selected") {
                return result.path;
            }
            return null;
        }
        throw new Error('Could not open directory picker. Install PowerShell ("powershell" or "pwsh") and try again.');
    }
    return null;
}
//# sourceMappingURL=directory-picker.js.map
//# debugId=c88f5612-1a66-5562-95f4-d5965c102d56
