import{useMemo as b}from"react";import{useLocation as h}from"react-router-dom";import{useActiveSectionId as I}from"@redocly/theme/core/openapi";import{pathMatchedLink as k}from"../../../utils/index.js";import{DEFAULT_WEBHOOKS_TAG_NAME as A}from"../../../constants.js";import{joinWithSeparator as y}from"../../../services/history/helpers.js";function L({items:s,routingBasePath:r}){const t=h(),o=I(t,!1,!1);return b(()=>S(s,o,t,r),[o,s,t,r])}function S(s,r="",t,o){const n=[];return c({contentItems:s,sidebarItems:n,activeItemId:r,location:t,routingBasePath:o}),n}function d(s,r="",t,o){return s.href?r&&o?y(o,s.id)===r:r&&!o?s.id===r:k(t,s.href):!1}function l(s,r="",t,o){return(s.items||[]).some(i=>d(i,r,t,o)||l(i,r,t,o))}function c(s){const{contentItems:r,sidebarItems:t,activeItemId:o,location:n,routingBasePath:i}=s;let p=!0;for(const e of r){const a=d(e,o,n,i),m=l(e,o,n,i);switch(e.type){case"group":t.push({type:"separator",label:e.name,active:a,hasActiveSubItem:m}),c({...s,contentItems:e.items,sidebarItems:t});break;case"tag":const f={type:"group",label:e.name,items:[],link:e.href,active:a,hasActiveSubItem:m,modified:!0};t.push(f),c({contentItems:e.items,sidebarItems:f.items||[],activeItemId:o,location:n,routingBasePath:i});break;case"operation":case"schema":case"tool":case"rsrc":case"prompt":e.isWebhook&&p&&e.parent.name!==A&&(t.push({type:"separator",label:"Webhooks",variant:"secondary"}),p=!1),t.push({type:"link",label:e.name,httpVerb:e.type=="operation"?e.httpVerb:e.type,link:e.href,badges:e.badges,active:a,hasActiveSubItem:m,modified:!0,deprecated:e.deprecated,isAdditionalOperation:e.isAdditionalOperation});break;case"section":if(e.depth===-1)continue;const u={type:e.items.length?"group":"link",label:e.name,link:e.href,items:[],active:a,hasActiveSubItem:m,modified:!0};t.push(u),e.items.length&&c({contentItems:e.items,sidebarItems:u.items,activeItemId:o,location:n,routingBasePath:i});break}}}export{L as useMenuItems};
