// @bun
import {
  getSavedCliTheme,
  resolveColorScheme,
  terminalThemePalette
} from "./chunk-8gqzjqmb.js";
import {
  __require
} from "./chunk-dhs2bg35.js";

// src/tui/elements.ts
import {
  BoxRenderable,
  TextRenderable,
  ScrollBoxRenderable
} from "@opentui/core";
function box(ctx, options = {}, children = []) {
  const node = new BoxRenderable(ctx, options);
  for (const child of children)
    node.add(child);
  return node;
}
function text(ctx, content, options = {}) {
  return new TextRenderable(ctx, { content, ...options });
}
function scrollBox(ctx, options = {}, children = []) {
  const node = new ScrollBoxRenderable(ctx, options);
  for (const child of children)
    node.add(child);
  return node;
}

// src/tui/style.ts
import {
  t,
  fg,
  bg,
  bold,
  italic,
  underline,
  dim,
  link,
  StyledText,
  RGBA,
  parseColor,
  stringToStyledText
} from "@opentui/core";

// src/tui/keyboard.ts
function onKey(renderer, handler) {
  renderer.keyInput.on("keypress", handler);
  return () => renderer.keyInput.off("keypress", handler);
}

// src/tui/reactive.ts
function signal(initial) {
  let value = initial;
  const listeners = new Set;
  const read = () => value;
  read.set = (next) => {
    if (Object.is(next, value))
      return;
    value = next;
    for (const listener of listeners)
      listener(value);
  };
  read.subscribe = (listener) => {
    listeners.add(listener);
    return () => {
      listeners.delete(listener);
    };
  };
  return read;
}
function bind(apply, deps) {
  apply();
  const disposers = deps.map((dep) => dep.subscribe(apply));
  return () => {
    for (const dispose of disposers)
      dispose();
  };
}

// src/tui/lifecycle.ts
function createScope() {
  const disposers = [];
  let disposed = false;
  const scope = {
    add(dispose) {
      if (disposed) {
        dispose();
        return dispose;
      }
      disposers.push(dispose);
      return dispose;
    },
    child() {
      const c = createScope();
      scope.add(() => c.dispose());
      return c;
    },
    dispose() {
      if (disposed)
        return;
      disposed = true;
      for (let i = disposers.length - 1;i >= 0; i--) {
        try {
          disposers[i]();
        } catch {}
      }
      disposers.length = 0;
    },
    get disposed() {
      return disposed;
    }
  };
  return scope;
}
function interval(scope, ms, fn) {
  const id = setInterval(fn, ms);
  id.unref?.();
  return scope.add(() => clearInterval(id));
}
function timeout(scope, ms, fn) {
  const id = setTimeout(fn, ms);
  id.unref?.();
  return scope.add(() => clearTimeout(id));
}

// src/tui/router.ts
function router(ctx, parentScope, source, build) {
  const container = box(ctx, { flexDirection: "column" });
  let viewScope = null;
  const swap = () => {
    viewScope?.dispose();
    for (const child of [...container.getChildren()]) {
      container.remove(child.id);
      child.destroyRecursively();
    }
    viewScope = createScope();
    container.add(build(source(), viewScope));
  };
  parentScope.add(() => viewScope?.dispose());
  parentScope.add(bind(swap, [source]));
  return container;
}

// src/tui/mount.ts
async function mount(build, config = {}) {
  const { createCliRenderer } = await import("@opentui/core");
  const renderer = await createCliRenderer({
    screenMode: "main-screen",
    exitOnCtrlC: false,
    ...config
  });
  const scope = createScope();
  renderer.root.add(build(renderer, scope));
  renderer.start();
  let resolveExit;
  const exited = new Promise((resolve) => {
    resolveExit = resolve;
  });
  let unmounted = false;
  return {
    renderer,
    scope,
    unmount() {
      if (unmounted)
        return;
      unmounted = true;
      scope.dispose();
      try {
        renderer.destroy();
      } catch {}
      resolveExit();
    },
    waitUntilExit: () => exited
  };
}

// src/tui/render-to-string.ts
import { createTestRenderer } from "@opentui/core/testing";
async function renderToString(build, options = {}) {
  const width = options.width ?? (process.stdout.columns || 80);
  const height = options.maxHeight ?? 24;
  const setup = await createTestRenderer({ width, height });
  try {
    setup.renderer.root.alignItems = "flex-start";
    setup.renderer.root.add(build(setup.renderer));
    await setup.renderOnce();
    return trimFrame(setup.captureCharFrame());
  } finally {
    setup.renderer.destroy();
  }
}
function trimFrame(frame) {
  const lines = frame.split(`
`).map((line) => line.replace(/\s+$/, ""));
  while (lines.length > 0 && lines[lines.length - 1] === "")
    lines.pop();
  return lines.join(`
`);
}
// src/theme/index.ts
var darkTheme = terminalThemePalette.dark.cli;
var lightTheme = terminalThemePalette.light.cli;
var getActiveTheme = (preference = getSavedCliTheme()) => {
  return resolveColorScheme(preference) === "light" ? lightTheme : darkTheme;
};

export { box, text, scrollBox, t, fg, bold, link, onKey, signal, bind, createScope, interval, timeout, router, mount, renderToString, getActiveTheme };
