import{useLocation as D}from"react-router-dom";import{useEffect as _,useMemo as f,useState as b}from"react";import{useActiveSectionId as k}from"@redocly/theme/core/hooks";import{getMenuItemType as C}from"@redocly/theme/core/utils";import{MenuItemType as T}from"@redocly/theme/core/constants";import{usePageSharedData as y,useSidebarItemsData as h,useSidebarConnectedCatalogConfig as G,useSidebarConnectedCatalogEntity as E}from"../../providers/page-data/hooks";import{mapItemsPropsToState as g}from"./utils";import{injectCatalogItems as F}from"../../utils/catalog/inject-catalog-items";import{CATALOG_BASE_SLUG as O,CATALOG_SLUGS as B}from"../../../server/constants/plugins/catalog-entities";const K="catalog.backToAllLabel",M=`${O}/${B.ALL}`;function N(o){const n=h();return f(()=>p(n,o),[n,o])}function H(o){const n=D(),t=k(n,!0),e=E(),I=G(),l=N(o),c=e?F(l,e,I?.slug):l,[u,v]=b(null),s=f(()=>g(c,n,t),[t,n,c]),a=y("current-catalog-info"),A=f(()=>S(s),[c]),[r,d]=b(A),i=r.length>0?r[r.length-1]:void 0,L=i&&g([...i.item.link?[{...i.item,items:[],menuStyle:void 0,separatorLine:!1}]:[],...i.item.items],n,t);_(()=>{if(!u||u!==t){const m=S(s);d(m),v(t)}},[l,n,t,u]);const w=i?{slug:i.previousLink,event:"sidebar_drilldown_back_button.clicked"}:e?{label:"Back to Catalog",labelTranslationKey:K,slug:M,event:"back_to_catalog_button.clicked"}:a?{label:a.catalog.label,labelTranslationKey:a.catalog.labelTranslationKey,slug:a.catalog.link,event:"back_to_catalog_button.clicked"}:void 0;return{currentItems:L||s,allSidebarItems:s,popDrilldownState:()=>{r.length>0&&d(r.slice(0,r.length-1))},pushDrilldownState:m=>{d([...r,m])},backLink:w}}function p(o,n){const t={};for(const e of o)e.versionFolderId&&e.version&&(t[e.versionFolderId]=t[e.versionFolderId]??[]).push(e.version);return o.filter(e=>e.version==null?e:n?e.version===n.version?!0:e.versionFolderId&&t[e.versionFolderId]?.includes(n.version)?!1:e.isDefault:e.isDefault).map(e=>Array.isArray(e.items)&&o.length?{...e,items:p(e.items,n)}:e)}function S(o,n=[]){for(const t of o){if(!t.active&&!t.hasActiveSubItem)continue;if(C(t)===T.DrillDown&&n.push({item:t}),t.active)return n;if(t.hasActiveSubItem)return S(t.items,n)}return n}export{N as useRawSidebarItems,H as useSidebarItems};
