import { PrefixedRepository, decodeNumberForDbKey, encodeNumberForDbKey } from "@lodestar/db";
import { NUMBER_OF_COLUMNS } from "@lodestar/params";
import { isGloasDataColumnSidecar, ssz } from "@lodestar/types";
import { isGloasDataColumnSidecarBytes } from "../../util/multifork.js";
import { Bucket, getBucketNameByValue } from "../buckets.js";
const COLUMN_INDEX_BYTE_SIZE = 2;
const BLOCK_ROOT_BYTE_SIZE = 32;
/**
 * DataColumnSidecarsRepository
 * Used to store `unfinalized` DataColumnSidecars
 *
 * Indexed data by `blockRoot` + `columnIndex`
 */
export class DataColumnSidecarRepository extends PrefixedRepository {
    constructor(config, db) {
        const bucket = Bucket.fulu_dataColumnSidecars;
        super(config, db, bucket, ssz.fulu.DataColumnSidecar, getBucketNameByValue(bucket));
    }
    /**
     * Id is hashTreeRoot of unsigned BeaconBlock
     */
    getId(value) {
        return value.index;
    }
    encodeValue(value) {
        if (isGloasDataColumnSidecar(value)) {
            return ssz.gloas.DataColumnSidecar.serialize(value);
        }
        return ssz.fulu.DataColumnSidecar.serialize(value);
    }
    decodeValue(data) {
        if (isGloasDataColumnSidecarBytes(data)) {
            return ssz.gloas.DataColumnSidecar.deserialize(data);
        }
        return ssz.fulu.DataColumnSidecar.deserialize(data);
    }
    encodeKeyRaw(prefix, id) {
        return Buffer.concat([prefix, encodeNumberForDbKey(id, COLUMN_INDEX_BYTE_SIZE)]);
    }
    decodeKeyRaw(raw) {
        return {
            prefix: raw.slice(0, BLOCK_ROOT_BYTE_SIZE),
            id: decodeNumberForDbKey(raw.slice(BLOCK_ROOT_BYTE_SIZE), COLUMN_INDEX_BYTE_SIZE),
        };
    }
    getMaxKeyRaw(prefix) {
        return Buffer.concat([prefix, encodeNumberForDbKey(NUMBER_OF_COLUMNS - 1, COLUMN_INDEX_BYTE_SIZE)]);
    }
    getMinKeyRaw(prefix) {
        return Buffer.concat([prefix, encodeNumberForDbKey(0, COLUMN_INDEX_BYTE_SIZE)]);
    }
}
//# sourceMappingURL=dataColumnSidecar.js.map