import*as c from"node:path";import{CONFIG_FILE_NAME as x}from"../../../constants/index.js";import{resolvePlugins as p}from"../../config/external-plugins.js";import{slash as D}from"../../../shared/utils.js";import{generateMarkdocComponentsEntry as M,generateMarkdocSchemasEntry as R}from"./codegen/index.js";import{DEFAULT_THEME_NAME as T,USER_THEME_ALIAS as v}from"../../config/constants.js";import{isDefined as u}from"../../../shared/jsUtils.js";const d=new Map,N=(a,n)=>{const h=c.resolve(n,v),i=c.resolve(n,x),k=["components","core","layouts","markdoc","icons","config","ext"],w="@redocly\\/theme\\/",F=k.map(t=>`${w}${t}`).join("|"),E=new RegExp(`^(@redocly/theme$|${F})`);return{name:"esbuild-redocly-custom-themes-resolver",setup:t=>{t.onResolve({filter:/@redocly-markdoc\/schema/},async e=>{const s=(await p(n,a.config.plugins)).flatMap(r=>r.markdoc?.schemaFile).filter(u);return{namespace:"markdoc-schema-ns",path:e.path,watchFiles:[...s,i],watchDirs:[c.resolve(h,"markdoc")],pluginData:{schemaFiles:s}}}),t.onLoad({filter:/@redocly-markdoc\/schema/,namespace:"markdoc-schema-ns"},({pluginData:e})=>({contents:R(e),resolveDir:"/",loader:"ts"})),t.onResolve({filter:/@redocly-markdoc\/components/},async e=>{const s=[...(await p(n,a.config.plugins)).map(r=>r.markdoc?.componentsFile).filter(u)];return{namespace:"markdoc-schema-ns",path:e.path,watchFiles:[...s,i],pluginData:{componentsFiles:s},watchDirs:[c.resolve(h,"markdoc")]}}),t.onLoad({filter:/@redocly-markdoc\/components/,namespace:"markdoc-schema-ns"},({pluginData:e})=>({contents:M(e),resolveDir:"/",loader:"ts"})),t.onResolve({filter:/@styles/},async e=>{const s=(await p(n,a.config.plugins)).map(r=>r.stylesFile).filter(Boolean);return{namespace:"user-styles-ns",path:e.path,watchFiles:[...s,i],pluginData:{styleFiles:s},watchDirs:[h]}}),t.onLoad({filter:/@styles/,namespace:"user-styles-ns"},async({pluginData:e})=>{const{styleFiles:o}=e;return{contents:o.map(r=>`@import url("${D(c.relative(n,r))}");`).join(`
`),loader:"css",resolveDir:n}}),t.onResolve({filter:/(^|\/)styles\.css$/},async({path:e,...o})=>{if(o.pluginData==="missing-styles-ns")return;const s=await t.resolve(e,{...o,pluginData:"missing-styles-ns"});return s.errors.length&&s.errors.some(r=>r.text.match(/^Could not resolve .*?\bstyles.css\b/))?{namespace:"missing-styles-ns",path:e}:s}),t.onLoad({filter:/(^|\/)styles\.css$/,namespace:"missing-styles-ns"},async()=>({contents:"",loader:"css"})),t.onResolve({filter:E},async e=>{if(e.resolveDir==="")return;const o=`${e.path}:${e.importer}`;if(d.has(o))return d.get(o);const{path:s,...r}=e,l=await p(n,a.config.plugins),f=e.path.replace(T,"")||"index";for(let m=l.length-1;m>=0;m--){const $=l[m].isNpmPackage?[c.join(l[m].name,"src",f),{...r,resolveDir:n}]:[`./${f}`,{...r,resolveDir:l[m].pluginDirAbsolutePath}],y=A(await t.resolve(...$));if("path"in y&&y.path!==e.importer){const g={...y,watchFiles:[i,c.join(n,v,f+".tsx")]};return l[m].name===v||d.set(o,g),g}}})}}};function A(a){return a.errors.length?{errors:a.errors}:a}export{N as ThemesResolver,d as resolveCache};
