// @ts-ignore
import db from '../../basedDbNative.cjs';
// Can't import these from utils or it would be a cyclic import.
const DECODER = new TextDecoder('utf-8');
const ENCODER = new TextEncoder();
const selvaIoErrlog = new Uint8Array(256);
var compressor = db.createCompressor();
var decompressor = db.createDecompressor();
function SelvaIoErrlogToString(buf) {
    let i;
    let len = (i = buf.indexOf(0)) >= 0 ? i : buf.byteLength;
    return DECODER.decode(selvaIoErrlog.slice(0, len));
}
const native = {
    historyAppend(history, typeId, nodeId, dbCtx) {
        return db.historyAppend(history, typeId, nodeId, dbCtx);
    },
    historyCreate(pathname, mainLen) {
        const pathBuf = ENCODER.encode(pathname + '\0');
        return db.historyCreate(pathBuf, mainLen + 16 - (mainLen % 16));
    },
    workerCtxInit: () => {
        return db.workerCtxInit();
    },
    externalFromInt(address) {
        return db.externalFromInt(address);
    },
    intFromExternal(external) {
        return db.intFromExternal(external);
    },
    modify: (data, types, dbCtx, dirtyBlocksOut) => {
        db.modify(data, types, dbCtx, dirtyBlocksOut);
    },
    getQueryBuf: (q, dbCtx) => {
        const x = db.getQueryBuf(dbCtx, q);
        return x;
    },
    start: () => {
        return db.start();
    },
    stop: (dbCtx) => {
        return db.stop(dbCtx);
    },
    saveCommon: (path, dbCtx) => {
        const pathBuf = ENCODER.encode(path + '\0');
        return db.saveCommon(pathBuf, dbCtx);
    },
    saveRange: (path, typeCode, start, end, dbCtx, hashOut) => {
        const pathBuf = ENCODER.encode(path + '\0');
        return db.saveRange(pathBuf, typeCode, start, end, dbCtx, hashOut);
    },
    loadCommon: (path, dbCtx) => {
        const pathBuf = ENCODER.encode(path + '\0');
        const err = db.loadCommon(pathBuf, dbCtx, selvaIoErrlog);
        if (err) {
            throw new Error(`Failed to load common. selvaError: ${err} cause:\n${SelvaIoErrlogToString(selvaIoErrlog)}`);
        }
    },
    loadRange: (path, dbCtx) => {
        const pathBuf = ENCODER.encode(path + '\0');
        const err = db.loadRange(pathBuf, dbCtx, selvaIoErrlog);
        if (err) {
            throw new Error(`Failed to load a range. selvaError: ${err} cause:\n${SelvaIoErrlogToString(selvaIoErrlog)}`);
        }
    },
    updateSchemaType: (prefix, buf, dbCtx) => {
        return db.updateSchema(prefix, buf, dbCtx);
    },
    getTypeInfo: (typeId, dbCtx) => {
        return db.getTypeInfo(typeId, dbCtx);
    },
    getNodeRangeHash: (typeId, start, end, bufOut, dbCtx) => {
        return db.getNodeRangeHash(typeId, start, end, bufOut, dbCtx);
    },
    compress: (buf, offset, stringSize) => {
        return db.compress(compressor, buf, offset, stringSize);
    },
    decompress: (input, output, offset, len) => {
        return db.decompress(decompressor, input, output, offset, len);
    },
    crc32: (buf) => {
        return db.crc32(buf);
    },
    createSortIndex: (buf, dbCtx) => {
        return db.createSortIndex(dbCtx, buf);
    },
    destroySortIndex: (buf, dbCtx) => {
        return db.destroySortIndex(dbCtx, buf);
    },
    xxHash64: (buf, target, index) => {
        return db.xxHash64(buf, target, index);
    },
    equals: (a, b) => {
        return !!db.equals(a, b);
    },
};
global.__basedDb__native__ = native;
export default native;
//# sourceMappingURL=native.js.map