import*as i from"path";import{existsSync as d,readFileSync as v}from"fs";import{USER_THEME_ALIAS as S}from"../../../constants/common.js";import{setMapValue as c}from"../../../utils/collection/set-map-value.js";import{isLocalLink as w}from"../../../utils/path/is-local-link.js";import{reporter as E}from"../../tools/notifiers/reporter.js";import{isDevelopMode as P}from"../../utils/envs/is-develop-mode.js";import{copyStaticFile as x}from"../../utils/index.js";import{reportEsbuildError as A}from"../esbuild-logger.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"],_=(u,n)=>{const l=i.resolve(n,S),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},e=>{if(!w(e.path))return{external:!0};const o=i.resolve(e.resolveDir,e.path);if(!d(o)){if(P()&&!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 A(`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 m=x(e.resolveDir,o,u.outdir);if(e.kind==="url-token")return c(t,s,{external:!0,path:m,watchDirs:[l]});{const p=i.relative(n,o);return c(t,s,{namespace:"assets-ns",path:p,pluginData:{assetPath:m},watchDirs:[l]})}}),a.onLoad({filter:f,namespace:"assets-ns"},({pluginData:e})=>({contents:e.assetPath,loader:"text"}))}}};export{_ as AssetsResolver};
