"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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, {
  clearPath: () => clearPath,
  getPathValue: () => getPathValue,
  hasOverride: () => hasOverride,
  invalidateCaches: () => invalidateCaches,
  registerCacheInvalidation: () => registerCacheInvalidation,
  resetPaths: () => resetPaths,
  setPath: () => setPath
});
module.exports = __toCommonJS(rewire_exports);
var import_map_set = require("../primordials/map-set");
const stateSymbol = Symbol.for("@socketsecurity/lib/paths/rewire/state");
const _globalThis = globalThis;
if (!_globalThis[stateSymbol]) {
  _globalThis[stateSymbol] = {
    testOverrides: new import_map_set.MapCtor(),
    valueCache: new import_map_set.MapCtor(),
    cacheInvalidationCallbacks: []
  };
}
const sharedState = _globalThis[stateSymbol];
const testOverrides = sharedState.testOverrides;
const valueCache = sharedState.valueCache;
const cacheInvalidationCallbacks = sharedState.cacheInvalidationCallbacks;
function clearPath(key) {
  testOverrides.delete(key);
  invalidateCaches();
}
function getPathValue(key, originalFn) {
  if (testOverrides.has(key)) {
    return testOverrides.get(key);
  }
  if (valueCache.has(key)) {
    return valueCache.get(key);
  }
  const value = originalFn();
  valueCache.set(key, value);
  return value;
}
function hasOverride(key) {
  return testOverrides.has(key);
}
function invalidateCaches() {
  valueCache.clear();
  for (const callback of cacheInvalidationCallbacks) {
    try {
      callback();
    } catch {
    }
  }
}
function registerCacheInvalidation(callback) {
  cacheInvalidationCallbacks.push(callback);
}
function resetPaths() {
  testOverrides.clear();
  invalidateCaches();
}
function setPath(key, value) {
  testOverrides.set(key, value);
  invalidateCaches();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearPath,
  getPathValue,
  hasOverride,
  invalidateCaches,
  registerCacheInvalidation,
  resetPaths,
  setPath
});
