// @bun
import {
  shutdownDevConsole
} from "./chunk-e5m20bzw.js";
import {
  open_default
} from "./chunk-fr1k79kd.js";
import {
  adkLogo
} from "./chunk-5agyx08n.js";
import {
  ensureUiServer
} from "./chunk-damz8wsc.js";
import {
  bind,
  box,
  fg,
  getActiveTheme,
  mount,
  onKey,
  signal,
  t,
  text
} from "./chunk-m2h26j5f.js";
import {
  auth
} from "./chunk-7krp2mgz.js";

// src/commands/adk-home.ts
async function resolveLoginStatus() {
  try {
    const details = await auth.getCurrentProfileDetails();
    if (!details) {
      return null;
    }
    const who = details.displayName ? `${details.displayName} (${details.email})` : details.email || details.name;
    return details.name && details.name !== "default" ? `${who} \xB7 ${details.name}` : who;
  } catch {
    return null;
  }
}
function openInBrowser(url) {
  open_default(url).catch(() => {});
}
function devConsoleHome(renderer, scope, props) {
  const { version, url, wasSpawned, loginStatus, stopping } = props;
  const theme = getActiveTheme();
  const runningLine = text(renderer, t`${fg(theme.status.success)(`${theme.symbols.dot} DevConsole running`)}${fg(theme.text.dim)(wasSpawned ? "  (started)" : "  (already running)")}`);
  const loginLine = loginStatus ? text(renderer, t`${fg(theme.text.dim)(`${theme.symbols.checkmark} Logged in as ${loginStatus}`)}`) : text(renderer, t`${fg(theme.text.dim)(`${theme.symbols.dot} Not logged in \u2014 run \`adk login\` to deploy agents`)}`);
  const stoppingLine = text(renderer, "");
  scope.add(bind(() => {
    stoppingLine.content = stopping() ? t`${fg(theme.accent.yellow)("Stopping DevConsole\u2026")}` : wasSpawned ? t`${fg(theme.text.dim)("Press Ctrl-C to stop the DevConsole.")}` : t`${fg(theme.text.dim)("Press Ctrl-C to exit (the DevConsole keeps running).")}`;
  }, [stopping]));
  return box(renderer, { flexDirection: "column", paddingX: 1, paddingY: 1 }, [
    adkLogo(renderer, {
      title: "Botpress ADK",
      subtitle: `v${version}`,
      logoColor: theme.accent.purple,
      titleColor: theme.text.primary,
      subtitleColor: theme.text.dim
    }),
    box(renderer, { marginTop: 1, flexDirection: "column" }, [
      text(renderer, t`${fg(theme.text.dim)("You're not inside an ADK agent project \u2014 opened the DevConsole instead.")}`),
      text(renderer, t`${fg(theme.text.dim)("Use it to create a new agent or open an existing one.")}`)
    ]),
    box(renderer, { marginTop: 1, flexDirection: "column" }, [
      runningLine,
      text(renderer, t`${fg(theme.text.link)(url)}`)
    ]),
    box(renderer, { marginTop: 1 }, [loginLine]),
    box(renderer, { marginTop: 1 }, [stoppingLine])
  ]);
}
async function adkHome({ version }) {
  const { port, wasSpawned, pid } = await ensureUiServer({ standalone: true });
  const url = `http://localhost:${port}`;
  openInBrowser(url);
  const loginStatus = await resolveLoginStatus();
  let appRef = null;
  let stoppingGuard = false;
  const app = await mount((renderer, scope) => {
    const stopping = signal(false);
    const handleStop = async () => {
      if (stoppingGuard)
        return;
      stoppingGuard = true;
      if (wasSpawned) {
        stopping.set(true);
        try {
          await shutdownDevConsole({ port, pid });
        } catch {}
      }
      appRef?.unmount();
    };
    scope.add(onKey(renderer, (key) => {
      if (key.ctrl && key.name === "c" || key.name === "q") {
        handleStop();
      }
    }));
    const onSignal = () => void handleStop();
    process.once("SIGINT", onSignal);
    process.once("SIGTERM", onSignal);
    scope.add(() => {
      process.off("SIGINT", onSignal);
      process.off("SIGTERM", onSignal);
    });
    return devConsoleHome(renderer, scope, { version, url, wasSpawned, loginStatus, stopping });
  }, {
    exitOnCtrlC: false,
    maxFps: 15
  });
  appRef = app;
  await app.waitUntilExit();
}

export { resolveLoginStatus, adkHome };
