"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.base64FromBytes = base64FromBytes;
exports.bytesFromBase64 = bytesFromBase64;
exports.fromDuration = fromDuration;
exports.fromJsonTimestamp = fromJsonTimestamp;
exports.fromTimestamp = fromTimestamp;
exports.isObject = isObject;
exports.isSet = isSet;
exports.omitDefault = omitDefault;
exports.setPaginationParams = void 0;
exports.toDuration = toDuration;
exports.toTimestamp = toTimestamp;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.5.4
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or yarn proto command to regenerate this bundle.
*/

var globalThis = function () {
  if (typeof globalThis !== 'undefined') return globalThis;
  if (typeof self !== 'undefined') return self;
  if (typeof window !== 'undefined') return window;
  if (typeof global !== 'undefined') return global;
  throw 'Unable to locate global object';
}();
var atob = globalThis.atob || function (b64) {
  return globalThis.Buffer.from(b64, 'base64').toString('binary');
};
function bytesFromBase64(b64) {
  var bin = atob(b64);
  var arr = new Uint8Array(bin.length);
  for (var i = 0; i < bin.length; ++i) {
    arr[i] = bin.charCodeAt(i);
  }
  return arr;
}
var btoa = globalThis.btoa || function (bin) {
  return globalThis.Buffer.from(bin, 'binary').toString('base64');
};
function base64FromBytes(arr) {
  var bin = [];
  arr.forEach(function (_byte) {
    bin.push(String.fromCharCode(_byte));
  });
  return btoa(bin.join(''));
}
function omitDefault(input) {
  if (typeof input === 'string') {
    return input === '' ? undefined : input;
  }
  if (typeof input === 'number') {
    return input === 0 ? undefined : input;
  }
  if (typeof input === "boolean") {
    return input === false ? undefined : input;
  }
  if (typeof input === 'bigint') {
    return input === BigInt(0) ? undefined : input;
  }
  throw new Error("Got unsupported type ".concat((0, _typeof2["default"])(input)));
}
function toDuration(duration) {
  return {
    seconds: BigInt(Math.floor(parseInt(duration) / 1000000000)),
    nanos: parseInt(duration) % 1000000000
  };
}
function fromDuration(duration) {
  return (parseInt(duration.seconds.toString()) * 1000000000 + duration.nanos).toString();
}
function isSet(value) {
  return value !== null && value !== undefined;
}
function isObject(value) {
  return (0, _typeof2["default"])(value) === 'object' && value !== null;
}
var setPaginationParams = exports.setPaginationParams = function setPaginationParams(options, pagination) {
  if (!pagination) {
    return options;
  }
  if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.countTotal) !== 'undefined') {
    options.params['pagination.count_total'] = pagination.countTotal;
  }
  if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.key) !== 'undefined') {
    // String to Uint8Array
    // let uint8arr = new Uint8Array(Buffer.from(data,'base64'));

    // Uint8Array to String
    options.params['pagination.key'] = Buffer.from(pagination.key).toString('base64');
  }
  if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.limit) !== 'undefined') {
    options.params['pagination.limit'] = pagination.limit.toString();
  }
  if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.offset) !== 'undefined') {
    options.params['pagination.offset'] = pagination.offset.toString();
  }
  if (typeof (pagination === null || pagination === void 0 ? void 0 : pagination.reverse) !== 'undefined') {
    options.params['pagination.reverse'] = pagination.reverse;
  }
  return options;
};
function toTimestamp(date) {
  var seconds = numberToLong(date.getTime() / 1000);
  var nanos = date.getTime() % 1000 * 1000000;
  return {
    seconds: seconds,
    nanos: nanos
  };
}
function fromTimestamp(t) {
  var millis = Number(t.seconds) * 1000;
  millis += t.nanos / 1000000;
  return new Date(millis);
}
var timestampFromJSON = function timestampFromJSON(object) {
  return {
    seconds: isSet(object.seconds) ? BigInt(object.seconds.toString()) : BigInt(0),
    nanos: isSet(object.nanos) ? Number(object.nanos) : 0
  };
};
function fromJsonTimestamp(o) {
  if (o instanceof Date) {
    return toTimestamp(o);
  } else if (typeof o === 'string') {
    return toTimestamp(new Date(o));
  } else {
    return timestampFromJSON(o);
  }
}
function numberToLong(number) {
  return BigInt(Math.trunc(number));
}