import { Binary } from 'polkadot-api';
import { mapObject } from 'polkadot-api/utils';
import { firstValueFrom } from 'rxjs';
import { getParams, respond } from './rpc_utils.js';

const dev_newBlock = async (con, req, { newBlock }) => {
  const { params } = getParams(req, ["params"]);
  const hash = await newBlock(
    params ? {
      ...params,
      storage: params.storage && mapObject(
        params.storage,
        (v) => v == null ? null : Binary.fromHex(v)
      ),
      transactions: params.transactions?.map(Binary.fromHex)
    } : {}
  );
  con.send(respond(req, hash));
};
const dev_setStorage = async (con, req, { chain }) => {
  const { storageValues, blockHash } = getParams(req, [
    "storageValues",
    "blockHash"
  ]);
  let targetHash = blockHash ?? await firstValueFrom(chain.best$);
  chain.setStorage(
    targetHash,
    Object.fromEntries(
      storageValues.map(([key, value]) => [
        key,
        value ? Binary.fromHex(value) : null
      ])
    )
  );
  con.send(respond(req, targetHash));
};

export { dev_newBlock, dev_setStorage };
//# sourceMappingURL=dev.js.map
