import*as c from"path";import{convertSwagger2OpenAPI as Q}from"@redocly/openapi-docs/lib/utils/loadAndBundleSpec.js";import{bundle as W,createConfig as Y,getTotals as Z}from"@redocly/openapi-core";import R from"@redocly/openapi-docs";import{combineUrls as j}from"@redocly/theme/core/utils";import{CONFIG_FILE_NAME as B}from"../../../constants/index.js";import{PUBLIC_API_DEFINITIONS_FOLDER as L}from"../../constants/common.js";import{logger as x,reporter as P}from"../../utils/index.js";import{pluralize as k}from"../../../shared/utils.js";import{injectDecoratorIntoConfig as oo}from"./decorators.js";import{getAllApiConfigsByPath as no}from"../get-api-config.js";import{deepMerge as M}from"../../../shared/jsUtils.js";import{normalizeFeedbackOptions as eo}from"./utils.js";import{replaceFileExtension as T}from"./store-definition-bundles.js";import{ExternalResolver as ro}from"../../fs/utils/external-ref-resolver.js";import{MAX_BUNDLING_ERRORS_TO_PRINT as to}from"./constants.js";async function $o(o,{fs:e,cache:g,getConfig:p,isPathIgnored:w}){if(c.posix.basename(o)===B)return[];let f;try{f=(await g.load(o,"yaml")).data}catch{return[]}if(!f?.openapi&&!f?.swagger)return x.verbose(`${o} file is not definition. Skipping`),[];const l=await p("."),m=await e.getFileInfo(o),n=await p(c.posix.dirname(o)),u=no(n?.apis,o,n.configPath),E=[],U=await w(o);for(const a of u){if(!a.output&&U)continue;const i=M(n,{rbac:void 0},a);if(i.decorators&&typeof i.decorators!="object"){P.panicOnBuildContentError(`'decorators' must be an object at redocly.yaml, got '${typeof i.decorators}'`);continue}if(i.plugins&&!Array.isArray(i.plugins)){P.panicOnBuildContentError(`'plugins' must be an array at redocly.yaml, got '${typeof i.plugins}'`);continue}const z=n.configPath?c.posix.dirname(n.configPath):".",r=a.output?c.posix.join(z,a.output):o;m&&a.output&&!await e.exists(r)&&e.addVirtualFile(r,m);const S=[],V=c.resolve(e.cwd,n.realConfigPath||B),A=new ro(e),G=await Y(oo(i,r,S),{configPath:V,externalRefResolver:A}),D=c.resolve(e.cwd,o),J={config:G,base:c.dirname(D),doc:{source:{absoluteRef:D},parsed:JSON.parse(JSON.stringify(f))},externalRefResolver:A},{bundle:{parsed:b},problems:_}=await W(J),t=Z(_);let v=[t.errors&&`${t.errors} ${k(t.errors,"error","errors")}`,t.warnings&&`${t.warnings} ${k(t.warnings,"warning","warnings")}`,t.ignored&&`${t.ignored} ignored`].filter(Boolean).join(", ");if(t.errors){const d=_.filter(s=>s.severity==="error");let $=`${v} while bundling ${r} definition:
`;const C=Math.min(d.length,to);for(let s=0;s<C;s++){const N=d[s];$+=`${N.message} at ${N.location?.[0]?.pointer??""}
`}if(d.length>C){const s=d.length-C;$+=`... and ${s} more ${k(s,"error","errors")}`}x.panic($)}t.warnings&&x.warn(`${v} while bundling %rp definition`,r);const h=io(b.swagger!==void 0?await Q(b):b,n.metadata),O=M(l.openapi||{},n.openapi||{},a.openapi||a?.theme?.openapi||{}),H=h["x-feedback"]||O.feedback||n.feedback,F={...O,feedback:eo(H),hideSidebar:!0,disableRouter:!0,mockServer:i?.mockServer||l?.mockServer,downloadUrls:O.downloadUrls||[{url:j(L,`${T(r,".json")}?download`)},{url:j(L,`${T(r,".yaml")}?download`)}]},y=R.normalizeOptions(F);let I;try{I=new R.OpenAPIParser(h,void 0,y)}catch(d){P.panicOnBuild(d);continue}const{contentItems:X,flatItems:q}=R.buildContentItems(I,y),K={definition:h,config:i,relativePath:r,realRelativePath:m?.realRelativePath||r,customOutputRelativeFile:a.output&&r,markdocChunks:S,contentItems:X,flatItems:q,options:y,rawOptions:F,parser:I};E.push(K)}return E}async function Co(o,e){const{fs:g,cache:p,getConfig:w}=e,f=[];for(const l of await g.scan(/(\.ya?ml|\.json)$/)){if(!await w(c.posix.dirname(l.relativePath)))continue;const n=await p.load(l.relativePath,"load-oas");n.data&&n.data.length&&f.push(...n.data.map(u=>({...u,isVirtual:l.isVirtual||u.isVirtual,hash:n.compoundHash})))}return f}function io(o,e){return e?{...o,info:{...o.info,"x-metadata":{...o.info["x-metadata"],...e}}}:o}export{$o as definitionLoader,Co as definitionsLoader};
