import*as i from"path";import{existsSync as d,readFileSync as v}from"fs";import{copyStaticFile as S,reporter as w}from"../../utils/index.js";import{USER_THEME_ALIAS as E}from"../../config/constants.js";import{setMapValue as m}from"../../utils/map.js";import{reportEsbuildError as P}from"../esbuild-logger.js";import{isDevelopMode as x}from"../../utils/is-develop-mode.js";import{isLocalLink as A}from"../../../shared/utils.js";const f=/\.(gif|jpe?g|tiff?|png|webp|bmp|eot|otf|woff|woff2|ttf|pdf|svg|mp4|webm)$/i,D=["require-call","dynamic-import"],L=(u,n)=>{const l=i.resolve(n,E),t=new Map,r=new Map;return{name:"esbuild-redocly-custom-assets-resolver",setup:a=>{a.onStart(()=>{r.clear(),w.clearEsbuildProblems()}),a.onResolve({filter:f},e=>{if(!A(e.path))return{external:!0};const o=i.resolve(e.resolveDir,e.path);if(!d(o)){if(x()&&!D.includes(e.kind)){r.has(e.importer)||r.set(e.importer,v(e.importer,"utf-8"));const p=r.get(e.importer),h=i.relative(n,e.importer);return P(`Could not resolve asset: ${e.path}`,h,p,{lineText:e.path}),{external:!0}}return}const s=e.kind+":"+o;if(t.has(s))return t.get(s);const c=S(e.resolveDir,o,u.outdir);if(e.kind==="url-token")return m(t,s,{external:!0,path:c,watchDirs:[l]});{const p=i.relative(n,o);return m(t,s,{namespace:"assets-ns",path:p,pluginData:{assetPath:c},watchDirs:[l]})}}),a.onLoad({filter:f,namespace:"assets-ns"},({pluginData:e})=>({contents:e.assetPath,loader:"text"}))}}};export{L as AssetsResolver};
