// ../link-routing/dist/index.mjs
function isRouteRecord(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeRenderStage(stage) {
  return stage === "preview" ? "preview" : "published";
}
function isRouteVisibleInStage(routeEntry, stageInput) {
  const stage = normalizeRenderStage(stageInput);
  if (stage === "preview" || !isRouteRecord(routeEntry)) {
    return true;
  }
  const status = routeEntry.status;
  return !(typeof status === "string" && status !== "published");
}
function extractRouteHref(candidate, stageInput = "published") {
  if (typeof candidate === "string") {
    const trimmed = candidate.trim();
    return trimmed.length > 0 ? trimmed : null;
  }
  if (!isRouteRecord(candidate)) {
    return null;
  }
  const stage = normalizeRenderStage(stageInput);
  const fields = stage === "preview" ? ["draftPath", "path", "href", "url"] : ["path", "href", "url"];
  for (const field of fields) {
    const value = candidate[field];
    if (typeof value === "string" && value.trim().length > 0) {
      return value.trim();
    }
  }
  return null;
}
function extractRouteUpdatedAt(candidate) {
  if (!isRouteRecord(candidate)) {
    return null;
  }
  const fields = [
    "updatedAt",
    "updated_at",
    "publishedAt",
    "published_at",
    "modifiedAt",
    "modified_at"
  ];
  for (const field of fields) {
    const value = candidate[field];
    if (typeof value === "string" && value.length > 0) {
      return value;
    }
  }
  return null;
}
function resolveRouteTargetForStage(routeIdValue, routes, stageInput) {
  const routeId = typeof routeIdValue === "string" ? routeIdValue : null;
  if (!routeId || !routes) {
    return null;
  }
  const routeEntry = routes[routeId];
  if (!isRouteVisibleInStage(routeEntry, stageInput)) {
    return null;
  }
  const href = extractRouteHref(routeEntry, stageInput);
  if (!href) {
    return null;
  }
  return {
    routeId,
    href,
    updatedAt: extractRouteUpdatedAt(routeEntry)
  };
}
function resolveRouteHrefForStage(routeIdValue, routes, stageInput) {
  return resolveRouteTargetForStage(routeIdValue, routes, stageInput)?.href ?? null;
}

// src/portal/constants.ts
var PORTAL_COOKIE_MAX_AGE = 30 * 24 * 60 * 60;
var CUSTOMER_PORTAL_PATH = "/portal";
var DEFAULT_PORTAL_REDIRECT = CUSTOMER_PORTAL_PATH;

// 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;
  const primaryMenus = navigation.filter((menu) => menu.isPrimary);
  if (primaryMenus.length > 1) {
    console.warn(
      `[riverbank/sdk] Multiple primary navigation menus detected (${primaryMenus.length}); using first flagged menu by input order.`
    );
  }
  const flaggedPrimary = primaryMenus[0];
  if (flaggedPrimary) return flaggedPrimary;
  if (navigation.length === 1) return navigation[0] ?? null;
  return [...navigation].sort((a, b) => {
    const createdAtCompare = (a.createdAt ?? "").localeCompare(b.createdAt ?? "");
    if (createdAtCompare !== 0) return createdAtCompare;
    return a.id.localeCompare(b.id);
  })[0] ?? null;
}
function getFooterNavigation(navigation) {
  if (!navigation || navigation.length === 0) return null;
  const footerMenus = navigation.filter((menu) => menu.isFooter);
  if (footerMenus.length > 1) {
    console.warn(
      `[riverbank/sdk] Multiple footer navigation menus detected (${footerMenus.length}); using first flagged menu by input order.`
    );
  }
  return footerMenus[0] ?? null;
}
function getNavigationBySlug(navigation, slug) {
  if (!navigation || navigation.length === 0) return null;
  return navigation.find((menu) => menu.name === slug) ?? null;
}
function getPrimaryNavItems(navigation, options) {
  return transformToNavItems(getPrimaryNavigation(navigation), options);
}
function getNavItemsBySlug(navigation, slug, options) {
  return transformToNavItems(getNavigationBySlug(navigation, slug), options);
}
function transformToNavItems(menu, options) {
  if (!menu?.items || menu.items.length === 0) return [];
  const toNavLink = (item) => {
    const renderable = deriveRenderableNavigationLink(item, options);
    if (!renderable) return null;
    return buildNavLinkFromResolvedHref(
      renderable,
      extractHref(renderable.payload, options)
    );
  };
  const { items } = buildNestedStructure(
    menu.items,
    {
      toLink: toNavLink,
      createDropdown: (id, label, children) => ({
        kind: "dropdown",
        id,
        label,
        children
      }),
      extractCta: false
    }
  );
  return items;
}
function buildMenuViewModel(navigation, options) {
  const menu = getPrimaryNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null };
  }
  const toMenuLink = (item) => {
    const renderable = deriveRenderableNavigationLink(item, options);
    if (!renderable) return null;
    return {
      id: renderable.id,
      label: renderable.label,
      link: convertToLinkValue(renderable.payload, options),
      target: null,
      rel: null,
      active: false,
      ...renderable.cart ? { cart: renderable.cart } : {}
    };
  };
  const { items, ctaItem } = buildNestedStructure(
    menu.items,
    {
      toLink: toMenuLink,
      createDropdown: (id, label, children) => ({
        id,
        label,
        link: null,
        target: null,
        rel: null,
        active: false,
        children
      }),
      extractCta: true
    }
  );
  const ctaWithVariant = ctaItem ? { ...ctaItem, variant: "primary" } : null;
  return { items, ctaItem: ctaWithVariant };
}
function buildLogoViewModel(logo, fallbackTitle) {
  if (!logo) {
    return null;
  }
  if (!logo.url && !logo.storagePath) {
    return null;
  }
  return {
    type: "image",
    src: logo.url ?? "",
    // Empty when using storagePath - MediaNode builds direct URL
    alt: resolveAltText(logo.alt, fallbackTitle),
    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 resolveAltText(alt, fallback) {
  return alt && alt.trim().length > 0 ? alt : fallback ?? "Site logo";
}
function readNavigationItemPayload(item) {
  const payload = item.url;
  return payload ?? null;
}
function isPortalEnabled(options) {
  return options?.portalEnabled === true;
}
function isCommerceEnabled(options) {
  return options?.commerceEnabled === true;
}
function resolvePortalHref(link) {
  return link.path?.trim() || DEFAULT_PORTAL_REDIRECT;
}
function resolveCheckoutHref(options) {
  if (options?.checkoutHref === null) {
    return null;
  }
  const href = options?.checkoutHref?.trim();
  return href && href.length > 0 ? href : "/checkout";
}
function resolvePortalLabel(link, options) {
  const loggedOutLabel = link.labels?.loggedOut?.trim() || "Login";
  const loggedInLabel = link.labels?.loggedIn?.trim() || "My Account";
  return options?.hasPortalSession ? loggedInLabel : loggedOutLabel;
}
function resolveItemLabel(link, fallbackLabel, options) {
  if (link?.kind === "portal") {
    return resolvePortalLabel(link, options);
  }
  if (link?.kind === "cart") {
    return resolveCartVisibleLabel(link) ?? fallbackLabel;
  }
  return fallbackLabel;
}
function resolveCartVisibleLabel(link) {
  if (link.label.kind !== "icon-with-label") {
    return null;
  }
  const label = link.label.label.trim();
  return label.length > 0 ? label : null;
}
function resolveCartAriaLabel(link, fallbackLabel) {
  const fallback = fallbackLabel.trim();
  return resolveCartVisibleLabel(link) ?? (fallback.length > 0 ? fallback : "Cart");
}
function buildCartNavViewModel(link, fallbackLabel, options) {
  return {
    visibleLabel: resolveCartVisibleLabel(link),
    badge: link.badge,
    ariaLabel: resolveCartAriaLabel(link, fallbackLabel),
    checkoutHref: resolveCheckoutHref(options)
  };
}
function deriveRenderableNavigationLink(item, options) {
  const payload = readNavigationItemPayload(item);
  if (!payload) {
    return null;
  }
  if (payload.kind === "portal" && !isPortalEnabled(options)) {
    return null;
  }
  if (payload.kind === "cart" && !isCommerceEnabled(options)) {
    return null;
  }
  return {
    id: item.id,
    label: resolveItemLabel(payload, item.label, options),
    payload,
    isExternal: isExternalLink(payload),
    ...payload.kind === "cart" ? { cart: buildCartNavViewModel(payload, item.label, options) } : {}
  };
}
function buildNavLinkFromResolvedHref(item, href) {
  if (!href) {
    return null;
  }
  return {
    kind: "link",
    id: item.id,
    label: item.label,
    href,
    link: { href },
    isExternal: item.isExternal,
    ...item.cart ? { cart: item.cart } : {}
  };
}
function extractHref(link, options) {
  if (!link) return "";
  return resolveHref(link, options?.routes ?? {}, options) ?? "";
}
function isExternalLink(link) {
  if (!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 ?? a.position ?? 0) - (b.orderIndex ?? b.position ?? 0));
  const result = [];
  let ctaItem = null;
  for (const item of rootItems) {
    if (options.extractCta && !ctaItem && Boolean(item.isCta) && item.urlType !== "dropdown") {
      const link2 = options.toLink(item);
      if (link2) {
        ctaItem = link2;
        continue;
      }
    }
    const childRecords = childrenByParentId.get(item.id) ?? [];
    const hasChildren = childRecords.length > 0;
    const isDropdown = item.urlType === "dropdown" || hasChildren;
    if (isDropdown) {
      const children = childRecords.sort((a, b) => (a.orderIndex ?? a.position ?? 0) - (b.orderIndex ?? b.position ?? 0)).map(options.toLink).filter((child) => child !== null);
      if (children.length > 0) {
        result.push(options.createDropdown(item.id, item.label, children));
      }
      continue;
    }
    const link = options.toLink(item);
    if (link) {
      result.push(link);
    }
  }
  return { items: result, ctaItem };
}
function convertToLinkValue(payload, options) {
  if (!payload) return null;
  if (payload.kind === "portal") {
    if (!isPortalEnabled(options)) return null;
    return {
      kind: "url",
      href: resolvePortalHref(payload)
    };
  }
  if (payload.kind === "cart") {
    if (!isCommerceEnabled(options)) return null;
    const checkoutHref = resolveCheckoutHref(options);
    if (!checkoutHref) return null;
    return {
      kind: "url",
      href: checkoutHref
    };
  }
  if (payload.kind === "external" || payload.kind === "url") {
    return payload.href ? { kind: payload.kind, href: payload.href } : null;
  }
  if (payload.kind === "page") {
    return payload.identifier ? { kind: "page", identifier: payload.identifier } : null;
  }
  if (payload.kind === "entry") {
    if (!payload.contentType || !payload.identifier) {
      return null;
    }
    return {
      kind: "entry",
      contentType: payload.contentType,
      identifier: payload.identifier
    };
  }
  if (payload.kind === "internal") {
    return {
      kind: "internal",
      routeId: payload.routeId,
      entityId: payload.entityId,
      entityType: payload.entityType,
      href: payload.href,
      title: payload.title,
      typeLabel: payload.typeLabel,
      contentTypeKey: payload.contentTypeKey ?? null,
      contentTypeName: payload.contentTypeName ?? null,
      updatedAt: payload.updatedAt ?? null
    };
  }
  return null;
}
function buildMenu(navigation, routes, options) {
  const menu = getPrimaryNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null };
  }
  const toNavLink = (item) => {
    const renderable = deriveRenderableNavigationLink(item, options);
    if (!renderable) return null;
    return buildNavLinkFromResolvedHref(
      renderable,
      resolveHref(renderable.payload, routes, options)
    );
  };
  const { items, ctaItem } = buildNestedStructure(
    menu.items,
    {
      toLink: toNavLink,
      createDropdown: (id, label, children) => ({
        kind: "dropdown",
        id,
        label,
        children
      }),
      extractCta: true
    }
  );
  const ctaWithVariant = ctaItem ? { ...ctaItem, variant: "primary" } : null;
  return { items, ctaItem: ctaWithVariant };
}
function buildFooterMenu(navigation, routes, options) {
  const menu = getFooterNavigation(navigation);
  if (!menu) {
    return { items: [], ctaItem: null, groups: [], links: [] };
  }
  const toNavLink = (item) => {
    const renderable = deriveRenderableNavigationLink(item, options);
    if (!renderable) return null;
    return buildNavLinkFromResolvedHref(
      renderable,
      resolveHref(renderable.payload, routes, options)
    );
  };
  const built = buildNestedStructure(
    menu.items,
    {
      toLink: toNavLink,
      createDropdown: (id, label, children) => ({
        kind: "dropdown",
        id,
        label,
        children
      }),
      extractCta: false
    }
  );
  const groups = built.items.filter((item) => item.kind === "dropdown");
  const links = built.items.filter((item) => item.kind === "link");
  return { items: built.items, ctaItem: null, groups, links };
}
function resolveHref(link, routes, options) {
  if (!link) return null;
  if (link.kind === "external" || link.kind === "url") {
    return link.href || null;
  }
  if (link.kind === "portal") {
    return isPortalEnabled(options) ? resolvePortalHref(link) : null;
  }
  if (link.kind === "cart") {
    return isCommerceEnabled(options) ? resolveCheckoutHref(options) : null;
  }
  const stage = normalizeRenderStage(options?.previewStage);
  if (link.kind === "internal") {
    return resolveRouteHrefWithFallback(link.routeId, link.href ?? null, routes, stage);
  }
  if (link.kind === "page") {
    const routeIdFromPayload = readRouteIdFromLink(link);
    const routeIdFromIdentifierMap = options?.pagesByIdentifier?.[link.identifier] ?? null;
    const routeId = routeIdFromPayload ?? routeIdFromIdentifierMap;
    return resolveRouteHrefWithFallback(routeId, readHrefFromLink(link), routes, stage);
  }
  if (link.kind === "entry") {
    const routeIdFromPayload = readRouteIdFromLink(link);
    const routeIdFromIdentifierMap = readEntryRouteIdFromIdentifierMap(options, link.contentType, link.identifier);
    const routeId = routeIdFromPayload ?? routeIdFromIdentifierMap;
    return resolveRouteHrefWithFallback(routeId, readHrefFromLink(link), routes, stage);
  }
  return null;
}
function resolveNavigationLinkHref(link, routes, options) {
  return resolveHref(link, routes, options);
}
function resolveRouteHrefWithFallback(routeId, fallbackHref, routes, stage) {
  if (!routeId) {
    return fallbackHref;
  }
  const resolved = resolveRouteHrefForStage(routeId, routes, stage);
  if (resolved) {
    return resolved;
  }
  const route = routes[routeId];
  if (!route) {
    return fallbackHref;
  }
  if (!isRouteVisibleInStage(route, stage)) {
    return null;
  }
  return fallbackHref;
}
function readRouteIdFromLink(link) {
  if (!("routeId" in link) || typeof link.routeId !== "string") {
    return null;
  }
  return link.routeId;
}
function readHrefFromLink(link) {
  if (!("href" in link) || typeof link.href !== "string") {
    return null;
  }
  return link.href;
}
function readEntryRouteIdFromIdentifierMap(options, contentType, identifier) {
  const entryMap = options?.entriesByIdentifier?.[contentType];
  if (!entryMap) {
    return null;
  }
  return entryMap[identifier] ?? null;
}
function buildLogo(logo, fallbackAlt) {
  if (!logo || !logo.url) {
    return null;
  }
  const alt = resolveAltText(logo.alt, fallbackAlt);
  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;
}
export {
  buildFooterMenu,
  buildLogo,
  buildLogoViewModel,
  buildMenu,
  buildMenuViewModel,
  getFooterNavigation,
  getNavItemsBySlug,
  getNavigationBySlug,
  getPrimaryNavItems,
  getPrimaryNavigation,
  isNavDropdown,
  isNavLink,
  resolveNavigationLinkHref,
  transformToNavItems
};
