import{existsSync as f}from"node:fs";import n from"node:path";import{resolvePlugins as g}from"@redocly/openapi-core";import{DEFAULT_PROJECT_PLUGIN_PATHS as m,DEFAULT_THEME_PLUGIN_PATH as d,STYLES_FILE as p}from"../constants/common.js";import{GATED_PLUGINS as P}from"../constants/entitlements.js";import{isDefined as y}from"../../utils/guards/is-defined.js";import{reporter as E}from"../tools/notifiers/reporter.js";import{sha as F}from"../utils/crypto/sha.js";import{resolveModule as C}from"./resolve-module.js";import{EntitlementsProvider as D}from"../entitlements/entitlements-provider.js";const u={sha:"",value:Promise.resolve({resolvedPlugins:[],errors:[]})};async function M(e,t=[]){const r=F(JSON.stringify(t));if(u.sha!==r){u.sha=r;const s=j(e,t),l=await g(s,e)||[];if(!s.some(i=>m.includes(i))){const i=m[0],a=n.join(e,i);l.push({id:"",absolutePath:a,path:i})}u.value=Promise.all(l.map(i=>A(i))).then(i=>{const a=i.filter(y),h=a.filter(c=>c?.type&&["error","warning"].includes(c.type)),v=a.filter(c=>c.type==="resolvedPlugin");return{errors:h,resolvedPlugins:v}})}const o=await u.value;if(o.errors.length)for(let s=0;s<o.errors.length;s++)await E.panicOnBuildContentError(o.errors[s].error);return o.resolvedPlugins}function j(e,t=[]){const r=[...t];for(const o of m)!r.includes(o)&&f(n.join(e,o))&&r.push(o);return r.includes(d)||r.unshift(d),r}async function A(e){if(!e.absolutePath||!e.path)return{type:"error",error:`Plugin with path "${e}" not found`};const t=n.dirname(e.absolutePath),{lifecyclePlugin:r,config:o}=L(e);return{name:n.dirname(e.path),type:"resolvedPlugin",isNpmPackage:I(e.path),pluginDirAbsolutePath:t,lifecyclePlugin:r,config:o,stylesFile:n.resolve(t,p),markdoc:S(t)}}function I(e){const t=n.dirname(e);return C(n.join(t,"package.json"))!=null}function L(e){const t=D.instance(),r=e.path||"",o=!t.canAccessFeature("customPlugins")&&!P.includes(r),s=e.requiredEntitlements&&P.includes(r)&&!e.requiredEntitlements.every(l=>t.canAccessFeature(l));return o||s?{lifecyclePlugin:{id:e.id},config:{}}:{lifecyclePlugin:{id:e.id,processContent:e.processContent,afterRoutesCreated:e.afterRoutesCreated,loaders:e.loaders,requiredEntitlements:e.requiredEntitlements},config:{schema:e.redoclyConfigSchema,ejectIgnore:e.ejectIgnore}}}function S(e){const t=n.resolve(e,"markdoc"),r=n.join(t,"schema.ts");if(!f(r))return null;const o=n.join(t,"components.tsx");return{schemaFile:r,componentsFile:f(o)?o:void 0}}export{M as resolveExternalPlugins};
