/* Router — the URL is the source of truth for what page you are on.
 *
 * Until now the route lived only in localStorage, so every page in the site
 * shared the single URL "/". You could not link to Quiénes somos, could not
 * share an area page, the browser's back button did nothing, and a search
 * engine saw one page. This maps each route onto a real path and keeps the
 * two in sync.
 *
 * Paths are Spanish across the board rather than switching with the language
 * toggle. A URL that changes under you when you flip to English would break
 * any link already shared, and the domain is a .com for a Madrid firm whose
 * primary audience reads Spanish.
 *
 * Deep links work because the build emits a real index.html for every path
 * (see scripts/build-pages.cjs) — GitHub Pages then answers 200 with the
 * app, which boots and reads location.pathname. No 404 fallback trick, so
 * shared links return a real page to crawlers as well as to people.
 */

/* route id → path. Dynamic routes (sector, article) carry a `:param`
   placeholder that is substituted in and parsed back out. */
const AI_ROUTES = [
  { route: "home",        path: "/" },
  { route: "quienes",     path: "/quienes-somos" },
  { route: "proyectos",   path: "/proyectos" },
  { route: "sector",      path: "/proyectos/:sectorId" },
  { route: "personas",    path: "/equipo" },
  { route: "talento",     path: "/talento" },
  { route: "vida",        path: "/talento/vida-en-aguilera" },
  { route: "newsletter",  path: "/noticias" },
  { route: "article",     path: "/noticias/:articleId" },
  { route: "contacto",    path: "/contacto" },
  { route: "cookies",     path: "/politica-de-cookies" },
  { route: "privacidad",  path: "/politica-de-privacidad" },
  { route: "aviso-legal", path: "/aviso-legal" },
];

/* Build a path from a route + params. Unknown routes fall back to "/" so a
   typo in a setRoute call sends someone home rather than to a dead URL. */
const aiPathFor = (state) => {
  const entry = AI_ROUTES.find(r => r.route === state.route);
  if (!entry) return "/";
  return entry.path
    .replace(":sectorId", state.sectorId || "")
    .replace(":articleId", state.articleId || "")
    .replace(/\/$/, "") || "/";
};

/* Parse a path back into { route, sectorId, articleId }.
   Static paths win over dynamic ones, so /proyectos matches the hub rather
   than being read as an area whose id is the empty string. */
const aiStateFor = (pathname) => {
  const clean = (pathname || "/").replace(/\/+$/, "") || "/";
  const exact = AI_ROUTES.find(r => !r.path.includes(":") && r.path === clean);
  if (exact) return { route: exact.route, sectorId: null, articleId: null };

  for (const r of AI_ROUTES) {
    if (!r.path.includes(":")) continue;
    const prefix = r.path.split("/:")[0];
    if (clean.startsWith(prefix + "/")) {
      const value = clean.slice(prefix.length + 1);
      if (!value || value.includes("/")) continue;
      if (r.route === "sector")  return { route: "sector",  sectorId: value, articleId: null };
      if (r.route === "article") return { route: "article", sectorId: null, articleId: value };
    }
  }
  /* Unrecognised path — render home rather than a blank screen. The URL is
     left alone so it is obvious in the address bar that the link was wrong. */
  return { route: "home", sectorId: null, articleId: null };
};

Object.assign(window, { AI_ROUTES, aiPathFor, aiStateFor });
