import*as u from"dotenv";import*as E from"path";import{PUBLIC_ENV_PREFIX as b}from"../constants/index.js";import{isObject as m}from"./jsUtils.js";const A=/{{\s*process\.env\.([A-Z0-9_]+)\s*}}/gi;function f(e){return process.env[e]}function O(e,n=[]){const r=new Set,o=new Set,c={};return!m(e)&&!Array.isArray(e)?{resolvedObj:e,unsetEnvVars:Array.from(r),interpolatedEnvVars:Array.from(o),replacedValues:c}:{resolvedObj:Object.entries(e).reduce((t,[s,a])=>{const v=n.concat(s);if(typeof a=="string"){const{interpolatedValue:i,unsetVars:d,interpolatedVars:V}=h(a);t[s]=i;for(const l of d)r.add(l);for(const l of V)o.add(l);V.length>0&&(c[v.join(":")]={original:a,replaced:i})}else if(m(a)||Array.isArray(a)){const i=O(a,v);t[s]=i.resolvedObj;for(const d of i.unsetEnvVars)r.add(d);for(const d of i.interpolatedEnvVars)o.add(d);Object.assign(c,i.replacedValues)}else t[s]=a;return t},Array.isArray(e)?[]:{}),unsetEnvVars:Array.from(r),interpolatedEnvVars:Array.from(o),replacedValues:c}}function h(e){let n=e;const r=new Set,o=new Set,c=[...e.matchAll(A)];for(const p of c){const t=p[1].toUpperCase(),s=f(t);s===void 0?r.add(t):(n=n.replace(p[0],s),o.add(t))}return{interpolatedValue:n,unsetVars:Array.from(r),interpolatedVars:Array.from(o)}}function w(e,n){n==null?delete process.env[e]:process.env[e]=n}function x(){const e=process.env,n=Object.entries(e).filter(([r])=>r.startsWith(b));return Object.fromEntries(n)}function _(e){u.config({path:E.resolve(e??"",".env")});const n=f("REDOCLY_ENV");let r;switch(n){case"production":r=".env.production";break;case"preview":r=".env.preview";break;case"development":default:r=".env.development";break}u.config({path:E.resolve(e??"",r)})}function j(){return f("REDOCLY_ENV")==="production"}export{x as getPublicEnvVariables,h as interpolateEnvVariables,j as isProductionMode,_ as loadEnvironmentVariables,f as readEnvVariable,O as replaceEnvVariablesDeep,w as writeEnvVariable};
