// Which step the setup wizard opens on — the decision that used to be an
// INFERENCE and is now an answer.
//
// The wizard's resumability check ("you already have a company selected with a
// chart — did you mean to resume it?") was written when #setup had exactly one
// door: App.tsx's zero-companies first run. Asking is right there, because the
// user stated nothing. It grew more doors since — the company switcher's
// "+ Add company…" (Sidebar.tsx) and Settings' "+ Add another company" — and
// those carry an unambiguous statement of intent. Inferring over the top of it
// made the app answer a question the user had already answered: choose "add a
// company" with an established company selected and you got "Welcome back,
// <that other company>", offering to resume books you never mentioned.
//
// So the door is now part of the route (#/setup?new=1, see lib/useHashView.ts)
// and lands here. The rule is one line: a stated intent wins; only an arrival
// with NOTHING stated gets asked.
//
// Pure on purpose — this repo's web tests have no DOM renderer, so the seam
// worth pinning is the decision, not the component around it.

/** How the user got to #setup. */
export type SetupEntry =
  /** "+ Add company…" / "+ Add another company", or "set up a different
   *  company instead" — the user said what they want; do not second-guess. */
  | "new-company"
  /** Plain #/setup: first run, a typed URL, a bookmark, or coming back to a
   *  half-finished setup. Nothing stated, so it is fair to ask. */
  | "unspecified";

/** The steps this decision can produce, plus the "still asking" placeholder. */
export type SetupEntryStep = "checking" | "name" | "choose-path" | "resume-notice";

export interface SetupCompanyState {
  /** The company the switcher currently shows, if any. */
  selectedCompanyId: string | null;
  /**
   * How many accounts that company has.
   * `undefined` — not asked yet (the fetch is still in flight).
   * `null` — asked and the request failed. Fails OPEN: a server hiccup must
   * never trap someone inside the wizard, so it is treated as "we know
   * nothing", not as "zero accounts".
   */
  accountsCount?: number | null;
}

/**
 * True when the wizard should look up the selected company's accounts at all.
 * A user who asked to ADD a company is not asking about the selected one, so
 * the request is not merely ignored — it is never made.
 */
export function asksAboutSelectedCompany(entry: SetupEntry): boolean {
  return entry !== "new-company";
}

/**
 * Given the door and what is known about the selected company, the step.
 *
 * Note the order: the stated intent is checked BEFORE anything about the
 * selected company is consulted. Reintroducing the bug means moving a
 * company-state check above this line, and the tests in
 * tests/web/setup-entry.test.ts fail when it moves.
 */
export function setupStepFor(entry: SetupEntry, state: SetupCompanyState): SetupEntryStep {
  if (entry === "new-company") return "name";
  if (!state.selectedCompanyId) return "name";
  if (state.accountsCount === undefined) return "checking";
  if (state.accountsCount === null) return "name";
  return state.accountsCount > 0 ? "resume-notice" : "choose-path";
}
