import { hasAllFeatures } from "@open-mercato/shared/security/features";
function isPortalPattern(pattern) {
  if (!pattern) return false;
  return pattern.startsWith("/[orgSlug]/portal/") || pattern === "/[orgSlug]/portal";
}
function hasNoUnresolvedParams(href) {
  return !href.includes("[");
}
function resolveHref(pattern, orgSlug) {
  return pattern.replace("[orgSlug]", orgSlug);
}
function pickGroup(group) {
  if (group === "main" || group === "account") return group;
  return "main";
}
function pickPreferredRoute(existing, candidate) {
  const existingFeatures = existing.requireCustomerFeatures ?? [];
  const candidateFeatures = candidate.requireCustomerFeatures ?? [];
  if (existingFeatures.length === 0 && candidateFeatures.length > 0) return candidate;
  if (candidateFeatures.length === 0 && existingFeatures.length > 0) return existing;
  if (existingFeatures.length !== candidateFeatures.length) {
    return candidateFeatures.length > existingFeatures.length ? candidate : existing;
  }
  return existing;
}
function buildPortalNav({
  routes,
  orgSlug,
  grantedFeatures,
  isPortalAdmin = false
}) {
  const mainItems = [];
  const accountItems = [];
  const dedupedByPattern = /* @__PURE__ */ new Map();
  for (const route of routes) {
    const pattern = route.pattern ?? route.path;
    if (!isPortalPattern(pattern)) continue;
    if (route.navHidden) continue;
    const nav = route.nav;
    if (!nav || typeof nav.label !== "string" || nav.label.length === 0) continue;
    const existing = dedupedByPattern.get(pattern);
    dedupedByPattern.set(pattern, existing ? pickPreferredRoute(existing, route) : route);
  }
  for (const route of dedupedByPattern.values()) {
    const pattern = route.pattern ?? route.path;
    const nav = route.nav;
    const requireFeatures = route.requireCustomerFeatures ?? [];
    if (!isPortalAdmin && requireFeatures.length) {
      if (!hasAllFeatures(grantedFeatures, requireFeatures)) continue;
    }
    const href = resolveHref(pattern, orgSlug);
    if (!hasNoUnresolvedParams(href)) continue;
    const group = pickGroup(nav.group);
    const item = {
      id: `portal-nav:${pattern}`,
      label: nav.label,
      labelKey: nav.labelKey,
      href,
      icon: nav.icon,
      order: typeof nav.order === "number" ? nav.order : 100
    };
    if (group === "account") accountItems.push(item);
    else mainItems.push(item);
  }
  const sortItems = (items) => items.sort((a, b) => {
    if (a.order !== b.order) return a.order - b.order;
    return a.label.localeCompare(b.label);
  });
  sortItems(mainItems);
  sortItems(accountItems);
  const groups = [];
  if (mainItems.length) groups.push({ id: "main", items: mainItems });
  if (accountItems.length) groups.push({ id: "account", items: accountItems });
  return groups;
}
function mergePortalSidebarGroupsWithInjected(discovered, injected) {
  const mergeGroup = (base, extra) => {
    const knownIds = new Set(base.map((item) => item.id));
    const knownHrefs = new Set(base.map((item) => item.href).filter((href) => Boolean(href)));
    const merged = [...base];
    for (const item of extra) {
      if (knownIds.has(item.id)) continue;
      if (item.href && knownHrefs.has(item.href)) continue;
      merged.push(item);
      knownIds.add(item.id);
      if (item.href) knownHrefs.add(item.href);
    }
    return merged;
  };
  const mainBase = discovered.find((g) => g.id === "main")?.items ?? [];
  const accountBase = discovered.find((g) => g.id === "account")?.items ?? [];
  return {
    main: mergeGroup(mainBase, injected.main),
    account: mergeGroup(accountBase, injected.account)
  };
}
export {
  buildPortalNav,
  mergePortalSidebarGroupsWithInjected
};
//# sourceMappingURL=nav.js.map
