import { getBaseKeyValueStore } from "./kv-base.js";
import { getConfig } from "../../config/index.js";
export const GLOBAL_STORE_NAME = "global";
export const GLOBAL_COUNT_NAME = "globalCount";
export function getKeyValueStore(name, options) {
    const config = getConfig();
    if (config.getKeyValueStore) {
        return config.getKeyValueStore(name, options);
    }
    const store = getBaseKeyValueStore(name, options);
    const counters = options?.counter === true
        ? {
            store: getCounterStore(name),
            global: getGlobalCounterStore(),
        }
        : undefined;
    return {
        ...store,
        counters,
        async set(key, value) {
            await counters?.global.increment(GLOBAL_COUNT_NAME);
            await counters?.store.increment(key);
            return store.set(key, value);
        },
        async delete(key) {
            await counters?.store.delete(key);
            return store.delete(key);
        },
        async clear() {
            await counters?.global.clear();
            await counters?.store.clear();
            return store.clear();
        }
    };
}
export function getCounterStoreName(baseName) {
    return `${baseName}Counter`;
}
export function getCounterStore(name) {
    return getBaseKeyValueStore(getCounterStoreName(name));
}
export function getGlobalCounterStore() {
    return getCounterStore(GLOBAL_STORE_NAME);
}
export async function getGlobalCount() {
    const store = getGlobalCounterStore();
    const count = await store.get(GLOBAL_COUNT_NAME);
    if (typeof count !== "number") {
        return -1;
    }
    return count;
}
//# sourceMappingURL=kv.js.map