import { ENCODER } from '@saulx/utils';
import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
import { ModifyError } from './ModifyRes.js';
import { setCursor } from './setCursor.js';
import native from '../../native.js';
export function getBuffer(value) {
    if (typeof value === 'object') {
        if (value instanceof Uint8Array) {
            return value;
        }
        if (value.buffer instanceof ArrayBuffer) {
            return new Uint8Array(value.buffer, 0, value.byteLength);
        }
    }
    else if (typeof value === 'string') {
        return ENCODER.encode(value);
    }
}
export function writeBinaryRaw(value, ctx) {
    let size = value.byteLength + 6;
    let crc = native.crc32(value);
    ctx.buf[ctx.len++] = size;
    ctx.buf[ctx.len++] = size >>>= 8;
    ctx.buf[ctx.len++] = size >>>= 8;
    ctx.buf[ctx.len++] = size >>>= 8;
    ctx.buf[ctx.len++] = 0;
    ctx.buf[ctx.len++] = 0;
    ctx.buf.set(value, ctx.len);
    ctx.len += value.byteLength;
    ctx.buf[ctx.len++] = crc;
    ctx.buf[ctx.len++] = crc >>>= 8;
    ctx.buf[ctx.len++] = crc >>>= 8;
    ctx.buf[ctx.len++] = crc >>>= 8;
}
export function writeBinary(value, ctx, schema, t, parentId, modifyOp) {
    let size;
    if (value === null) {
        size = 0;
    }
    else {
        value = getBuffer(value);
        if (!value || !t.validation(value, t)) {
            return new ModifyError(t, value);
        }
        size = value.byteLength + 6;
    }
    if (size === 0) {
        if (modifyOp === UPDATE) {
            if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
                return RANGE_ERR;
            }
            setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
            ctx.buf[ctx.len++] = DELETE;
        }
    }
    else {
        if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + size > ctx.max) {
            return RANGE_ERR;
        }
        setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
        ctx.buf[ctx.len++] = modifyOp;
        writeBinaryRaw(value, ctx);
    }
}
//# sourceMappingURL=binary.js.map