import { isForkPostAltair } from "@lodestar/params";
import { ssz, sszTypesFor, } from "@lodestar/types";
import { BeaconBlocksByRootRequestType, BlobSidecarsByRootRequestType, DataColumnSidecarsByRootRequestType, } from "../../util/types.js";
export { ReqRespMethod };
/** ReqResp protocol names or methods. Each ReqRespMethod can have multiple versions and encodings */
var ReqRespMethod;
(function (ReqRespMethod) {
    // Phase 0
    ReqRespMethod["Status"] = "status";
    ReqRespMethod["Goodbye"] = "goodbye";
    ReqRespMethod["Ping"] = "ping";
    ReqRespMethod["Metadata"] = "metadata";
    ReqRespMethod["BeaconBlocksByRange"] = "beacon_blocks_by_range";
    ReqRespMethod["BeaconBlocksByRoot"] = "beacon_blocks_by_root";
    ReqRespMethod["BlobSidecarsByRange"] = "blob_sidecars_by_range";
    ReqRespMethod["BlobSidecarsByRoot"] = "blob_sidecars_by_root";
    ReqRespMethod["DataColumnSidecarsByRange"] = "data_column_sidecars_by_range";
    ReqRespMethod["DataColumnSidecarsByRoot"] = "data_column_sidecars_by_root";
    ReqRespMethod["LightClientBootstrap"] = "light_client_bootstrap";
    ReqRespMethod["LightClientUpdatesByRange"] = "light_client_updates_by_range";
    ReqRespMethod["LightClientFinalityUpdate"] = "light_client_finality_update";
    ReqRespMethod["LightClientOptimisticUpdate"] = "light_client_optimistic_update";
})(ReqRespMethod || (ReqRespMethod = {}));
/** Request SSZ type for each method and ForkName */
export const requestSszTypeByMethod = (fork, config) => ({
    // Status type should ideally be determined by protocol version and not fork but since
    // we only start using the new status version after the fork this is not an issue
    [ReqRespMethod.Status]: sszTypesFor(fork).Status,
    [ReqRespMethod.Goodbye]: ssz.phase0.Goodbye,
    [ReqRespMethod.Ping]: ssz.phase0.Ping,
    [ReqRespMethod.Metadata]: null,
    [ReqRespMethod.BeaconBlocksByRange]: ssz.phase0.BeaconBlocksByRangeRequest,
    [ReqRespMethod.BeaconBlocksByRoot]: BeaconBlocksByRootRequestType(fork, config),
    [ReqRespMethod.BlobSidecarsByRange]: ssz.deneb.BlobSidecarsByRangeRequest,
    [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(fork, config),
    [ReqRespMethod.DataColumnSidecarsByRange]: ssz.fulu.DataColumnSidecarsByRangeRequest,
    [ReqRespMethod.DataColumnSidecarsByRoot]: DataColumnSidecarsByRootRequestType(config),
    [ReqRespMethod.LightClientBootstrap]: ssz.Root,
    [ReqRespMethod.LightClientUpdatesByRange]: ssz.altair.LightClientUpdatesByRange,
    [ReqRespMethod.LightClientFinalityUpdate]: null,
    [ReqRespMethod.LightClientOptimisticUpdate]: null,
});
const blocksResponseType = (fork, version) => {
    if (version === Version.V1) {
        return ssz.phase0.SignedBeaconBlock;
    }
    return ssz[fork].SignedBeaconBlock;
};
export const responseSszTypeByMethod = {
    [ReqRespMethod.Status]: (_, version) => (version === Version.V2 ? ssz.fulu.Status : ssz.phase0.Status),
    [ReqRespMethod.Goodbye]: () => ssz.phase0.Goodbye,
    [ReqRespMethod.Ping]: () => ssz.phase0.Ping,
    [ReqRespMethod.Metadata]: (_, version) => version === Version.V1 ? ssz.phase0.Metadata : version === Version.V2 ? ssz.altair.Metadata : ssz.fulu.Metadata,
    [ReqRespMethod.BeaconBlocksByRange]: blocksResponseType,
    [ReqRespMethod.BeaconBlocksByRoot]: blocksResponseType,
    [ReqRespMethod.BlobSidecarsByRange]: () => ssz.deneb.BlobSidecar,
    [ReqRespMethod.BlobSidecarsByRoot]: () => ssz.deneb.BlobSidecar,
    [ReqRespMethod.LightClientBootstrap]: (fork) => sszTypesFor(onlyPostAltairFork(fork)).LightClientBootstrap,
    [ReqRespMethod.LightClientUpdatesByRange]: (fork) => sszTypesFor(onlyPostAltairFork(fork)).LightClientUpdate,
    [ReqRespMethod.LightClientFinalityUpdate]: (fork) => sszTypesFor(onlyPostAltairFork(fork)).LightClientFinalityUpdate,
    [ReqRespMethod.DataColumnSidecarsByRange]: () => ssz.fulu.DataColumnSidecar,
    [ReqRespMethod.DataColumnSidecarsByRoot]: () => ssz.fulu.DataColumnSidecar,
    [ReqRespMethod.LightClientOptimisticUpdate]: (fork) => sszTypesFor(onlyPostAltairFork(fork)).LightClientOptimisticUpdate,
};
function onlyPostAltairFork(fork) {
    if (isForkPostAltair(fork)) {
        return fork;
    }
    throw Error(`Not a post-altair fork ${fork}`);
}
export { Version };
var Version;
(function (Version) {
    Version[Version["V1"] = 1] = "V1";
    Version[Version["V2"] = 2] = "V2";
    Version[Version["V3"] = 3] = "V3";
})(Version || (Version = {}));
//# sourceMappingURL=types.js.map