import{join,resolve}from"node:path";import{readFileSync}from"node:fs";import{parseEnv}from"node:util";import{isObject}from"#shared/guards.js";const DEVELOPMENT_ENV_FILE_NAMES=[`.env.development.local`,`.env.local`,`.env.development`,`.env`];function isMissingEnvironmentFileError(e){return isObject(e)&&e.code===`ENOENT`}const developmentEnvironmentLoaders=new Map;function getDevelopmentEnvironmentFilePaths(n){let r=resolve(n);return DEVELOPMENT_ENV_FILE_NAMES.map(t=>join(r,t))}function loadDevelopmentEnvironmentFiles(e){getDevelopmentEnvironmentLoader(e).reload()}function getDevelopmentEnvironmentLoader(e){let n=resolve(e),r=developmentEnvironmentLoaders.get(n);if(r!==void 0)return r;let i=createDevelopmentEnvironmentLoader(n);return developmentEnvironmentLoaders.set(n,i),i}function createDevelopmentEnvironmentLoader(e){let t=new Set(Object.keys(process.env)),n=new Map;return{reload(){let r=readDevelopmentEnvironmentValues(e);for(let[e,i]of n)r.has(e)||t.has(e)||(process.env[e]===i&&delete process.env[e],n.delete(e));for(let[e,i]of r)t.has(e)||(process.env[e]=i,n.set(e,i))}}}function readDevelopmentEnvironmentValues(t){let i=new Map;for(let o of[...DEVELOPMENT_ENV_FILE_NAMES].reverse())try{let a=parseEnv(readFileSync(join(t,o),`utf8`));for(let[e,t]of Object.entries(a))t!==void 0&&i.set(e,t)}catch(e){if(!isMissingEnvironmentFileError(e))throw e}return i}export{DEVELOPMENT_ENV_FILE_NAMES,getDevelopmentEnvironmentFilePaths,loadDevelopmentEnvironmentFiles};