import*as i from"path";import{existsSync as d,readFileSync as v}from"fs";import{USER_THEME_ALIAS as w}from"../../../constants/common.js";import{setMapValue as m}from"../../../utils/collection/set-map-value.js";import{isLocalLink as S}from"../../../utils/path/is-local-link.js";import{reporter as E}from"../../tools/notifiers/reporter.js";import{envConfig as P}from"../../config/env-config.js";import{copyStaticFile as x}from"../../utils/index.js";import{reportEsbuildError as y}from"../esbuild-logger.js";const f=/\.(gif|jpe?g|tiff?|png|webp|bmp|eot|otf|woff|woff2|ttf|pdf|svg|mp4|webm)$/i,A=["require-call","dynamic-import"],L=(u,n)=>{const l=i.resolve(n,w),t=new Map,r=new Map;return{name:"esbuild-redocly-custom-assets-resolver",setup:a=>{a.onStart(()=>{r.clear(),E.clearEsbuildProblems()}),a.onResolve({filter:f},async e=>{if(!S(e.path))return{external:!0};const o=i.resolve(e.resolveDir,e.path);if(!d(o)){if(P.isDevelopMode&&!A.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 y(`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=await x(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};
