// src/lib/nav-active.ts
function normalizePathname(pathname) {
  if (pathname.length > 1 && pathname.endsWith("/")) return pathname.slice(0, -1);
  return pathname;
}
function navUrlPath(url) {
  const hash = url.indexOf("#");
  return hash >= 0 ? url.slice(0, hash) : url;
}
function navUrlFragment(url) {
  const i = url.indexOf("#");
  return i >= 0 ? url.slice(i + 1) : null;
}
function normalizedLocationHash(locationHash) {
  if (!locationHash) return "";
  return locationHash.startsWith("#") ? locationHash.slice(1) : locationHash;
}
function buildNavHashClaims(urls) {
  const map = /* @__PURE__ */ new Map();
  for (const url of urls) {
    const p = navUrlPath(url);
    const f = navUrlFragment(url);
    if (!p || f === null) continue;
    let set = map.get(p);
    if (!set) {
      set = /* @__PURE__ */ new Set();
      map.set(p, set);
    }
    set.add(f);
  }
  return map;
}
function navHasMoreSpecificHashMatch(pathname, locationHash, hashClaimsByPath) {
  const claims = hashClaimsByPath.get(pathname);
  if (!claims) return false;
  const h = normalizedLocationHash(locationHash);
  if (h === "") return false;
  return claims.has(h);
}
function pathnameMatchesNavPath(pathname, pathOnly, locationHash, hashClaimsByPath) {
  const norm = normalizePathname(pathname);
  const h = normalizedLocationHash(locationHash);
  if (pathOnly === "/") {
    if (norm !== "/" || h !== "") return false;
    return !navHasMoreSpecificHashMatch("/", locationHash, hashClaimsByPath);
  }
  if (norm === pathOnly) {
    return !navHasMoreSpecificHashMatch(pathOnly, locationHash, hashClaimsByPath);
  }
  if (pathOnly === "/library") {
    return norm.startsWith("/library/");
  }
  if (pathOnly.startsWith("/library/")) {
    return norm === pathOnly;
  }
  return norm.startsWith(`${pathOnly}/`);
}
function resolveActiveNavHref(pathname, candidateUrls, options) {
  const norm = normalizePathname(pathname);
  const hashClaims = options?.hashClaimsByPath ?? buildNavHashClaims(candidateUrls);
  const locationHash = options?.locationHash ?? "";
  let bestHref = null;
  let bestLen = -1;
  for (const url of candidateUrls) {
    const pathOnly = navUrlPath(url);
    const frag = navUrlFragment(url);
    if (!pathOnly || pathOnly === "#") continue;
    let matches = false;
    if (frag !== null) {
      const h = normalizedLocationHash(locationHash);
      if (pathOnly === "/") matches = norm === "/" && h === frag;
      else if (pathOnly === "/library") matches = norm.startsWith("/library/") && h === frag;
      else if (pathOnly.startsWith("/library/")) matches = norm === pathOnly && h === frag;
      else matches = norm === pathOnly && h === frag;
    } else {
      matches = pathnameMatchesNavPath(norm, pathOnly, locationHash, hashClaims);
    }
    if (matches && pathOnly.length > bestLen) {
      bestHref = url;
      bestLen = pathOnly.length;
    }
  }
  return bestHref;
}
function isNavHrefActive(pathname, url, allNavUrls, options) {
  const active = resolveActiveNavHref(pathname, allNavUrls, options);
  if (!active) return false;
  return navUrlPath(active) === navUrlPath(url) && navUrlFragment(active) === navUrlFragment(url);
}
function collectNavUrls(items) {
  const out = [];
  const walk = (list) => {
    for (const it of list) {
      if (typeof it.url === "string" && it.url.length > 0 && it.url !== "#") out.push(it.url);
      if (Array.isArray(it.children)) walk(it.children);
    }
  };
  walk(items);
  return out;
}
export {
  buildNavHashClaims,
  collectNavUrls,
  isNavHrefActive,
  navUrlFragment,
  navUrlPath,
  normalizePathname,
  normalizedLocationHash,
  resolveActiveNavHref
};
//# sourceMappingURL=nav-active.js.map