import { spawnSync } from "child_process";
import { clipboard } from "./clipboard-native.js";
const DEFAULT_CLIPBOARD_TIMEOUT_MS = 3000;
const DEFAULT_MAX_BUFFER_BYTES = 10 * 1024 * 1024;
function runPowerShellClipboardRead(command) {
    const result = spawnSync(command, [
        "-NoProfile",
        "-Command",
        "[Console]::OutputEncoding = [Text.Encoding]::UTF8; $text = Get-Clipboard -Raw -Format Text -ErrorAction SilentlyContinue; if ($null -ne $text) { [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($text)) }",
    ], {
        timeout: DEFAULT_CLIPBOARD_TIMEOUT_MS,
        maxBuffer: DEFAULT_MAX_BUFFER_BYTES,
        windowsHide: true,
    });
    if (result.error || result.status !== 0) {
        return null;
    }
    const encoded = Buffer.isBuffer(result.stdout)
        ? result.stdout.toString("utf-8").trim()
        : String(result.stdout ?? "").trim();
    if (!encoded) {
        return null;
    }
    try {
        return Buffer.from(encoded, "base64").toString("utf-8");
    }
    catch {
        return null;
    }
}
export async function readClipboardText(options) {
    const platform = options?.platform ?? process.platform;
    if (clipboard && platform !== "linux" && clipboard.hasText()) {
        const text = await clipboard.getText();
        return text.length > 0 ? text : null;
    }
    if (platform !== "win32") {
        return null;
    }
    return runPowerShellClipboardRead("pwsh.exe") ?? runPowerShellClipboardRead("powershell.exe");
}
//# sourceMappingURL=clipboard-text.js.map