"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = void 0;
exports.getContext = getContext;
exports.getContextProp = getContextProp;
exports.setContextProp = setContextProp;
exports.removeContextProp = removeContextProp;
exports.withContext = withContext;
exports.withContextData = withContextData;
const node_async_hooks_1 = require("node:async_hooks");
const context = new node_async_hooks_1.AsyncLocalStorage();
const ContextLifetimeKey = Symbol('context-lifetime');
function getContext() {
    const store = context.getStore();
    const lifetime = store?.[ContextLifetimeKey];
    return lifetime?.active === false ? undefined : store;
}
// oxlint-disable-next-line typescript/no-explicit-any -- preserves the existing permissive default for callers that do not supply T
function getContextProp(name) {
    return getContext()?.[name];
}
// oxlint-disable-next-line typescript/no-explicit-any -- preserves the existing permissive default for callers that do not supply T
function setContextProp(name, value) {
    const store = getContext();
    if (store)
        store[name] = value;
}
function removeContextProp(name) {
    const store = getContext();
    if (store)
        delete store[name];
}
async function withContext(cb) {
    if (getContext())
        return cb();
    return withContextData({}, cb);
}
async function withContextData(data, cb) {
    const lifetime = { active: true };
    const store = { ...getContext(), ...data, [ContextLifetimeKey]: lifetime };
    return context.run(store, async () => {
        try {
            return await cb();
        }
        finally {
            lifetime.active = false;
        }
    });
}
exports.__tsfTypeAliases = JSON.parse("[1,{\"SimpleStore\":{\"kind\":18,\"index\":{\"kind\":1},\"types\":[],\"typeName\":\"SimpleStore\"}}]")[1];
//# sourceMappingURL=context.js.map