import '@polkadot-api/substrate-client';
import { filterObject } from 'polkadot-api/utils';
import { Subject } from 'rxjs';
import { logger } from './logger.js';
import { archive_v1_storageDiff, archive_v1_storage, archive_v1_stopStorageDiff, archive_v1_stopStorage, archive_v1_header, archive_v1_hashByHeight, archive_v1_genesisHash, archive_v1_finalizedHeight, archive_v1_call, archive_v1_body } from './rpc/archive_v1.js';
import { chainHead_v1_unpin, chainHead_v1_unfollow, chainHead_v1_storage, chainHead_v1_stopOperation, chainHead_v1_header, chainHead_v1_follow, chainHead_v1_call, chainHead_v1_body } from './rpc/chainHead_v1.js';
import { chainSpec_v1_properties, chainSpec_v1_genesisHash, chainSpec_v1_chainName } from './rpc/chainSpec_v1.js';
import { dev_setStorage, dev_newBlock } from './rpc/dev.js';
import { forklift_xcm_push_ump, forklift_xcm_push_hrmp, forklift_xcm_open_hrmp_channel, forklift_xcm_consume_dmp, forklift_xcm_attach_sibling, forklift_xcm_attach_relay } from './rpc/forklift_xcm.js';
import { transaction_v1_stop, transaction_v1_broadcast } from './rpc/transaction_v1.js';

const log = logger.child({ module: "serve" });
const methods = {
  archive_v1_body,
  archive_v1_call,
  archive_v1_finalizedHeight,
  archive_v1_genesisHash,
  archive_v1_hashByHeight,
  archive_v1_header,
  archive_v1_stopStorage,
  archive_v1_stopStorageDiff,
  archive_v1_storage,
  archive_v1_storageDiff,
  chainHead_v1_body,
  chainHead_v1_call,
  chainHead_v1_follow,
  chainHead_v1_header,
  chainHead_v1_stopOperation,
  chainHead_v1_storage,
  chainHead_v1_unfollow,
  chainHead_v1_unpin,
  chainSpec_v1_chainName,
  chainSpec_v1_genesisHash,
  chainSpec_v1_properties,
  dev_newBlock,
  dev_setStorage,
  forklift_xcm_attach_relay,
  forklift_xcm_attach_sibling,
  forklift_xcm_consume_dmp,
  forklift_xcm_open_hrmp_channel,
  forklift_xcm_push_hrmp,
  forklift_xcm_push_ump,
  transaction_v1_broadcast,
  transaction_v1_stop
};
const createServer = (ctx, rpcOverrides) => {
  const getActiveMethods = (overrides) => filterObject(
    {
      ...methods,
      ...overrides
    },
    (v) => v != null
  );
  let activeMethods = getActiveMethods(rpcOverrides);
  const provider = (send) => {
    const disconnect = new Subject();
    const con = {
      send,
      disconnect$: disconnect.asObservable(),
      context: {
        chainHead_v1_subs: {},
        archive_v1_storage_subs: {}
      }
    };
    return {
      disconnect() {
      },
      async send(req) {
        if (req.method === "rpc_methods") {
          return send({
            jsonrpc: "2.0",
            id: req.id,
            result: { methods: Object.keys(activeMethods) }
          });
        }
        const method = activeMethods[req.method];
        if (method) {
          method(con, req, { ...ctx, provider });
        } else {
          log.warn({ method: req.method }, "unknown RPC method");
          send({
            jsonrpc: "2.0",
            id: req.id,
            error: {
              code: -32601,
              message: "Method not found"
            }
          });
        }
      }
    };
  };
  return Object.assign(provider, {
    setRpcOverrides: (rpcOverrides2) => {
      activeMethods = getActiveMethods(rpcOverrides2);
    }
  });
};

export { createServer, methods };
//# sourceMappingURL=serve.js.map
