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