import { HttpClient } from "@cosmjs/tendermint-rpc";
import { createJsonRpcRequest, encodeHeightParam, decodeBlockResults } from "../utils/tendermintClientHelpers";

/**
 * Gets the block results from the Comet38 node
 * Adjusted for IXO blocksync-core
 * @param endpoint - The endpoint of the Comet38 node
 * @returns - The block results
 */
export const blockResults = async (height, endpoint, includeResults = false) => {
  // only allow endpoiints as string and starting with http
  if (typeof endpoint !== "string" || !endpoint.startsWith("http")) {
    throw new Error("Invalid endpoint");
  }
  if (!height) {
    throw new Error("Invalid height");
  }
  const query = {
    method: "block_results",
    params: {
      height: height
    }
  };
  const req = createJsonRpcRequest(query.method, encodeHeightParam(query.params));
  const result = await new HttpClient(endpoint).execute(req);
  if (!result.result) {
    var _result$message;
    throw new Error((_result$message = result.message) !== null && _result$message !== void 0 ? _result$message : "No RPC result");
  }
  // console.dir(result.result, { depth: null });
  return decodeBlockResults(result.result, includeResults);
};