import { commandExists, runCommand } from "../commands.js"

export const hasCups = () => commandExists("lpstat")

export const listPrinters = async () => {
    if (!await hasCups()) return []

    const result = await runCommand("lpstat", ["-p"], { timeoutMs: 10_000 })
    if (result.code !== 0) return []

    return result.stdout
        .split(/\r?\n/)
        .map((line) => line.match(/^printer\s+(\S+)/)?.[1])
        .filter((printer): printer is string => Boolean(printer))
}
