"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getTagsWithOperations=exports.getTagGroupsItems=exports.getTagsItems=void 0;const group_1=require("../../models/group"),constants_1=require("../../constants"),utils_1=require("../../utils"),operation_1=require("./operation"),markdown_1=require("./markdown");function getTagsItems(e,t,s,o,n){let a;if(a=void 0===o?Object.keys(t):o.tags,!Array.isArray(a))return console.warn("Unexpected values of tags. Check tags or x-tagGroups in your definition."),[];const r=a.map((e=>t[e]?(t[e].used=!0,t[e]):(console.warn(`Non-existing tag "${e}" is added to the group "${null==o?void 0:o.name}"`),null))),i=[];for(const t of r){if(!t)continue;const o=(0,group_1.getTagOrGroup)("tag",t,s);if(o.depth=constants_1.GROUP_DEPTH+1,""===t.name){const e=[...(0,markdown_1.addMarkdownItems)((0,utils_1.getValueFromMdParsedExtension)(t,"description")||"",o,o.depth+1),...(0,operation_1.getOperationsItems)(void 0,t,o.depth+1)];i.push(...e);continue}const a=getTagRelatedSchema({definition:e.definition,tag:t,parent:o,schemaDefinitionsTagName:n.schemaDefinitionsTagName});o.items=[...a,...(0,markdown_1.addMarkdownItems)((0,utils_1.getValueFromMdParsedExtension)(t,"description")||"",o,o.depth+1),...(0,operation_1.getOperationsItems)(o,t,o.depth+1)],i.push(o)}return i}function getTagGroupsItems(e,t,s,o,n){const a=[];for(const r of s){const s=(0,group_1.getTagOrGroup)("group",r,t);s.depth=constants_1.GROUP_DEPTH,s.items=getTagsItems(e,o,s,r,n),a.push(s)}return a}function getTagRelatedSchema({definition:e,tag:t,parent:s,schemaDefinitionsTagName:o}){var n;const a=o?[o]:[];return Object.entries((null===(n=e.components)||void 0===n?void 0:n.schemas)||{}).map((([e,o])=>{if(!(o["x-tags"]||a).includes(t.name))return null;const n=(0,group_1.getTagOrGroup)("schema",{name:e,"x-displayName":`${o.title||e}`,description:`{% schemaDefinition showWriteOnly="true" schemaRef="#/components/schemas/${e}" /%}`,isSchema:!0,level:2},s);return n.depth=s.depth+1,n})).filter(Boolean)}function getTagsWithOperations(e,t){const{definition:s}=e,o={},n=s["x-webhooks"]||s.webhooks;for(const e of t||[])o[e.name]=Object.assign(Object.assign({},e),{operations:[]});return s.paths&&getTags(e,s.paths,o),n&&getTags(e,n,o,!0),o}function getTags(e,t,s,o){for(const n of Object.keys(t||{})){const a=t[n],r=Object.keys(a).filter(utils_1.isOperationName);for(const t of r){const r=a[t];if(a.$ref){const{resolved:t}=e.deref(a);getTags(e,{[n]:t},s,o);continue}let i=null==r?void 0:r.tags;i&&i.length||(i=[""]);for(const e of i){let i=s[e];void 0===i&&(i={name:e,operations:[]},s[e]=i),i["x-traitTag"]||i.operations.push(Object.assign(Object.assign({},r),{pathName:n,pointer:utils_1.JsonPointer.compile(["paths",n,t]),httpVerb:t,pathParameters:a.parameters||[],pathServers:a.servers,isWebhook:!!o}))}}}}exports.getTagsItems=getTagsItems,exports.getTagGroupsItems=getTagGroupsItems,exports.getTagsWithOperations=getTagsWithOperations;
//# sourceMappingURL=tags.js.map