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