import*as c from"node:path";import{CONFIG_FILE_NAME as $}from"../../../constants/common.js";import{DEFAULT_THEME_NAME as D,USER_THEME_ALIAS as v}from"../../../constants/common.js";import{isDefined as u}from"../../../utils/guards/is-defined.js";import{slash as M}from"../../../utils/path/slash.js";import{resolveExternalPlugins as p}from"../../external-plugins/resolve-external-plugins.js";import{generateMarkdocComponentsEntry as R,generateMarkdocSchemasEntry as T}from"./codegen/index.js";const d=new Map,N=(a,n)=>{const h=c.resolve(n,v),i=c.resolve(n,$),k=["components","core","layouts","markdoc","icons","config","ext"],w="@redocly\\/theme\\/",E=k.map(t=>`${w}${t}`).join("|"),F=new RegExp(`^(@redocly/theme$|${E})`);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:T(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:R(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("${M(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:F},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(D,"")||"index";for(let m=l.length-1;m>=0;m--){const x=l[m].isNpmPackage?[c.join(l[m].name,"src",f),{...r,resolveDir:n}]:[`./${f}`,{...r,resolveDir:l[m].pluginDirAbsolutePath}],y=A(await t.resolve(...x));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};
