import{__exports as t}from"../../../_virtual/Explorer.mjs";import a from"../../../_virtual/promises.mjs";import e from"../../../_virtual/_node_path.mjs";import{__require as i}from"./defaults.mjs";import{__require as r}from"./ExplorerBase.mjs";import{__require as o}from"./merge.mjs";import{__require as s}from"./util.mjs";var n;function l(){if(n)return t;n=1;var l=t&&t.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.Explorer=void 0;const c=l(a),f=l(e),h=i(),u=r(),d=o(),m=s();let g=class extends u.ExplorerBase{async load(t){t=f.default.resolve(t);const a=async()=>await this.config.transform(await this.#t(t));return this.loadCache?await(0,m.emplace)(this.loadCache,t,a):await a()}async search(t=""){if(this.config.metaConfigFilePath){this.loadingMetaConfig=!0;const t=await this.load(this.config.metaConfigFilePath);if(this.loadingMetaConfig=!1,t&&!t.isEmpty)return t}t=f.default.resolve(t);const a=this.#a(t),e=await a.next();
/* istanbul ignore if -- @preserve */
if(e.done)throw new Error(`Could not find any folders to iterate through (start from ${t})`);let i=e.value;const r=async()=>{
/* istanbul ignore if -- @preserve */
if(await(0,m.isDirectory)(i.path))for(const t of this.getSearchPlacesForDir(i,h.globalConfigSearchPlaces))try{const a=await this.#t(t);if(!(null===a||a.isEmpty&&this.config.ignoreEmptySearchPlaces))return await this.config.transform(a)}catch(t){if("ENOENT"===t.code||"EISDIR"===t.code||"ENOTDIR"===t.code||"EACCES"===t.code)continue;throw t}const t=await a.next();return t.done?await this.config.transform(null):(i=t.value,this.searchCache?await(0,m.emplace)(this.searchCache,i.path,r):await r())};return this.searchCache?await(0,m.emplace)(this.searchCache,t,r):await r()}async#t(t,a=[]){const e=await c.default.readFile(t,{encoding:"utf-8"});return this.toCosmiconfigResult(t,await this.#e(t,e,a))}async#e(t,a,e){const i=await this.#i(t,a);if(!i||!(0,d.hasOwn)(i,"$import"))return i;const r=f.default.dirname(t),{$import:o,...s}=i,n=Array.isArray(o)?o:[o],l=[...e,t];this.validateImports(t,n,l);const c=await Promise.all(n.map(async t=>{const a=f.default.resolve(r,t),e=await this.#t(a,l);return e?.config}));return(0,d.mergeAll)([...c,s],{mergeArrays:this.config.mergeImportArrays})}async#i(t,a){if(""===a.trim())return;const e=f.default.extname(t),i=this.config.loaders[e||"noExt"]??this.config.loaders.default;if(!i)throw new Error(`No loader specified for ${(0,u.getExtensionDescription)(e)}`);try{const r=await i(t,a);return"package"!==f.default.basename(t,e)?r:(0,m.getPropertyByPath)(r,this.config.packageProp??this.config.moduleName)??null}catch(a){throw a.filepath=t,a}}async#r(t){try{return await c.default.stat(t),!0}catch(t){return!1}}async*#a(t){switch(this.config.searchStrategy){case"none":return void(yield{path:t,isGlobalConfig:!1});case"project":{let a=t;for(;;){yield{path:a,isGlobalConfig:!1};for(const t of["json","yaml"]){const e=f.default.join(a,`package.${t}`);if(await this.#r(e))break}const t=f.default.dirname(a);
/* istanbul ignore if -- @preserve */if(t===a)break;a=t}return}case"global":yield*this.getGlobalDirs(t)}}};return t.Explorer=g,t}export{l as __require};