"use strict";

var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UpgradeFields = exports.Upgrade = exports.ErrorReceipt = void 0;
var _channel = require("./channel");
var _helpers = require("../../../../helpers");
var _m0 = _interopRequireWildcard(require("protobufjs/minimal"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } //@ts-nocheck
/**
 * Upgrade is a verifiable type which contains the relevant information
 * for an attempted upgrade. It provides the proposed changes to the channel
 * end, the timeout for this upgrade attempt and the next packet sequence
 * which allows the counterparty to efficiently know the highest sequence it has received.
 * The next sequence send is used for pruning and upgrading from unordered to ordered channels.
 */

/**
 * Upgrade is a verifiable type which contains the relevant information
 * for an attempted upgrade. It provides the proposed changes to the channel
 * end, the timeout for this upgrade attempt and the next packet sequence
 * which allows the counterparty to efficiently know the highest sequence it has received.
 * The next sequence send is used for pruning and upgrading from unordered to ordered channels.
 */

/**
 * UpgradeFields are the fields in a channel end which may be changed
 * during a channel upgrade.
 */

/**
 * UpgradeFields are the fields in a channel end which may be changed
 * during a channel upgrade.
 */

/**
 * ErrorReceipt defines a type which encapsulates the upgrade sequence and error associated with the
 * upgrade handshake failure. When a channel upgrade handshake is aborted both chains are expected to increment to the
 * next sequence.
 */

/**
 * ErrorReceipt defines a type which encapsulates the upgrade sequence and error associated with the
 * upgrade handshake failure. When a channel upgrade handshake is aborted both chains are expected to increment to the
 * next sequence.
 */

function createBaseUpgrade() {
  return {
    fields: undefined,
    timeout: undefined,
    nextSequenceSend: _helpers.Long.UZERO
  };
}
var Upgrade = exports.Upgrade = {
  encode: function encode(message) {
    var writer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _m0.Writer.create();
    if (message.fields !== undefined) {
      UpgradeFields.encode(message.fields, writer.uint32(10).fork()).ldelim();
    }
    if (message.timeout !== undefined) {
      _channel.Timeout.encode(message.timeout, writer.uint32(18).fork()).ldelim();
    }
    if (!message.nextSequenceSend.isZero()) {
      writer.uint32(24).uint64(message.nextSequenceSend);
    }
    return writer;
  },
  decode: function decode(input, length) {
    var reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    var end = length === undefined ? reader.len : reader.pos + length;
    var message = createBaseUpgrade();
    while (reader.pos < end) {
      var tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.fields = UpgradeFields.decode(reader, reader.uint32());
          break;
        case 2:
          message.timeout = _channel.Timeout.decode(reader, reader.uint32());
          break;
        case 3:
          message.nextSequenceSend = reader.uint64();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromJSON: function fromJSON(object) {
    return {
      fields: (0, _helpers.isSet)(object.fields) ? UpgradeFields.fromJSON(object.fields) : undefined,
      timeout: (0, _helpers.isSet)(object.timeout) ? _channel.Timeout.fromJSON(object.timeout) : undefined,
      nextSequenceSend: (0, _helpers.isSet)(object.nextSequenceSend) ? _helpers.Long.fromValue(object.nextSequenceSend) : _helpers.Long.UZERO
    };
  },
  toJSON: function toJSON(message) {
    var obj = {};
    message.fields !== undefined && (obj.fields = message.fields ? UpgradeFields.toJSON(message.fields) : undefined);
    message.timeout !== undefined && (obj.timeout = message.timeout ? _channel.Timeout.toJSON(message.timeout) : undefined);
    message.nextSequenceSend !== undefined && (obj.nextSequenceSend = (message.nextSequenceSend || _helpers.Long.UZERO).toString());
    return obj;
  },
  fromPartial: function fromPartial(object) {
    var message = createBaseUpgrade();
    message.fields = object.fields !== undefined && object.fields !== null ? UpgradeFields.fromPartial(object.fields) : undefined;
    message.timeout = object.timeout !== undefined && object.timeout !== null ? _channel.Timeout.fromPartial(object.timeout) : undefined;
    message.nextSequenceSend = object.nextSequenceSend !== undefined && object.nextSequenceSend !== null ? _helpers.Long.fromValue(object.nextSequenceSend) : _helpers.Long.UZERO;
    return message;
  }
};
function createBaseUpgradeFields() {
  return {
    ordering: 0,
    connectionHops: [],
    version: ""
  };
}
var UpgradeFields = exports.UpgradeFields = {
  encode: function encode(message) {
    var writer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _m0.Writer.create();
    if (message.ordering !== 0) {
      writer.uint32(8).int32(message.ordering);
    }
    var _iterator = _createForOfIteratorHelper(message.connectionHops),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var v = _step.value;
        writer.uint32(18).string(v);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    if (message.version !== "") {
      writer.uint32(26).string(message.version);
    }
    return writer;
  },
  decode: function decode(input, length) {
    var reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    var end = length === undefined ? reader.len : reader.pos + length;
    var message = createBaseUpgradeFields();
    while (reader.pos < end) {
      var tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.ordering = reader.int32();
          break;
        case 2:
          message.connectionHops.push(reader.string());
          break;
        case 3:
          message.version = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromJSON: function fromJSON(object) {
    return {
      ordering: (0, _helpers.isSet)(object.ordering) ? (0, _channel.orderFromJSON)(object.ordering) : 0,
      connectionHops: Array.isArray(object === null || object === void 0 ? void 0 : object.connectionHops) ? object.connectionHops.map(function (e) {
        return String(e);
      }) : [],
      version: (0, _helpers.isSet)(object.version) ? String(object.version) : ""
    };
  },
  toJSON: function toJSON(message) {
    var obj = {};
    message.ordering !== undefined && (obj.ordering = (0, _channel.orderToJSON)(message.ordering));
    if (message.connectionHops) {
      obj.connectionHops = message.connectionHops.map(function (e) {
        return e;
      });
    } else {
      obj.connectionHops = [];
    }
    message.version !== undefined && (obj.version = message.version);
    return obj;
  },
  fromPartial: function fromPartial(object) {
    var _object$ordering, _object$connectionHop, _object$version;
    var message = createBaseUpgradeFields();
    message.ordering = (_object$ordering = object.ordering) !== null && _object$ordering !== void 0 ? _object$ordering : 0;
    message.connectionHops = ((_object$connectionHop = object.connectionHops) === null || _object$connectionHop === void 0 ? void 0 : _object$connectionHop.map(function (e) {
      return e;
    })) || [];
    message.version = (_object$version = object.version) !== null && _object$version !== void 0 ? _object$version : "";
    return message;
  }
};
function createBaseErrorReceipt() {
  return {
    sequence: _helpers.Long.UZERO,
    message: ""
  };
}
var ErrorReceipt = exports.ErrorReceipt = {
  encode: function encode(message) {
    var writer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _m0.Writer.create();
    if (!message.sequence.isZero()) {
      writer.uint32(8).uint64(message.sequence);
    }
    if (message.message !== "") {
      writer.uint32(18).string(message.message);
    }
    return writer;
  },
  decode: function decode(input, length) {
    var reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    var end = length === undefined ? reader.len : reader.pos + length;
    var message = createBaseErrorReceipt();
    while (reader.pos < end) {
      var tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.sequence = reader.uint64();
          break;
        case 2:
          message.message = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromJSON: function fromJSON(object) {
    return {
      sequence: (0, _helpers.isSet)(object.sequence) ? _helpers.Long.fromValue(object.sequence) : _helpers.Long.UZERO,
      message: (0, _helpers.isSet)(object.message) ? String(object.message) : ""
    };
  },
  toJSON: function toJSON(message) {
    var obj = {};
    message.sequence !== undefined && (obj.sequence = (message.sequence || _helpers.Long.UZERO).toString());
    message.message !== undefined && (obj.message = message.message);
    return obj;
  },
  fromPartial: function fromPartial(object) {
    var _object$message;
    var message = createBaseErrorReceipt();
    message.sequence = object.sequence !== undefined && object.sequence !== null ? _helpers.Long.fromValue(object.sequence) : _helpers.Long.UZERO;
    message.message = (_object$message = object.message) !== null && _object$message !== void 0 ? _object$message : "";
    return message;
  }
};