// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
//   protoc-gen-ts_proto  v2.11.4
//   protoc               unknown
// source: cheqd/oracle/v2/abci.proto

/* eslint-disable */
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
import { DecCoin } from "../../../cosmos/base/v1beta1/coin.js";
import { AggregateExchangeRateVote } from "./oracle.js";

/**
 * OracleVoteExtension defines the vote extension structure used by the oracle
 * module.
 */
export interface OracleVoteExtension {
  height: bigint;
  exchangeRates: DecCoin[];
}

/**
 * InjectedVoteExtensionTx defines the vote extension tx injected by the prepare
 * proposal handler.
 */
export interface InjectedVoteExtensionTx {
  exchangeRateVotes: AggregateExchangeRateVote[];
  extendedCommitInfo: Uint8Array;
}

function createBaseOracleVoteExtension(): OracleVoteExtension {
  return { height: 0n, exchangeRates: [] };
}

export const OracleVoteExtension: MessageFns<OracleVoteExtension> = {
  encode(message: OracleVoteExtension, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.height !== 0n) {
      if (BigInt.asIntN(64, message.height) !== message.height) {
        throw new globalThis.Error("value provided for field message.height of type int64 too large");
      }
      writer.uint32(8).int64(message.height);
    }
    for (const v of message.exchangeRates) {
      DecCoin.encode(v!, writer.uint32(18).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): OracleVoteExtension {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseOracleVoteExtension();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 8) {
            break;
          }

          message.height = reader.int64() as bigint;
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.exchangeRates.push(DecCoin.decode(reader, reader.uint32()));
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): OracleVoteExtension {
    return {
      height: isSet(object.height) ? BigInt(object.height) : 0n,
      exchangeRates: globalThis.Array.isArray(object?.exchangeRates)
        ? object.exchangeRates.map((e: any) => DecCoin.fromJSON(e))
        : globalThis.Array.isArray(object?.exchange_rates)
        ? object.exchange_rates.map((e: any) => DecCoin.fromJSON(e))
        : [],
    };
  },

  toJSON(message: OracleVoteExtension): unknown {
    const obj: any = {};
    if (message.height !== 0n) {
      obj.height = message.height.toString();
    }
    if (message.exchangeRates?.length) {
      obj.exchangeRates = message.exchangeRates.map((e) => DecCoin.toJSON(e));
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<OracleVoteExtension>, I>>(base?: I): OracleVoteExtension {
    return OracleVoteExtension.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<OracleVoteExtension>, I>>(object: I): OracleVoteExtension {
    const message = createBaseOracleVoteExtension();
    message.height = object.height ?? 0n;
    message.exchangeRates = object.exchangeRates?.map((e) => DecCoin.fromPartial(e)) || [];
    return message;
  },
};

function createBaseInjectedVoteExtensionTx(): InjectedVoteExtensionTx {
  return { exchangeRateVotes: [], extendedCommitInfo: new Uint8Array(0) };
}

export const InjectedVoteExtensionTx: MessageFns<InjectedVoteExtensionTx> = {
  encode(message: InjectedVoteExtensionTx, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.exchangeRateVotes) {
      AggregateExchangeRateVote.encode(v!, writer.uint32(10).fork()).join();
    }
    if (message.extendedCommitInfo.length !== 0) {
      writer.uint32(18).bytes(message.extendedCommitInfo);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): InjectedVoteExtensionTx {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseInjectedVoteExtensionTx();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.exchangeRateVotes.push(AggregateExchangeRateVote.decode(reader, reader.uint32()));
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.extendedCommitInfo = reader.bytes();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): InjectedVoteExtensionTx {
    return {
      exchangeRateVotes: globalThis.Array.isArray(object?.exchangeRateVotes)
        ? object.exchangeRateVotes.map((e: any) => AggregateExchangeRateVote.fromJSON(e))
        : globalThis.Array.isArray(object?.exchange_rate_votes)
        ? object.exchange_rate_votes.map((e: any) => AggregateExchangeRateVote.fromJSON(e))
        : [],
      extendedCommitInfo: isSet(object.extendedCommitInfo)
        ? bytesFromBase64(object.extendedCommitInfo)
        : isSet(object.extended_commit_info)
        ? bytesFromBase64(object.extended_commit_info)
        : new Uint8Array(0),
    };
  },

  toJSON(message: InjectedVoteExtensionTx): unknown {
    const obj: any = {};
    if (message.exchangeRateVotes?.length) {
      obj.exchangeRateVotes = message.exchangeRateVotes.map((e) => AggregateExchangeRateVote.toJSON(e));
    }
    if (message.extendedCommitInfo.length !== 0) {
      obj.extendedCommitInfo = base64FromBytes(message.extendedCommitInfo);
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<InjectedVoteExtensionTx>, I>>(base?: I): InjectedVoteExtensionTx {
    return InjectedVoteExtensionTx.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<InjectedVoteExtensionTx>, I>>(object: I): InjectedVoteExtensionTx {
    const message = createBaseInjectedVoteExtensionTx();
    message.exchangeRateVotes = object.exchangeRateVotes?.map((e) => AggregateExchangeRateVote.fromPartial(e)) || [];
    message.extendedCommitInfo = object.extendedCommitInfo ?? new Uint8Array(0);
    return message;
  },
};

function bytesFromBase64(b64: string): Uint8Array {
  if ((globalThis as any).Buffer) {
    return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
  } else {
    const bin = globalThis.atob(b64);
    const arr = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; ++i) {
      arr[i] = bin.charCodeAt(i);
    }
    return arr;
  }
}

function base64FromBytes(arr: Uint8Array): string {
  if ((globalThis as any).Buffer) {
    return globalThis.Buffer.from(arr).toString("base64");
  } else {
    const bin: string[] = [];
    arr.forEach((byte) => {
      bin.push(globalThis.String.fromCharCode(byte));
    });
    return globalThis.btoa(bin.join(""));
  }
}

type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined;

type DeepPartial<T> = T extends Builtin ? T
  : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
  : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
  : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
  : Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
type Exact<P, I extends P> = P extends Builtin ? P
  : P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}

interface MessageFns<T> {
  encode(message: T, writer?: BinaryWriter): BinaryWriter;
  decode(input: BinaryReader | Uint8Array, length?: number): T;
  fromJSON(object: any): T;
  toJSON(message: T): unknown;
  create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
  fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
}
