// Backend-neutral file, asset and clock access.
//
// This package exists because `tycho_image` used to read and write files through
// `laplaceliu/tycho_shader`, which is a `foreign_library` with a native C stub —
// so every renderer that decodes an image inherited a hard native-only edge and
// could not be built for the browser. Only five call sites needed that, and all
// of them were plain file IO.
//
// Keeping the edge here instead has a second payoff: the three environments
// genuinely differ in *where* bytes come from (desktop file system, Android
// asset manager, browser fetch), and that difference now has one place to live.
//
// The implementation is chosen per target (`impl_native.mbt` / `impl_js.mbt`)
// rather than through a virtual package: every executable that transitively
// reaches a virtual package has to declare an override, and `2d`/`3d` pull this
// in through `image`. See roadmap/adr-005-module-architecture.md (decision D2).

///|
/// Read a file (or asset) into memory.
///
/// Returns empty `Bytes` when the path cannot be resolved or read, matching the
/// behaviour the shader package had. Callers that must distinguish "missing"
/// from "empty" should ask `asset_exists` first.
pub fn read_file(path : String) -> Bytes {
  c_read_file(path)
}

///|
/// Write `data` to `path`, replacing any existing file.
///
/// Returns `false` on failure. On the browser target writes go to an in-page
/// virtual file system, so a round trip inside one page works but nothing is
/// persisted to disk.
pub fn write_file(path : String, data : Bytes) -> Bool {
  c_write_file(path, data) != 0
}

///|
/// Whether `path` can be resolved and read.
pub fn asset_exists(path : String) -> Bool {
  c_asset_exists(path) != 0
}

///|
/// Monotonic milliseconds. Only differences between two readings are meaningful;
/// the epoch is unspecified.
pub fn now_ms() -> UInt64 {
  c_now_ms()
}