import { getKeyValueStore } from "../data/index.js";
import { listKeyValueStoreIndex } from "../data/storage/store-index.js";
export async function listInternalStorageBucketNames() {
    const keys = await listKeyValueStoreIndex();
    const buckets = keys
        .filter(key => key.startsWith("bucket:"))
        .map(key => key.split(":")[1]);
    return ["default", ...new Set(buckets)];
}
export function getInternalStorageBucket(bucket = "default") {
    const isDefault = bucket === "default";
    const prefix = isDefault ? "" : `bucket:${bucket}:`;
    return {
        getKeyValueStore(name, options) {
            if (isDefault) {
                return getKeyValueStore(name, options);
            }
            else {
                return getKeyValueStore(`${prefix}${name}`, options);
            }
        },
        async delete() {
            const names = await getStoresToDelete();
            for (const name of names) {
                const store = getKeyValueStore(name);
                await store.clear();
            }
            async function getStoresToDelete() {
                const keys = await listKeyValueStoreIndex();
                if (isDefault) {
                    return keys.filter(key => !key.startsWith("bucket:"));
                }
                else {
                    return keys.filter(key => key.startsWith(prefix));
                }
            }
        }
    };
}
//# sourceMappingURL=internal.js.map