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 SLOT_BYTE_SIZE = 8;
/**
 * DataColumnSidecarsRepository
 * Used to store `finalized` DataColumnSidecars
 *
 * Indexed data by `slot` + `columnIndex`
 */
export class DataColumnSidecarArchiveRepository extends PrefixedRepository {
    constructor(config, db) {
        const bucket = Bucket.fulu_dataColumnSidecarsArchive;
        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([
            encodeNumberForDbKey(prefix, SLOT_BYTE_SIZE),
            encodeNumberForDbKey(id, COLUMN_INDEX_BYTE_SIZE),
        ]);
    }
    decodeKeyRaw(raw) {
        return {
            prefix: decodeNumberForDbKey(raw, SLOT_BYTE_SIZE),
            id: decodeNumberForDbKey(raw.slice(SLOT_BYTE_SIZE), COLUMN_INDEX_BYTE_SIZE),
        };
    }
    getMaxKeyRaw(prefix) {
        return Buffer.concat([
            encodeNumberForDbKey(prefix, SLOT_BYTE_SIZE),
            encodeNumberForDbKey(NUMBER_OF_COLUMNS - 1, COLUMN_INDEX_BYTE_SIZE),
        ]);
    }
    getMinKeyRaw(prefix) {
        return Buffer.concat([
            encodeNumberForDbKey(prefix, SLOT_BYTE_SIZE),
            encodeNumberForDbKey(0, COLUMN_INDEX_BYTE_SIZE),
        ]);
    }
}
//# sourceMappingURL=dataColumnSidecarArchive.js.map