// src/navigation/index.ts
function isNavLink(item) {
  return item.kind === "link";
}
function isNavDropdown(item) {
  return item.kind === "dropdown";
}
function getPrimaryNavigation(navigation) {
  if (!navigation || navigation.length === 0) return null;
  return navigation.find((menu) => menu.isPrimary) ?? navigation[0] ?? null;
}
function getNavigationBySlug(navigation, slug) {
  if (!navigation || navigation.length === 0) return null;
  return navigation.find((menu) => menu.name === slug) ?? null;
}
function getPrimaryNavItems(navigation) {
  return transformToNavItems(getPrimaryNavigation(navigation));
}
function getNavItemsBySlug(navigation, slug) {
  return transformToNavItems(getNavigationBySlug(navigation, slug));
}
function transformToNavItems(menu) {
  if (!menu?.items || menu.items.length === 0) return [];
  return menu.items.filter((item) => !item.parentId && item.urlType !== "dropdown").sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0)).map((item) => ({
    label: item.label,
    href: extractHref(item),
    isExternal: isExternalLink(item)
  })).filter((item) => Boolean(item.href));
}
function transformToNestedNavItems(menu) {
  if (!menu?.items || menu.items.length === 0) return [];
  const toNavLink = (item) => {
    const href = extractHref(item);
    if (!href) return null;
    return {
      kind: "link",
      id: item.id,
      label: item.label,
      href,
      isExternal: isExternalLink(item)
    };
  };
  const { items } = buildNestedStructure(
    menu.items,
    {
      toLink: toNavLink,
      createDropdown: (id, label, children) => ({
        kind: "dropdown",
        id,
        label,
        children
      }),
      extractCta: false
    }
  );
  return items;
}
function getNestedPrimaryNavItems(navigation) {
  return transformToNestedNavItems(getPrimaryNavigation(navigation));
}
function getNestedNavItemsBySlug(navigation, slug) {
  return transformToNestedNavItems(getNavigationBySlug(navigation, slug));
}
function buildMenuViewModel(navigation) {
  const menu = getPrimaryNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null };
  }
  const flatItems = menu.items.filter((item) => !item.parentId && item.urlType !== "dropdown").sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0));
  const items = [];
  let ctaItem = null;
  for (const item of flatItems) {
    const viewItem = {
      id: item.id,
      label: item.label,
      link: convertToLinkValue(item),
      target: null,
      rel: null,
      active: false
    };
    if (!ctaItem && Boolean(item.isCta)) {
      ctaItem = { ...viewItem, variant: "primary" };
      continue;
    }
    items.push(viewItem);
  }
  return { items, ctaItem };
}
function buildLogoViewModel(logo, fallbackTitle) {
  if (!logo) {
    return null;
  }
  if (!logo.url && !logo.storagePath) {
    return null;
  }
  const alt = logo.alt && logo.alt.trim().length > 0 ? logo.alt : fallbackTitle ?? "Site logo";
  return {
    type: "image",
    src: logo.url ?? "",
    // Empty when using storagePath - MediaNode builds direct URL
    alt,
    assetId: logo.assetId ?? void 0,
    width: logo.width ?? void 0,
    height: logo.height ?? void 0,
    storagePath: logo.storagePath ?? void 0,
    storageBucket: logo.storageBucket ?? void 0
  };
}
function extractHref(item) {
  const link = item.url;
  if (!link || typeof link !== "object" || !("href" in link)) return "";
  return link.href;
}
function isExternalLink(item) {
  const link = item.url;
  if (!link || typeof link !== "object" || !("kind" in link)) return false;
  return link.kind === "external";
}
function buildChildrenByParentId(items) {
  const childrenByParentId = /* @__PURE__ */ new Map();
  const itemsById = /* @__PURE__ */ new Map();
  for (const item of items) {
    itemsById.set(item.id, item);
  }
  for (const item of items) {
    if (item.parentId) {
      const parent = itemsById.get(item.parentId);
      if (parent?.parentId && typeof process !== "undefined" && process.env.NODE_ENV !== "production") {
        console.warn(
          `[SDK Navigation] Deeply nested item detected: "${item.label}" (id: ${item.id}). Only 1 level of nesting is supported. This item will be ignored.`
        );
        continue;
      }
      const siblings = childrenByParentId.get(item.parentId) ?? [];
      siblings.push(item);
      childrenByParentId.set(item.parentId, siblings);
    }
  }
  return childrenByParentId;
}
function buildNestedStructure(items, options) {
  const childrenByParentId = buildChildrenByParentId(items);
  const rootItems = items.filter((item) => !item.parentId).sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0));
  const result = [];
  let ctaItem = null;
  for (const item of rootItems) {
    if (options.extractCta && !ctaItem && Boolean(item.isCta) && item.urlType !== "dropdown") {
      const link = options.toLink(item);
      if (link) {
        ctaItem = link;
        continue;
      }
    }
    if (item.urlType === "dropdown") {
      const childRecords = childrenByParentId.get(item.id) ?? [];
      const children = childRecords.sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0)).map(options.toLink).filter((child) => child !== null);
      if (children.length > 0) {
        result.push(options.createDropdown(item.id, item.label, children));
      }
    } else {
      const link = options.toLink(item);
      if (link) {
        result.push(link);
      }
    }
  }
  return { items: result, ctaItem };
}
function convertToLinkValue(item) {
  const payload = item.url;
  if (!payload) return null;
  const kind = typeof payload.kind === "string" ? payload.kind : null;
  if (kind === "external" || kind === "url") {
    const href = typeof payload.href === "string" ? payload.href : null;
    return href ? { kind, href } : null;
  }
  if (kind === "internal") {
    const routeId = typeof payload.routeId === "string" ? payload.routeId : null;
    const entityId = typeof payload.entityId === "string" ? payload.entityId : null;
    const entityType = payload.entityType === "page" || payload.entityType === "content" ? payload.entityType : null;
    const href = typeof payload.href === "string" ? payload.href : null;
    const title = typeof payload.title === "string" ? payload.title : null;
    const typeLabel = typeof payload.typeLabel === "string" ? payload.typeLabel : null;
    if (!routeId || !entityId || !entityType || !href || !title || !typeLabel) {
      return null;
    }
    return {
      kind: "internal",
      routeId,
      entityId,
      entityType,
      href,
      title,
      typeLabel,
      contentTypeKey: typeof payload.contentTypeKey === "string" ? payload.contentTypeKey : null,
      contentTypeName: typeof payload.contentTypeName === "string" ? payload.contentTypeName : null,
      updatedAt: typeof payload.updatedAt === "string" ? payload.updatedAt : null
    };
  }
  return null;
}
function buildSimpleMenu(navigation, routes) {
  const menu = getPrimaryNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null };
  }
  const flatItems = menu.items.filter((item) => !item.parentId && item.urlType !== "dropdown").sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0));
  const items = [];
  let ctaItem = null;
  for (const item of flatItems) {
    const link = item.url;
    const href = resolveHref(link, routes);
    if (!href) continue;
    const navLink = {
      id: item.id,
      label: item.label,
      href,
      isExternal: link?.kind === "external"
    };
    if (!ctaItem && Boolean(item.isCta)) {
      ctaItem = navLink;
      continue;
    }
    items.push(navLink);
  }
  return { items, ctaItem };
}
function buildSimpleNestedMenu(navigation, routes) {
  const menu = getPrimaryNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null };
  }
  const toSimpleLink = (item) => {
    const link = item.url;
    const href = resolveHref(link, routes);
    if (!href) return null;
    return {
      kind: "link",
      id: item.id,
      label: item.label,
      href,
      isExternal: link?.kind === "external"
    };
  };
  return buildNestedStructure(
    menu.items,
    {
      toLink: toSimpleLink,
      createDropdown: (id, label, children) => ({
        kind: "dropdown",
        id,
        label,
        children
      }),
      extractCta: true
    }
  );
}
function resolveHref(link, routes) {
  if (!link) return null;
  if (link.kind === "external" || link.kind === "url") {
    return link.href || null;
  }
  if (link.kind === "internal" && link.routeId) {
    const route = routes[link.routeId];
    if (route) {
      if (typeof route === "string") {
        return route;
      }
      return route.path ?? route.href ?? route.draftPath ?? link.href ?? null;
    }
    return link.href ?? null;
  }
  return null;
}
function buildSimpleLogo(logo, fallbackAlt) {
  if (!logo || !logo.url) {
    return null;
  }
  const alt = logo.alt && logo.alt.trim().length > 0 ? logo.alt : fallbackAlt ?? "Site logo";
  const result = {
    type: "image",
    src: logo.url,
    alt
  };
  if (logo.width != null) {
    result.width = logo.width;
  }
  if (logo.height != null) {
    result.height = logo.height;
  }
  return result;
}
var transformNavItems = transformToNavItems;
var selectPrimaryMenu = getPrimaryNavigation;

export {
  isNavLink,
  isNavDropdown,
  getPrimaryNavigation,
  getNavigationBySlug,
  getPrimaryNavItems,
  getNavItemsBySlug,
  transformToNavItems,
  transformToNestedNavItems,
  getNestedPrimaryNavItems,
  getNestedNavItemsBySlug,
  buildMenuViewModel,
  buildLogoViewModel,
  buildSimpleMenu,
  buildSimpleNestedMenu,
  buildSimpleLogo,
  transformNavItems,
  selectPrimaryMenu
};
//# sourceMappingURL=chunk-PMHLZ3FW.mjs.map