import { AsyncLocalStorage } from "async_hooks";
import { ok } from "../is.js";
export class Context {
    storage;
    defaultValues;
    constructor(defaultValues) {
        this.defaultValues = defaultValues;
        this.storage = new AsyncLocalStorage();
    }
    get value() {
        const value = {
            ...this.storage.getStore()
        };
        ok(value);
        return value;
    }
    get(key) {
        const store = this.storage.getStore();
        if (!store)
            return undefined;
        return store[key];
    }
    set(key, value) {
        let store = this.storage.getStore();
        if (!store) {
            const next = { ...this.defaultValues };
            ok(next);
            store = next;
            this.storage.enterWith(store);
        }
        ok(value);
        store[key] = value;
    }
    run(values, fn) {
        ok(values);
        return this.storage.run(values, fn);
    }
}
export function createContext(defaultValues) {
    return new Context(defaultValues);
}
//# sourceMappingURL=context.js.map