import { DataTypeIndices } from "../../binary/type/DataTypeIndices.js";

/**
 *
 * @param {BinaryBuffer} buffer
 * @param {RowFirstTable} table
 */
function serializeRowFirstTable(buffer, table) {
    const spec = table.spec;

    //write types
    buffer.writeUint16(spec.types.length);
    spec.types.forEach(function (type) {
        const typeIndex = DataTypeIndices[type];
        buffer.writeUint8(typeIndex);
    });

    //write record length
    buffer.writeUint32(spec.bytesPerRecord);

    //write number of records
    buffer.writeUint32(table.length);

    //write data
    const numDataBytes = table.length * spec.bytesPerRecord;
    buffer.writeBytes(new Uint8Array(table.data), 0, numDataBytes);
}

export { serializeRowFirstTable };
