"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rewire_exports = {};
__export(rewire_exports, {
  clearEnv: () => clearEnv,
  getAsyncHooks: () => getAsyncHooks,
  getEnvValue: () => getEnvValue,
  hasOverride: () => hasOverride,
  isInEnv: () => isInEnv,
  resetEnv: () => resetEnv,
  setEnv: () => setEnv,
  withEnv: () => withEnv,
  withEnvSync: () => withEnvSync
});
module.exports = __toCommonJS(rewire_exports);
var import_node_process = __toESM(require("node:process"));
var import_predicates = require("../objects/predicates");
var import_boolean = require("./boolean");
var import_map_set = require("../primordials/map-set");
var import_object = require("../primordials/object");
let _async_hooks;
// @__NO_SIDE_EFFECTS__
function getAsyncHooks() {
  if (_async_hooks === void 0) {
    _async_hooks = require("node:async_hooks");
  }
  return _async_hooks;
}
const { AsyncLocalStorage } = /* @__PURE__ */ getAsyncHooks();
const isolatedOverridesStorage = new AsyncLocalStorage();
const sharedOverridesSymbol = Symbol.for(
  "@socketsecurity/lib/env/rewire/test-overrides"
);
const _globalThis = globalThis;
const isVitestEnv = (0, import_boolean.envAsBoolean)(import_node_process.default.env["VITEST"]);
if (isVitestEnv && !_globalThis[sharedOverridesSymbol]) {
  _globalThis[sharedOverridesSymbol] = new import_map_set.MapCtor();
}
const sharedOverrides = _globalThis[sharedOverridesSymbol];
function clearEnv(key) {
  sharedOverrides?.delete(key);
}
function getEnvValue(key) {
  const isolatedOverrides = isolatedOverridesStorage.getStore();
  if (isolatedOverrides?.has(key)) {
    return isolatedOverrides.get(key);
  }
  if (sharedOverrides?.has(key)) {
    return sharedOverrides.get(key);
  }
  return import_node_process.default.env[key];
}
function hasOverride(key) {
  const isolatedOverrides = isolatedOverridesStorage.getStore();
  return !!(isolatedOverrides?.has(key) || sharedOverrides?.has(key));
}
function isInEnv(key) {
  const isolatedOverrides = isolatedOverridesStorage.getStore();
  if (isolatedOverrides?.has(key)) {
    return true;
  }
  if (sharedOverrides?.has(key)) {
    return true;
  }
  return (0, import_predicates.hasOwn)(import_node_process.default.env, key);
}
function resetEnv() {
  sharedOverrides?.clear();
}
function setEnv(key, value) {
  sharedOverrides?.set(key, value);
}
async function withEnv(overrides, fn) {
  const map = new import_map_set.MapCtor((0, import_object.ObjectEntries)(overrides));
  return await isolatedOverridesStorage.run(map, fn);
}
function withEnvSync(overrides, fn) {
  const map = new import_map_set.MapCtor((0, import_object.ObjectEntries)(overrides));
  return isolatedOverridesStorage.run(map, fn);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearEnv,
  getAsyncHooks,
  getEnvValue,
  hasOverride,
  isInEnv,
  resetEnv,
  setEnv,
  withEnv,
  withEnvSync
});
