function getNodeAsyncContext() {
  const mod = globalThis.process?.getBuiltinModule?.('node:async_hooks');
  /* v8 ignore next */
  if (!mod?.AsyncLocalStorage) {
    throw new Error('AsyncLocalStorage not available');
  }
  return new mod.AsyncLocalStorage();
}
/* v8 ignore next */
function createFallbackAsyncContext() {
  let store;
  // eslint-disable-next-line no-console
  console.warn('AsyncLocalStorage not available');
  return {
    getStore: () => store,
    enterWith: value => (store = value),
    run: (value, cb) => {
      const prev = store;
      store = value;
      try {
        return cb();
      } finally {
        store = prev;
      }
    },
  };
}
export function createAsyncContext() {
  /* v8 ignore next */
  const ALS = globalThis.AsyncLocalStorage;
  /* v8 ignore next */
  if (typeof ALS === 'function' && ALS.prototype?.run) {
    return new ALS();
  }
  /* v8 ignore else */
  if (globalThis.process?.versions?.node) {
    return getNodeAsyncContext();
  }
  /* v8 ignore next */
  return createFallbackAsyncContext();
}
