"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useMenuItems=void 0;const theme_1=require("@redocly/theme"),react_1=require("react"),react_router_dom_1=require("react-router-dom"),utils_1=require("../../../utils");function useMenuItems({items:e}){const t=(0,react_router_dom_1.useLocation)(),s=(0,theme_1.useActiveSectionId)(t,!1,!1);return(0,react_1.useMemo)((()=>mapAndSetActiveItems(e,s,t)),[s,e,t])}function mapAndSetActiveItems(e,t="",s){const i=[];return convertOpenAPIDocs2Sidebar({contentItems:e,sidebarItems:i,activeItemId:t,location:s}),i}function isItemActive(e,t="",s){return!!e.href&&(t?e.id===t:(0,utils_1.pathMatchedLink)(s,e.href))}function isSubItemActive(e,t="",s){return(e.items||[]).some((e=>isItemActive(e,t,s)||isSubItemActive(e,t,s)))}function convertOpenAPIDocs2Sidebar(e){const{contentItems:t,sidebarItems:s,activeItemId:i,location:c}=e;let n=!0;for(const a of t){const t=isItemActive(a,i,c),o=isSubItemActive(a,i,c);switch(a.type){case"group":s.push({type:"separator",label:a.name,active:t,hasActiveSubItem:o}),convertOpenAPIDocs2Sidebar(Object.assign(Object.assign({},e),{contentItems:a.items,sidebarItems:s}));break;case"tag":const r={type:"group",label:a.name,items:[],link:a.href,active:t,hasActiveSubItem:o,modified:!0};s.push(r),convertOpenAPIDocs2Sidebar({contentItems:a.items,sidebarItems:r.items||[],activeItemId:i,location:c});break;case"operation":case"schema":a.isWebhook&&n&&(s.push({type:"separator",label:"Webhooks",variant:"secondary"}),n=!1),s.push({type:"link",label:a.name,httpVerb:"schema"===a.type?"schema":a.httpVerb,link:a.href,badges:a.badges,active:t,hasActiveSubItem:o,modified:!0,deprecated:a.deprecated});break;case"section":if(-1===a.depth)continue;const m={type:a.items.length?"group":"link",label:a.name,link:a.href,items:[],active:t,hasActiveSubItem:o,modified:!0};s.push(m),a.items.length&&convertOpenAPIDocs2Sidebar({contentItems:a.items,sidebarItems:m.items,activeItemId:i,location:c})}}}exports.useMenuItems=useMenuItems;
//# sourceMappingURL=useMenuItems.js.map