/**
 * Route → human label resolution shared by the nav-history provider and the
 * back button. Keep the keys in sync with the top-level routes under src/app.
 */

export const SITE_NAME = "Young Engineers";

export const ROUTE_LABELS: Record<string, string> = {
  "/": "Home",
  "/about": "About",
  "/programs": "Programs",
  "/workshops": "Workshops",
  "/class-registration": "Class Registration",
  "/form-submission": "Form Submission",
  "/privacy-policy": "Privacy Policy",
  "/terms-of-use": "Terms of Use",
};

export interface NavEntry {
  /** Pathname + search, e.g. "/programs?lang=he". */
  href: string;
  label: string;
  /**
   * The label came from the page itself (see NavLabel) rather than from the URL
   * or the document title, so nothing should overwrite it.
   */
  explicit?: boolean;
}

/** "/programs/5" → "/programs". "/a" → "/". "/" → null. */
function parentOf(pathname: string): string | null {
  const segments = pathname.split("/").filter(Boolean);
  if (segments.length === 0) return null;
  if (segments.length === 1) return "/";
  return "/" + segments.slice(0, -1).join("/");
}

function titleCase(segment: string): string {
  return segment.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}

/**
 * A label derived from the URL alone, used before `document.title` has settled
 * and as the label for the fallback target.
 */
export function labelForPath(pathname: string): string {
  const known = ROUTE_LABELS[pathname];
  if (known) return known;

  const segments = pathname.split("/").filter(Boolean);
  if (segments.length === 0) return ROUTE_LABELS["/"];
  return titleCase(segments[segments.length - 1]);
}

/**
 * Where the back button points when there is no history to go back to — a
 * direct landing from search, a shared link, or a fresh tab.
 *
 * Walks up the ancestors and returns the first one we have a real name for, so
 * a deep SEO URL like /workshops/12/bricks-challenge lands on Workshops rather
 * than on /workshops/12 (which is the same detail page under a shorter URL).
 * Falls back to Home.
 */
export function fallbackEntry(pathname: string): NavEntry {
  let current = parentOf(pathname);
  while (current) {
    if (ROUTE_LABELS[current]) {
      return { href: current, label: ROUTE_LABELS[current] };
    }
    current = parentOf(current);
  }
  return { href: "/", label: ROUTE_LABELS["/"] };
}

/**
 * Turns `document.title` into a back-button label, or null when it carries no
 * more information than the URL already does.
 *
 * Titles are pipe-delimited — either the "%s | Young Engineers" template or a
 * backend `seo_title` like "Smartivo | Coding Basics for Ages 4-6". Only the
 * first segment names the page, so the rest is dropped: the button reads
 * "Go back to Smartivo", never the whole SEO string.
 *
 * Detail pages should register their name via NavLabel instead of relying on
 * this; it is the fallback for everything else.
 */
export function labelFromDocumentTitle(title: string): string | null {
  const first = title.split("|")[0].trim();
  if (!first || first === SITE_NAME) return null;
  return first;
}
