"use strict";
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rewire_exports = {};
__export(rewire_exports, {
  clearEnv: () => clearEnv,
  getEnvValue: () => getEnvValue,
  hasOverride: () => hasOverride,
  resetEnv: () => resetEnv,
  setEnv: () => setEnv,
  withEnv: () => withEnv,
  withEnvSync: () => withEnvSync
});
module.exports = __toCommonJS(rewire_exports);
var import_async_hooks = require("async_hooks");
var import_helpers = require("./helpers");
const isolatedOverridesStorage = new import_async_hooks.AsyncLocalStorage();
const sharedOverridesSymbol = Symbol.for(
  "@socketsecurity/lib/env/rewire/test-overrides"
);
const isVitestEnv = (0, import_helpers.envAsBoolean)(process.env.VITEST);
if (isVitestEnv && !globalThis[sharedOverridesSymbol]) {
  globalThis[sharedOverridesSymbol] = /* @__PURE__ */ new Map();
}
const sharedOverrides = globalThis[sharedOverridesSymbol];
function getEnvValue(key) {
  const isolatedOverrides = isolatedOverridesStorage.getStore();
  if (isolatedOverrides?.has(key)) {
    return isolatedOverrides.get(key);
  }
  if (sharedOverrides?.has(key)) {
    return sharedOverrides.get(key);
  }
  return process.env[key];
}
function setEnv(key, value) {
  sharedOverrides?.set(key, value);
}
function clearEnv(key) {
  sharedOverrides?.delete(key);
}
function resetEnv() {
  sharedOverrides?.clear();
}
function hasOverride(key) {
  const isolatedOverrides = isolatedOverridesStorage.getStore();
  return !!(isolatedOverrides?.has(key) || sharedOverrides?.has(key));
}
async function withEnv(overrides, fn) {
  const map = new Map(Object.entries(overrides));
  return await isolatedOverridesStorage.run(map, fn);
}
function withEnvSync(overrides, fn) {
  const map = new Map(Object.entries(overrides));
  return isolatedOverridesStorage.run(map, fn);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearEnv,
  getEnvValue,
  hasOverride,
  resetEnv,
  setEnv,
  withEnv,
  withEnvSync
});
