// Which section of a video library page is showing.
//
// Browse / All / Genres used to be three routes per library, named
// inconsistently across the two libraries (`/library/tv/shows` vs
// `/library/movies/all`; `/library/shows/genres` vs `/library/movies/genres`).
// They are now one route with tabs, and this is the pure `?view=` ↔ tab
// mapping.
//
// TRACES: UR-063 | DR-105
export type LibraryView = "browse" | "all" | "genres";

/** Tab order, left to right. `browse` leads because it is the landing view. */
export const LIBRARY_VIEWS: readonly LibraryView[] = ["browse", "all", "genres"];

/** The view a page shows when `?view=` is absent or unrecognised. */
export const DEFAULT_LIBRARY_VIEW: LibraryView = "browse";

/**
 * Read a `?view=` value. Anything unknown — a typo, a stale bookmark, a
 * removed tab — lands on the default rather than rendering nothing.
 */
export function resolveLibraryView(value: string | null | undefined): LibraryView {
  if (value == null) return DEFAULT_LIBRARY_VIEW;
  const normalized = value.trim().toLowerCase();
  return (LIBRARY_VIEWS as readonly string[]).includes(normalized)
    ? (normalized as LibraryView)
    : DEFAULT_LIBRARY_VIEW;
}

/**
 * URL for a tab. The default view omits the param, so the landing URL stays
 * `/library/tv` — the same convention `searchRouteUrl` uses for the `all` scope.
 */
export function libraryViewUrl(basePath: string, view: LibraryView): string {
  return view === DEFAULT_LIBRARY_VIEW ? basePath : `${basePath}?view=${view}`;
}
