import { spawn, execFile } from "child_process";
import { promisify } from "util";
import { detectPlatform } from "./utils.js";
const execFileAsync = promisify(execFile);
function spawnWithInput(command, args, input) {
    return new Promise((resolve, reject) => {
        const child = spawn(command, args, { stdio: ["pipe", "ignore", "ignore"] });
        child.stdin.end(input);
        child.on("close", () => resolve());
        child.on("error", reject);
    });
}
export async function _copy(text) {
    const platform = await detectPlatform();
    if (platform === "macos") {
        await spawnWithInput("pbcopy", [], text);
    }
    else if (platform === "linux") {
        await spawnWithInput("xclip", ["-selection", "clipboard"], text);
    }
    else {
        console.error(`copy is not supported on platform: ${platform}. ` +
            `Supported platforms: macOS, Linux.`);
    }
}
export async function _paste() {
    const platform = await detectPlatform();
    if (platform === "macos") {
        const { stdout } = await execFileAsync("pbpaste");
        return stdout;
    }
    else if (platform === "linux") {
        const { stdout } = await execFileAsync("xclip", ["-selection", "clipboard", "-o"]);
        return stdout;
    }
    else {
        console.error(`paste is not supported on platform: ${platform}. ` +
            `Supported platforms: macOS, Linux.`);
        return "";
    }
}
