import{getTagOrGroup as l}from"../../models/group.js";import{DEFAULT_WEBHOOKS_TAG_NAME as O,GROUP_DEPTH as y,MAX_NESTING_TAGS_LEVEL as _}from"../../constants.js";import{getValueFromMdParsedExtension as h,isOperationName as j,JsonPointer as D,safeSlugify as G}from"../../utils/index.js";import{getOperationsItems as g}from"./operation.js";import{addMarkdownItems as T}from"./markdown.js";import{joinWithSeparator as I}from"../history/helpers.js";const N=["tools","resources","prompts"];function R(r,c,i,o,s){let t;if(o===void 0?t=Object.keys(c):t=o.tags,!Array.isArray(t))return console.warn("Unexpected values of tags. Check tags or x-tagGroups in your definition."),[];const f=t.map(e=>c[e]?(c[e].used=!0,c[e]):(console.warn(`Non-existing tag "${e}" is added to the group "${o?.name}"`),null)),a=[];for(const e of f){if(!e)continue;const n=l("tag",e,i);if(n.depth=y+1,e.name===""){const d=[...T(h(e,"description")||"",n,n.depth+1),...g(void 0,e,n.depth+1,c)];a.push(...d);continue}const m=P({definition:r.definition,tag:e,parent:n,schemaDefinitionsTagName:s.schemaDefinitionsTagName}),u=w(r.definition,e,n);n.items=[...m,...u,...T(h(e,"description")||"",n,n.depth+1),...g(n,e,n.depth+1,c)],a.push(n)}return a.filter(({name:e,items:n})=>e!==O||n.length>0)}function L(r,c,i,o,s){const t=[];for(const f of i){const a=l("group",f,c);a.depth=y,a.items=R(r,o,a,f,s),t.push(a)}return t}function P({definition:r,tag:c,parent:i,schemaDefinitionsTagName:o}){const s=o?[o]:[];return Object.entries(r.components?.schemas||{}).map(([t,f])=>{if(!(f["x-tags"]||s).includes(c.name))return null;const e=l("schema",{name:t,"x-displayName":`${f.title||t}`,description:`{% schemaDefinition showWriteOnly="true" schemaRef="#/components/schemas/${t}" /%}`,isSchema:!0,level:2},i);return e.depth=i.depth+1,e}).filter(Boolean)}function w(r,c,i){const o=[];for(const s of N)if(r["x-mcp"]?.[s]){const t=b(s),f=[t];for(const a of r["x-mcp"]?.[s]||[])if((a.tags?.length?a.tags:f).includes(c.name)){const n=I(i.id,G(a.name)),m=s==="tools"?"tool":s==="resources"?"rsrc":"prompt",u=s==="tools"?"":`${a.description||""}
`,d=l(m,{name:a.name,"x-displayName":a.title||a.name,description:`${u}{% mcp${t.slice(0,-1)} name="${a.name}" id="${n}" /%}`,isSchema:!0,level:2},i);d.depth=i.depth+1,o.push(d)}}return o}function W(r,c){const{definition:i}=r,o={},s=i["x-webhooks"]||i.webhooks,t=i["x-mcp"];for(const f of c||[])o[f.name]={...f,operations:[]};return i.paths&&S(r,i.paths,o),s&&S(r,s,o,!0),t&&U(t,o),o}function U(r,c){for(const i of N)if(r[i])for(const o of r[i]){const s=o.tags&&o.tags.length?o.tags:[b(i)];for(const t of s){let f=c[t];f===void 0&&(f={name:t,operations:[],used:!0},c[t]=f)}}}function E(r,c,i,o,s,t,f,a){if(s.$ref){const{resolved:n}=r.deref(s);S(r,{[o]:n},t,f);return}let e=i?.tags;(!e||!e.length)&&(e=f?[O]:[""]);for(const n of e){let m=t[n];m===void 0&&(m={name:n,operations:[]},t[n]=m),!m["x-traitTag"]&&m.operations.push({...i,pathName:o,pointer:D.compile(["paths",o,c]),httpVerb:c==="x-query"?"query":c,pathParameters:s.parameters||[],pathServers:s.servers,isWebhook:!!f,isAdditionalOperation:!!a,keywords:i["x-keywords"]})}}function S(r,c,i,o){for(const s of Object.keys(c||{})){const t=c[s],f=Object.keys(t).filter(j);for(const a of f){const e=t[a];E(r,a,e,s,t,i,o)}if(t.additionalOperations)for(const[a,e]of Object.entries(t.additionalOperations))E(r,a,e,s,t,i,o,!0)}}function z(r,c,i,o){const s=Object.values(c).filter(e=>{const n=e.kind;return n===void 0||n==="nav"}),t=new Map;s.forEach(e=>{t.set(e.name,e)});const f=s.filter(e=>{const n=e.parent;return!n||!t.has(n)}),a=(e,n,m=1)=>{if(m>_)return console.warn("The tags exceed the maximum nesting level."),[];const u=[];for(const d of e){const p=l("tag",d,n);if(p.depth=(n?.depth||y)+1,d.name===""){const x=[...T(h(d,"description")||"",p,p.depth+1),...g(void 0,d,p.depth+1,c)];u.push(...x);continue}const k=s.filter(x=>x.parent===d.name),$=P({definition:r.definition,tag:d,parent:p,schemaDefinitionsTagName:o.schemaDefinitionsTagName}),v=w(r.definition,d,p),A=a(k,p,m+1);p.items=[...$,...v,...T(h(d,"description")||"",p,p.depth+1),...g(p,d,p.depth+1,c),...A],u.push(p)}return u};return a(f,i,1).filter(({name:e,items:n})=>e!==O||n.length>0)}function b(r){return r.charAt(0).toUpperCase()+r.slice(1)}export{z as buildNestedTagsItems,b as capitalize,L as getTagGroupsItems,R as getTagsItems,W as getTagsWithOperations,E as processOperation};
