/* Lodestay — the route table.
   Deliberately NOT routes: the ⌘K palette, the notifications panel, the map
   pane and its selected pin, the search filter bar and its scope, the
   list/map switch, the confirm dialogs, and the arrival, review and cancel
   dialogs. They are states of a screen, not destinations — a breadcrumb that
   wants one of them is a bug in the screen.

   Four top-level destinations, wide canvases, a map that wants the whole
   width: this product runs the TOP-NAV shell.

   There is no /stays/:id/reviews and no /stays/:id/photos. A place is one
   record; its reviews and its rooms are sections of it, and registering a
   route to make a link resolve would be inventing a screen. Checkout is a
   route because it is a staged form the guest can be sent back to, and its
   breadcrumb has to say which place it belongs to. The receipt is a route for
   the same reason turned around: it is the one page a guest sends to somebody
   else, so it needs an address of its own. */

const LODESTAY_NAV = [
  { path: "/", label: "Stays", icon: "map-pinned" },
  { path: "/trips", label: "Trips", icon: "luggage", badge: "tripsNeedingYou" },
  { path: "/saved", label: "Saved", icon: "bookmark" },
  { path: "/inbox", label: "Messages", icon: "message-circle", badge: "unreadThreads" },

  /* Registered, not in the bar. */
  { path: "/stays/:id", label: "Place", parent: "/", offSidebar: true },
  { path: "/stays/:id/book", label: "Checkout", parent: "/stays/:id", offSidebar: true },
  { path: "/trips/:id", label: "Trip", parent: "/trips", offSidebar: true },
  { path: "/trips/:id/receipt", label: "Receipt", parent: "/trips/:id", offSidebar: true },
  /* A host is a record reached from a place, a trip or a conversation — never a
     destination, which is why it has a route and no bar item. */
  { path: "/hosts/:id", label: "Host", parent: "/", offSidebar: true },
  { path: "/account/profile", label: "Your profile", offSidebar: true },
  { path: "/account/payments", label: "Payment methods", offSidebar: true },

  /* Auth namespace — excluded from the bar; no product chrome. Password reset
     is four screens (request, sent, set-new, done) because each is a real
     stop: the middle two are where the wait and the dead link live. */
  { path: "/signin", label: "Sign in", offSidebar: true },
  { path: "/signup", label: "Create account", offSidebar: true },
  { path: "/verify-email", label: "Confirm your email", parent: "/signup", offSidebar: true },
  { path: "/forgot-password", label: "Reset your password", offSidebar: true },
  { path: "/forgot-password/sent", label: "Check your email", parent: "/forgot-password", offSidebar: true },
  { path: "/reset-password/:token", label: "Set new password", parent: "/forgot-password", offSidebar: true },
  { path: "/reset-password/done", label: "Password changed", parent: "/forgot-password", offSidebar: true },
  { path: "/account-locked", label: "Account locked", offSidebar: true },
  { path: "/signout", label: "Sign out", offSidebar: true },
];

const LODESTAY_ROUTES = indexRoutes(LODESTAY_NAV);

/* The projection the bar renders — derived by filtering the tree, never retyped. */
const LODESTAY_BAR = LODESTAY_NAV.filter((r) => !r.offSidebar);

const LODESTAY_ACCOUNT_MENU = [
  LODESTAY_ROUTES["/account/profile"],
  LODESTAY_ROUTES["/account/payments"],
  LODESTAY_ROUTES["/signout"],
];

/* Paths that render without product chrome (AGENTS §7.1). */
const LODESTAY_AUTH_PATHS = [
  "/signin", "/signup", "/verify-email", "/forgot-password", "/forgot-password/sent",
  "/reset-password/:token", "/reset-password/done", "/account-locked", "/signout",
];
/* The token route is parameterised, so membership is a test rather than a
   lookup — and /reset-password/done is a literal in the same namespace. */
const isAuthPath = (path) => LODESTAY_AUTH_PATHS.indexOf(path) > -1 || path.indexOf("/reset-password/") === 0;

/* The preview menu's page list — this table read whole and grouped, never
   retyped, with the parameterised routes resolved against the fixture so the
   link lands on a real record rather than on ":id". A pattern with no fixture
   example is left out rather than offered as a link resolving nowhere. */
const LODESTAY_PAGES = (data) => {
  const listing = data.listings[0], booking = data.bookings[0], host = data.hosts[0];
  const live = RESET_LINKS.find((l) => !AUTH_FLAGS.linkExpired(l, data.now));
  const dead = RESET_LINKS.find((l) => AUTH_FLAGS.linkExpired(l, data.now));
  const groups = templatePages(LODESTAY_NAV, LODESTAY_AUTH_PATHS, {
    "/stays/:id": listing ? "/stays/" + listing.id : null,
    "/stays/:id/book": listing ? "/stays/" + listing.id + "/book" : null,
    "/trips/:id": booking ? "/trips/" + booking.id : null,
    "/trips/:id/receipt": booking ? "/trips/" + booking.id + "/receipt" : null,
    "/hosts/:id": host ? "/hosts/" + host.id : null,
    "/reset-password/:token": live ? "/reset-password/" + live.token : null,
  });
  /* Both sides of one route. The reset screen is the form or the dead link
     depending on the token in the URL, and this menu exists so a reader can
     open either without waiting an hour for the first one to die. */
  if (dead) groups.forEach((group) => { if (group.label === "Auth") group.items.push({ to: "/reset-password/" + dead.token, label: "Reset link expired" }); });
  return groups;
};

Object.assign(window, { LODESTAY_NAV, LODESTAY_ROUTES, LODESTAY_BAR, LODESTAY_ACCOUNT_MENU, LODESTAY_AUTH_PATHS, isAuthPath, LODESTAY_PAGES });
