import{getTagOrGroup as d}from"../../models/group.js";import{DEFAULT_WEBHOOKS_TAG_NAME as g,GROUP_DEPTH as h}from"../../constants.js";import{getValueFromMdParsedExtension as T,isOperationName as S,JsonPointer as w,safeSlugify as k}from"../../utils/index.js";import{getOperationsItems as x}from"./operation.js";import{addMarkdownItems as O}from"./markdown.js";import{joinWithSeparator as b}from"../history/helpers.js";const y=["tools","resources","prompts"];function v(r,f,n,o,s){let t;if(o===void 0?t=Object.keys(f):t=o.tags,!Array.isArray(t))return console.warn("Unexpected values of tags. Check tags or x-tagGroups in your definition."),[];const a=t.map(e=>f[e]?(f[e].used=!0,f[e]):(console.warn(`Non-existing tag "${e}" is added to the group "${o?.name}"`),null)),i=[];for(const e of a){if(!e)continue;const c=d("tag",e,n);if(c.depth=h+1,e.name===""){const m=[...O(T(e,"description")||"",c,c.depth+1),...x(void 0,e,c.depth+1)];i.push(...m);continue}const p=A({definition:r.definition,tag:e,parent:c,schemaDefinitionsTagName:s.schemaDefinitionsTagName}),l=E(r.definition,e,c);c.items=[...p,...l,...O(T(e,"description")||"",c,c.depth+1),...x(c,e,c.depth+1)],i.push(c)}return i.filter(({name:e,items:c})=>e!==g||c.length>0)}function I(r,f,n,o,s){const t=[];for(const a of n){const i=d("group",a,f);i.depth=h,i.items=v(r,o,i,a,s),t.push(i)}return t}function A({definition:r,tag:f,parent:n,schemaDefinitionsTagName:o}){const s=o?[o]:[];return Object.entries(r.components?.schemas||{}).map(([t,a])=>{if(!(a["x-tags"]||s).includes(f.name))return null;const e=d("schema",{name:t,"x-displayName":`${a.title||t}`,description:`{% schemaDefinition showWriteOnly="true" schemaRef="#/components/schemas/${t}" /%}`,isSchema:!0,level:2},n);return e.depth=n.depth+1,e}).filter(Boolean)}function E(r,f,n){const o=[];for(const s of y)if(r["x-mcp"]?.[s]){const t=P(s),a=[t];for(const i of r["x-mcp"]?.[s]||[])if((i.tags?.length?i.tags:a).includes(f.name)){const c=b(n.id,k(i.name)),p=s==="tools"?`Tool name: \`${i.name}\`

`:"",m=d(s==="tools"?"tool":s==="resources"?"rsrc":"prompt",{name:i.name,"x-displayName":i.title||i.name,description:`${p}${i.description||""}
{% mcp${t.slice(0,-1)} name="${i.name}" id="${c}" /%}`,isSchema:!0,level:2},n);m.depth=n.depth+1,o.push(m)}}return o}function M(r,f){const{definition:n}=r,o={},s=n["x-webhooks"]||n.webhooks,t=n["x-mcp"];for(const a of f||[])o[a.name]={...a,operations:[]};return n.paths&&u(r,n.paths,o),s&&u(r,s,o,!0),t&&j(t,o),o}function j(r,f){for(const n of y)if(r[n])for(const o of r[n]){const s=o.tags&&o.tags.length?o.tags:[P(n)];for(const t of s){let a=f[t];a===void 0&&(a={name:t,operations:[],used:!0},f[t]=a)}}}function $(r,f,n,o,s,t,a,i){if(s.$ref){const{resolved:c}=r.deref(s);u(r,{[o]:c},t,a);return}let e=n?.tags;(!e||!e.length)&&(e=a?[g]:[""]);for(const c of e){let p=t[c];p===void 0&&(p={name:c,operations:[]},t[c]=p),!p["x-traitTag"]&&p.operations.push({...n,pathName:o,pointer:w.compile(["paths",o,f]),httpVerb:f,pathParameters:s.parameters||[],pathServers:s.servers,isWebhook:!!a,isAdditionalOperation:!!i,keywords:n["x-keywords"]})}}function u(r,f,n,o){for(const s of Object.keys(f||{})){const t=f[s],a=Object.keys(t).filter(S);for(const i of a){const e=t[i];$(r,i,e,s,t,n,o)}if(t.additionalOperations)for(const[i,e]of Object.entries(t.additionalOperations))$(r,i,e,s,t,n,o,!0)}}function P(r){return r.charAt(0).toUpperCase()+r.slice(1)}export{P as capitalize,I as getTagGroupsItems,v as getTagsItems,M as getTagsWithOperations,$ as processOperation};
