"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContext = getContext;
exports.getContextProp = getContextProp;
exports.setContextProp = setContextProp;
exports.removeContextProp = removeContextProp;
exports.withContext = withContext;
exports.withContextData = withContextData;
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const async_hooks_1 = require("async_hooks");
const __ΩSimpleStore = ['SimpleStore', 'PP&+J"LMw!y'];
const context = (async_hooks_1.AsyncLocalStorage.Ω = [[() => __ΩSimpleStore, 'n!']], new async_hooks_1.AsyncLocalStorage());
function getContext() {
    return context.getStore();
}
getContext.__type = [() => __ΩSimpleStore, 'getContext', 'PPn!-J/"'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getContextProp(name) {
    return getContext()?.[name];
}
getContextProp.__type = ['name', 'getContextProp', 'P&2!P"-J/"'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setContextProp(name, value) {
    const context = getContext();
    if (context) {
        context[name] = value;
    }
}
setContextProp.__type = ['name', 'value', 'setContextProp', 'P&2!"2"$/#'];
function removeContextProp(name) {
    const context = getContext();
    if (context) {
        delete context[name];
    }
}
removeContextProp.__type = ['name', 'removeContextProp', 'P&2!$/"'];
async function withContext(cb) {
    const existingContext = getContext();
    if (existingContext) {
        return cb();
    }
    return (Promise.Ω = [[() => cb, '', 'Pdi!Ph"!`/"qe!!!j']], new Promise(__assignType((resolve, reject) => {
        context.run({}, () => {
            cb()
                .then(__assignType((result) => resolve(result), [() => cb, '', 'result', 'PPdi!Ph"!`/"qe!!!j2#"/"']))
                .catch(__assignType(e => reject(e), ['e', '', 'P"2!"/"']));
        });
    }, ['resolve', 'reject', '', 'P"2!"2""/#'])));
}
withContext.__type = ['', 'cb', 'withContext', 'PP"`/!2""/#'];
async function withContextData(data, cb) {
    return withContext(async () => {
        const context = getContext();
        const dataKeys = [...Object.getOwnPropertyNames(data), ...Object.getOwnPropertySymbols(data)];
        const overwriteKeys = dataKeys.filter(__assignType(key => key in context, ['key', '', 'P"2!"/"']));
        const overwriteValues = overwriteKeys.map(__assignType(key => context[key], ['key', '', 'P"2!"/"']));
        Object.assign(context, data);
        try {
            return await cb();
        }
        finally {
            for (const key of dataKeys) {
                delete context[key];
            }
            for (let i = 0; i < overwriteKeys.length; i++) {
                context[overwriteKeys[i]] = overwriteValues[i];
            }
        }
    });
}
withContextData.__type = [() => __ΩSimpleStore, 'data', '', 'cb', 'withContextData', 'Pn!2"P"`/#2$"/%'];
//# sourceMappingURL=context.js.map