import{useMemo as k}from"react";import{useLocation as T}from"react-router-dom";import{useThemeConfig as y}from"@redocly/theme/core/hooks";import{withoutPathPrefix as K}from"@redocly/theme/core/utils";import{usePageVersions as g,useTranslate as v,usePageSharedData as d}from"../hooks";import{useSidebarItems as x}from"./useSidebarItems";function D(){const a=d("sidebar")?.relatedNavbarItem,n=d("current-catalog-info"),r=y(),{translate:l}=v(),{versions:e=[]}=g()||{},c=e.find(m=>m?.active),{allSidebarItems:i}=x(c),t=T().pathname,o=n?.item,s=n?.catalog,b=r.breadcrumbs?.prefixItems,f=r.breadcrumbs?.hide;return k(()=>{const m=B({allSidebarItems:i,pathname:K(t),catalogItemInfo:o,relatedNavbarItem:a,catalogInfo:s,prefixItems:b,hide:f,translate:l}),p=S(i,m);return{breadcrumbs:m,currentItemSiblings:p}},[t,a,i,o,s,b,f,l])}const B=({allSidebarItems:a=[],pathname:n,catalogItemInfo:r,relatedNavbarItem:l,catalogInfo:e,prefixItems:c,hide:i,translate:u})=>{if(i===!0)return[];const t=h(a);if(t.length===0)return[];if(r){const o=t[t.length-1];o&&o.label!==r.label&&t.unshift({label:r.label,link:r.link})}if(l&&l.label!==t[0].label&&t.unshift(l),e&&t.unshift({label:u(e.labelTranslationKey,e.label),labelTranslationKey:e.labelTranslationKey,link:e.link,icon:e.icon}),c?.length){const o=c.map(s=>({label:u(s.labelTranslationKey,s.label),labelTranslationKey:s.labelTranslationKey,link:s.link,icon:s.icon}));t.unshift(...o)}return t.length===1&&(!t[0]?.link||t[0]?.link===n)?[]:t},S=(a,n)=>{if(n.length===0)return[];const r=(e,c)=>{for(const i of e){if(i.active)return{item:i,parent:c};if(i.items?.length){const u=r(i.items,i);if(u)return u}}},l=r(a);return l?.parent?l.parent.items?.filter(e=>!!(e.link&&e.link!==l.item.link&&e.label)).map(e=>({label:e.label,labelTranslationKey:e.labelTranslationKey,link:e.link,icon:e.icon}))||[]:[]},h=a=>{const n=a.find(({active:l,hasActiveSubItem:e})=>l||e);if(!n?.label)return[];const r={label:n.label,labelTranslationKey:n.labelTranslationKey,link:n.link,icon:n.icon};return n.type==="group"&&n.selectFirstItemOnExpand&&n.items?.[0]?.link&&(r.link=n.items[0].link),n.items?.length||(r.link=void 0),[r,...h(n.items||[])]};export{B as generateBreadcrumbs,S as generateSiblings,D as useBreadcrumbs};
