// Formal contract for the clipboard public API.
//
// This file contains only `declare` statements — the source of truth for
// what's exposed to callers. Real definitions live elsewhere in the
// package: `ClipboardError` in `error.mbt`, `Backend` trait in
// `backend.mbt`, the `Clipboard` / `Handle` structs and their methods in
// `clipboard.mbt` / `handle.mbt`.
//
// MoonBit's warning 0068 (`declaration_unimplemented`) cross-checks each
// `declare` against the matching definition, so spec drift surfaces as a
// compile-time warning.
//
// ## Concurrency
//
// `Clipboard` is *not* protected by an internal mutex. Concurrent
// `set_text` / `get_text` from multiple tasks against the same
// `Clipboard` value may interleave subprocess invocations. If you need
// strict serialization, wrap calls in your own `@async.semaphore` or
// task group.
//
// ## Async
//
// All clipboard operations except construction of `Clipboard::new_in_memory`
// and `Handle::stop` are async because the underlying transport is
// `@async/process` (subprocess spawn). Callers must invoke these from an
// async context (`async fn`, `async test`, or inside `@async.with_task_group`).
///|
/// Opaque clipboard handle. Construct with `Clipboard::new` (native
/// subprocess-based) or `Clipboard::new_in_memory` (test-only).
declare pub type Clipboard
///|
/// Opaque watch handle. Stop the watcher with `Handle::stop`.
declare pub type Handle
///|
/// Construct a clipboard handle bound to the current OS's native backend.
/// On startup the OS is detected via environment variables and (on
/// Unix-like hosts) `uname -s`, so this call may spawn a subprocess.
///
/// Raises:
/// * `Unsupported(msg)` if the host OS isn't one of macOS / Linux X11
/// / Linux Wayland / Windows (e.g. headless Linux without DISPLAY
/// or WAYLAND_DISPLAY, or `uname -s` returns an unrecognised kernel
/// like `FreeBSD`). `msg` carries a short explanation.
/// * `ToolNotFound("uname")` if `uname` isn't on PATH (the call cannot
/// distinguish Darwin from Linux without it).
/// * `Timeout("uname")` if `uname` does not respond within the default
/// timeout — guards against a hung or hostile binary on PATH.
/// * `Backend(msg)` for any other OS-detection failure (e.g. `uname`
/// exited non-zero).
declare pub async fn Clipboard::new() -> Clipboard raise ClipboardError
///|
/// Construct a clipboard handle backed by an in-memory store. Useful in
/// tests and on headless hosts where the OS clipboard isn't reachable.
/// Never raises.
declare pub fn Clipboard::new_in_memory() -> Clipboard
///|
/// Read the current clipboard text.
///
/// Raises:
/// * `Empty` if there is no text content available (clipboard
/// unowned, owner exposes only non-text MIME types, or owner
/// returns empty bytes).
/// * `NotUtf8(len)` if the bytes are not valid UTF-8. Only the byte
/// length is reported — the raw bytes are deliberately not kept,
/// so the error cannot leak clipboard contents through a log sink.
/// * `ToolNotFound` if the underlying CLI tool isn't installed.
/// * `Timeout` if the tool didn't respond in time.
/// * `Backend(msg)` for any other platform-specific failure (X
/// server unreachable, Wayland compositor down, etc.). `msg`
/// contains the underlying tool's stderr to aid diagnosis.
declare pub async fn Clipboard::get_text(
self : Clipboard,
) -> String raise ClipboardError
///|
/// Write text to the clipboard.
///
/// Empty strings are valid input. The actually-observable behaviour for
/// `get_text` after `set_text("")` is OS-dependent: some backends
/// surface it as `Ok("")`, others as `Err(Empty)`. Callers that care
/// about the distinction should handle both.
///
/// Lifecycle note (Linux only): on X11 and Wayland the clipboard is
/// held in memory by a background `xclip` / `wl-copy` child started by
/// this call. That child exits when the calling process exits, so the
/// clipboard contents written here do *not* outlive your program. If
/// you need persistence after exit, run a dedicated clipboard manager
/// (`clipmenud`, `cliphist`, etc.) and let it take ownership. macOS
/// (`pbcopy`) and Windows (PowerShell `Set-Clipboard`) hand the data to
/// the OS itself, so persistence is automatic there.
///
/// Raises:
/// * `ToolNotFound` if the underlying CLI tool isn't installed.
/// * `Timeout` if the tool didn't respond in time.
/// * `Backend` for any other platform-specific failure, including the
/// underlying tool failing to grab the selection (e.g. `DISPLAY`
/// missing on X11, no Wayland compositor reachable).
declare pub async fn Clipboard::set_text(
self : Clipboard,
text : String,
) -> Unit raise ClipboardError
///|
/// Start watching for clipboard changes. A background task in `group`
/// polls `get_text` and invokes `on_change` whenever the clipboard
/// value transitions. `on_change` runs on the same async executor
/// thread as the caller (no extra threads).
///
/// `on_change` must not raise. If callers need fallible work, they
/// should catch within the closure.
///
/// `Handle::stop` cancels the watcher immediately (the task is woken
/// by a condition variable so it does not have to wait for the next
/// poll interval).
///
/// ## Baseline / first callback
///
/// `watch` reads the clipboard once at call time to seed its
/// "previous value" baseline so a steady-state clipboard does not
/// fire `on_change`.
///
/// * If the seed read succeeds, that value becomes the baseline and
/// is *not* surfaced to `on_change`. Only later transitions are.
/// * If the seed read fails (`Empty`, `Timeout`, `NotUtf8`,
/// `Backend`, …), the next read that succeeds is surfaced to
/// `on_change`. Concretely: starting `watch` on an empty
/// clipboard and then copying something will deliver that first
/// copied value to the callback — which is usually what callers
/// want, but it does mean a transient seed failure on a non-empty
/// clipboard can produce one "false-positive" callback carrying
/// the value that was already there.
///
/// Per-tick read failures inside the poll loop are silently
/// tolerated; the watcher retries on the next tick and surfaces a
/// callback only on a successful read that differs from the
/// previous one.
declare pub async fn Clipboard::watch(
self : Clipboard,
group : @async.TaskGroup[Unit],
on_change : (String) -> Unit,
) -> Handle
///|
/// Stop a watcher previously started with `Clipboard::watch`. After this
/// returns, the callback will not be invoked again. Idempotent.
declare pub fn Handle::stop(self : Handle) -> Unit