import { ssz } from "@lodestar/types";
import { BeaconBlocksByRootRequestType, BlobSidecarsByRootRequestType, DataColumnSidecarsByRootRequestType, } from "../../../util/types.js";
import { ReqRespMethod } from "../types.js";
import { onBeaconBlocksByRange } from "./beaconBlocksByRange.js";
import { onBeaconBlocksByRoot } from "./beaconBlocksByRoot.js";
import { onBlobSidecarsByRange } from "./blobSidecarsByRange.js";
import { onBlobSidecarsByRoot } from "./blobSidecarsByRoot.js";
import { onDataColumnSidecarsByRange } from "./dataColumnSidecarsByRange.js";
import { onDataColumnSidecarsByRoot } from "./dataColumnSidecarsByRoot.js";
import { onLightClientBootstrap } from "./lightClientBootstrap.js";
import { onLightClientFinalityUpdate } from "./lightClientFinalityUpdate.js";
import { onLightClientOptimisticUpdate } from "./lightClientOptimisticUpdate.js";
import { onLightClientUpdatesByRange } from "./lightClientUpdatesByRange.js";
function notImplemented(method) {
    return () => {
        throw Error(`Handler not implemented for ${method}`);
    };
}
/**
 * The ReqRespHandler module handles app-level requests / responses from other peers,
 * fetching state from the chain and database as needed.
 */
export function getReqRespHandlers({ db, chain }) {
    const handlers = {
        [ReqRespMethod.Status]: notImplemented(ReqRespMethod.Status),
        [ReqRespMethod.Goodbye]: notImplemented(ReqRespMethod.Goodbye),
        [ReqRespMethod.Ping]: notImplemented(ReqRespMethod.Ping),
        [ReqRespMethod.Metadata]: notImplemented(ReqRespMethod.Metadata),
        [ReqRespMethod.BeaconBlocksByRange]: (req, peerId, peerClient) => {
            const body = ssz.phase0.BeaconBlocksByRangeRequest.deserialize(req.data);
            return onBeaconBlocksByRange(body, chain, db, peerId, peerClient);
        },
        [ReqRespMethod.BeaconBlocksByRoot]: (req) => {
            const fork = chain.config.getForkName(chain.clock.currentSlot);
            const body = BeaconBlocksByRootRequestType(fork, chain.config).deserialize(req.data);
            return onBeaconBlocksByRoot(body, chain);
        },
        [ReqRespMethod.BlobSidecarsByRoot]: (req) => {
            const fork = chain.config.getForkName(chain.clock.currentSlot);
            const body = BlobSidecarsByRootRequestType(fork, chain.config).deserialize(req.data);
            return onBlobSidecarsByRoot(body, chain);
        },
        [ReqRespMethod.BlobSidecarsByRange]: (req) => {
            const body = ssz.deneb.BlobSidecarsByRangeRequest.deserialize(req.data);
            return onBlobSidecarsByRange(body, chain, db);
        },
        [ReqRespMethod.DataColumnSidecarsByRange]: (req, peerId, peerClient) => {
            const body = ssz.fulu.DataColumnSidecarsByRangeRequest.deserialize(req.data);
            return onDataColumnSidecarsByRange(body, chain, db, peerId, peerClient);
        },
        [ReqRespMethod.DataColumnSidecarsByRoot]: (req, peerId, peerClient) => {
            const body = DataColumnSidecarsByRootRequestType(chain.config).deserialize(req.data);
            return onDataColumnSidecarsByRoot(body, chain, db, peerId, peerClient);
        },
        [ReqRespMethod.LightClientBootstrap]: (req) => {
            const body = ssz.Root.deserialize(req.data);
            return onLightClientBootstrap(body, chain);
        },
        [ReqRespMethod.LightClientUpdatesByRange]: (req) => {
            const body = ssz.altair.LightClientUpdatesByRange.deserialize(req.data);
            return onLightClientUpdatesByRange(body, chain);
        },
        [ReqRespMethod.LightClientFinalityUpdate]: () => onLightClientFinalityUpdate(chain),
        [ReqRespMethod.LightClientOptimisticUpdate]: () => onLightClientOptimisticUpdate(chain),
    };
    return (method) => handlers[method];
}
//# sourceMappingURL=index.js.map