import e from"node:fs";import t from"node:path";import{fileURLToPath as o}from"node:url";import{codes as n}from"./errors.mjs";const r={}.hasOwnProperty,{ERR_INVALID_PACKAGE_CONFIG:s}=n,a=new Map;function i(n,{base:i,specifier:p}){const m=a.get(n);if(m)return m;let c;try{c=e.readFileSync(t.toNamespacedPath(n),"utf8")}catch(e){const t=e;if("ENOENT"!==t.code)throw t}const f={exists:!1,pjsonPath:n,main:void 0,name:void 0,type:"none",exports:void 0,imports:void 0};if(void 0!==c){let e;try{e=JSON.parse(c)}catch(e){const t=e,r=new s(n,(i?`"${p}" from `:"")+o(i||p),t.message);throw r.cause=t,r}f.exists=!0,r.call(e,"name")&&"string"==typeof e.name&&(f.name=e.name),r.call(e,"main")&&"string"==typeof e.main&&(f.main=e.main),r.call(e,"exports")&&(f.exports=e.exports),r.call(e,"imports")&&(f.imports=e.imports),!r.call(e,"type")||"commonjs"!==e.type&&"module"!==e.type||(f.type=e.type)}return a.set(n,f),f}function p(e){let t=new URL("package.json",e);for(;;){if(t.pathname.endsWith("node_modules/package.json"))break;const n=i(o(t),{specifier:e});if(n.exists)return n;const r=t;if(t=new URL("../package.json",t),t.pathname===r.pathname)break}return{pjsonPath:o(t),exists:!1,type:"none"}}function m(e){return p(e).type}export{p as getPackageScopeConfig,m as getPackageType,i as read};