import { stat } from "node:fs/promises";
import { readPersistedConfig } from "../../persisted-config.js";
import { hasRequiredFields } from "./state/required-fields.js";
import React from "react";
import { render, useApp } from "ink";
import { toastStore as defaultToastStore } from "./state/toast-store-instance.js";
import { activityStore as defaultActivityStore } from "./state/activity-store-instance.js";
async function fileExists(path) {
  try {
    const s = await stat(path);
    return s.isFile();
  } catch {
    return false;
  }
}
function defaultIsTTY() {
  return Boolean(process.stdout.isTTY);
}
function defaultCi() {
  return process.env.CI === "true" || process.env.CI === "1" || process.env.GITHUB_ACTIONS === "true";
}
async function resolveEntry(opts) {
  const isTTY = opts.isTTY ?? defaultIsTTY();
  const ci = opts.ci ?? defaultCi();
  const nonInteractive = opts.nonInteractive ?? false;
  const plain = opts.plain ?? false;
  const headless = !isTTY || ci || nonInteractive || plain;
  const exists = await fileExists(opts.configPath);
  if (opts.mode === "wizard") {
    return { mode: "wizard", firstRun: !exists, headless };
  }
  if (opts.mode === "home") {
    return { mode: "home", firstRun: !exists, headless };
  }
  if (!exists) {
    return { mode: "wizard", firstRun: true, headless };
  }
  const persisted = readPersistedConfig(opts.configPath);
  if (!hasRequiredFields(persisted)) {
    return { mode: "wizard", firstRun: false, headless };
  }
  return { mode: "home", firstRun: false, headless };
}
function MountRoot(props) {
  const { exit } = useApp();
  const [ShellRoot, setShellRoot] = React.useState(null);
  const [WizardSteps, setWizardSteps] = React.useState(null);
  const [phase, setPhase] = React.useState(props.initialView);
  React.useEffect(() => {
    let cancelled = false;
    void (async () => {
      const [routerMod, wizardMod] = await Promise.all([
        import("./router/ink.js"),
        import("./components/WizardSteps.js")
      ]);
      if (cancelled) return;
      setShellRoot(() => routerMod.InkRoot);
      setWizardSteps(() => wizardMod.WizardSteps);
    })();
    return () => {
      cancelled = true;
    };
  }, []);
  if (!ShellRoot || !WizardSteps) {
    return React.createElement(React.Fragment, null);
  }
  if (phase === "wizard") {
    return React.createElement(WizardSteps, {
      store: props.store,
      catalog: props.catalog,
      configPath: props.configPath,
      agents: props.agents,
      secretStore: props.secretStore,
      onDone: () => setPhase("home"),
      onSkip: () => setPhase("home")
    });
  }
  return React.createElement(ShellRoot, {
    store: props.store,
    catalog: props.catalog,
    onExit: () => exit(),
    version: props.version,
    productName: props.productName,
    toastStore: defaultToastStore,
    activityStore: defaultActivityStore
  });
}
async function runEntry(opts) {
  const resolution = await resolveEntry(opts);
  if (resolution.headless) {
    return { resolution, mounted: false };
  }
  const { waitUntilExit } = render(
    React.createElement(MountRoot, {
      store: opts.store,
      catalog: opts.catalog,
      initialView: resolution.mode,
      version: opts.version,
      productName: opts.productName,
      configPath: opts.configPath,
      agents: opts.agents,
      secretStore: opts.secretStore
    })
  );
  await waitUntilExit();
  return { resolution, mounted: true };
}
export {
  resolveEntry,
  runEntry
};
//# sourceMappingURL=entry.js.map