import{BaseResolver as i,ResolveError as h,Source as n,isAbsoluteUrl as l,readFileFromUrl as m}from"@redocly/openapi-core";import f from"path";import{slash as p}from"../../../utils/path/slash.js";const d=/^https?:\/\/.*\/_bundle\/(.*\.(?:yaml|json)$)/;class v extends i{fs;constructor(t){super(),this.fs=t}async resolveDocument(t,s,e=!1){const r=this.resolveExternalRef(t,s),a=this.cache.get(r);if(a)return a;const c=this.loadExternalRef(r).then(o=>this.parseDocument(o,e));return this.cache.set(r,c),c}async loadExternalRef(t){try{const s=t.match(d);if(s){const[,e]=s;if(await this.fs.exists(e)){const r=await this.fs.read(e);return new n(e,r.replace(/\r\n/g,`
`))}}if(l(t)){const{body:e,mimeType:r}=await m(t,this.config.http);return new n(t,e,r)}else{const e=p(f.relative(this.fs.cwd,t)),r=await this.fs.read(e);return new n(t,r.replace(/\r\n/g,`
`))}}catch(s){throw s.message=s.message.replace(", lstat",""),new h(s)}}}export{v as ExternalResolver};
