import { execSync } from 'node:child_process';
const defaultDeps = {
    platform: process.platform,
    exec: (cmd, opts) => execSync(cmd, opts),
};
/**
 * Platform-specific commands that open a native OS folder picker dialog.
 * Each returns the chosen absolute folder path on stdout, or exits non-zero / returns empty on cancel.
 */
const PLATFORM_COMMANDS = {
    darwin: `osascript -e 'POSIX path of (choose folder with prompt "Select a repository folder")'`,
    linux: `zenity --file-selection --directory --title="Select a repository folder" 2>/dev/null`,
    win32: `powershell -NoProfile -Command "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.FolderBrowserDialog; $f.Description = 'Select a repository folder'; if ($f.ShowDialog() -eq 'OK') { $f.SelectedPath } else { exit 1 }"`,
};
export class FolderDialogService {
    deps;
    constructor(deps = {}) {
        this.deps = { ...defaultDeps, ...deps };
    }
    /** Returns the OS-specific command string, or null if the platform is unsupported. */
    getCommand() {
        return PLATFORM_COMMANDS[this.deps.platform] ?? null;
    }
    /**
     * Opens a native folder picker dialog and returns the selected absolute path.
     * Returns `null` if the user cancels the dialog.
     * Throws if the platform is unsupported or the command fails unexpectedly.
     */
    pickFolder() {
        const command = this.getCommand();
        if (!command) {
            throw new Error(`Unsupported platform: ${this.deps.platform}`);
        }
        try {
            const result = this.deps.exec(command, { encoding: 'utf-8', timeout: 60_000 });
            const trimmed = result.trim();
            return trimmed.length > 0 ? trimmed : null;
        }
        catch (error) {
            // User cancelled the dialog — exit code 1 from osascript/zenity/powershell
            if (isExecError(error) && error.status === 1) {
                return null;
            }
            throw error;
        }
    }
}
function isExecError(error) {
    return typeof error === 'object' && error !== null && 'status' in error;
}
