import{useLocation as w}from"react-router-dom";import{useEffect as h,useMemo as m,useState as I}from"react";import{useActiveSectionId as k}from"@redocly/theme/core/hooks";import{getMenuItemType as y}from"@redocly/theme/core/utils";import{MenuItemType as A}from"@redocly/theme/core/constants";import{usePageData as F,usePageSharedData as _,useSidebarItemsData as T}from"../../providers/page-data/hooks";import{mapItemsPropsToState as p}from"./utils";function L(r){const t=T();return m(()=>S(t,r),[t,r])}function N(r){const t=w(),n=F(),e=k(t,!0),s=L(r),[u,v]=I(null),l=m(()=>p(s,t,e,n?.templateId),[e,t,n?.templateId,s]),a=_("current-catalog-info"),b=m(()=>f(l),[s]),[o,c]=I(b),i=o.length>0?o[o.length-1]:void 0,D=i&&p([...i.item.link?[{...i.item,items:[],menuStyle:void 0,separatorLine:!1}]:[],...i.item.items],t,e,n?.templateId);h(()=>{if(!u||u!==e){const d=f(l);c(d),v(e)}},[s,t,e,u]);const g=i?{slug:i.previousLink,event:"sidebar_drilldown_back_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:D||l,allSidebarItems:l,popDrilldownState:()=>{o.length>0&&c(o.slice(0,o.length-1))},pushDrilldownState:d=>{c([...o,d])},backLink:g}}function S(r,t){const n={};for(const e of r)e.versionFolderId&&e.version&&(n[e.versionFolderId]=n[e.versionFolderId]??[]).push(e.version);return r.filter(e=>e.version==null?e:t?e.version===t.version?!0:e.versionFolderId&&n[e.versionFolderId]?.includes(t.version)?!1:e.isDefault:e.isDefault).map(e=>Array.isArray(e.items)&&r.length?{...e,items:S(e.items,t)}:e)}function f(r,t=[]){for(const n of r){if(!n.active&&!n.hasActiveSubItem)continue;if(y(n)===A.DrillDown&&t.push({item:n}),n.active)return t;if(n.hasActiveSubItem)return f(n.items,t)}return t}export{L as useRawSidebarItems,N as useSidebarItems};
