import { ok } from "../../is.js";
export function createMemoryStore(name, options) {
    const map = new Map();
    const isCounter = name.endsWith("Counter");
    async function get(key) {
        return map.get(key);
    }
    async function set(key, value) {
        if (isCounter) {
            ok(typeof value === "number", "Expected number value for counter store");
        }
        map.set(key, value);
    }
    async function values() {
        return [...map.values()];
    }
    async function* asyncIterable() {
        for (const value of map.values()) {
            yield value;
        }
    }
    async function deleteFn(key) {
        map.delete(key);
    }
    async function has(key) {
        return map.has(key);
    }
    async function keys() {
        return [...map.keys()];
    }
    async function clear() {
        map.clear();
    }
    async function increment(key) {
        ok(isCounter, "Expected increment to be used with a counter store only");
        const storedValue = map.get(key);
        const currentValue = typeof storedValue === "number" ? storedValue : 0;
        const nextValue = currentValue + 1;
        ok(nextValue);
        map.set(key, nextValue);
        return nextValue;
    }
    function meta(key) {
        const fn = options?.meta;
        ok(fn, "expected meta option to be provided if meta is used");
        return fn(key);
    }
    return {
        name,
        get,
        set,
        values,
        delete: deleteFn,
        has,
        keys,
        clear,
        increment,
        meta,
        [Symbol.asyncIterator]() {
            return asyncIterable()[Symbol.asyncIterator]();
        },
    };
}
//# sourceMappingURL=memory.js.map