import { CID } from 'multiformats';
import { createLevelDatabase, LevelWrapper } from './level-wrapper.js';
// `level` works in Node.js 12+ and Electron 5+ on Linux, Mac OS, Windows and
// FreeBSD, including any future Node.js and Electron release thanks to Node-API, including ARM
// platforms like Raspberry Pi and Android, as well as in Chrome, Firefox, Edge, Safari, iOS Safari
//  and Chrome for Android.
/**
 * Blockstore implementation using LevelDB for storing the actual messages (in the case of MessageStore)
 * or the data associated with messages (in the case of a DataStore).
 */
export class BlockstoreLevel {
    config;
    db;
    constructor(config, db) {
        this.config = {
            createLevelDatabase,
            ...config
        };
        this.db = db ?? new LevelWrapper({ ...this.config, valueEncoding: 'binary' });
    }
    async open() {
        return this.db.open();
    }
    async close() {
        return this.db.close();
    }
    async partition(name) {
        const db = await this.db.partition(name);
        return new BlockstoreLevel({ ...this.config, location: '' }, db);
    }
    async put(key, val, options) {
        await this.db.put(String(key), val, options);
        return CID.parse(key.toString());
    }
    async get(key, options) {
        const result = await this.db.get(String(key), options);
        return result;
    }
    async has(key, options) {
        return this.db.has(String(key), options);
    }
    async delete(key, options) {
        return this.db.delete(String(key), options);
    }
    async isEmpty(options) {
        return this.db.isEmpty(options);
    }
    async *putMany(source, options) {
        for await (const entry of source) {
            await this.put(entry.cid, entry.block, options);
            yield entry.cid;
        }
    }
    async *getMany(source, options) {
        for await (const key of source) {
            yield {
                cid: key,
                block: await this.get(key, options)
            };
        }
    }
    async *getAll(options) {
        // @ts-expect-error keyEncoding is 'buffer' but types for db.iterator always return the key type as 'string'
        const li = this.db.iterator({
            keys: true,
            keyEncoding: 'buffer'
        }, options);
        for await (const [key, value] of li) {
            yield { cid: CID.decode(key), block: value };
        }
    }
    async *deleteMany(source, options) {
        for await (const key of source) {
            await this.delete(key, options);
            yield key;
        }
    }
    /**
     * deletes all entries
     */
    async clear() {
        return this.db.clear();
    }
}
//# sourceMappingURL=blockstore-level.js.map