"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.uploadWeb3Doc = exports.uploadPublicDoc = exports.getWeb3Doc = exports.getPublicDoc = exports.cellNodeChainMapping = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _axios = _interopRequireDefault(require("axios"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
/**
 * Sample: {
        key: 'lt2nc8vwbsjlfwap84z',
        contentType: 'application/ld+json',
        data: 'eyJAY29udGV4dCI6eyJpeG8iOiJodHRwczovL3czaWQub3JnL2l4by9ucy9wcm90b2NvbC8iLCJAaWQiOiJAdHlwZSIsInR5cGUiOiJAdHlwZSIsIkBwcm90ZWN0ZWQiOnRydWV9LCJ0eXBlIjoiaXhvOmVudGl0eSNwYWdlIiwicGFnZSI6eyJpZCI6IklyQ2NKOGhsOHQiLCJ0eXBlIjoicGFyYWdyYXBoIiwiZGF0YSI6eyJ0ZXh0IjoiVGhlIEltcGFjdCBEQU8gY29vcGVyYXRpdmUgbWFrZXMgb3V0Y29tZXMtYmFzZWQgaW52ZXN0bWVudHMgaW50byBpbm5vdmF0aW9ucywgcHJvamVjdHMgYW5kIHZlbnR1cmVzIHRoYXQgY2FuIGRsZWl2ZXIgSW50ZXJuZXQgb2YgSW1wYWN0IHNvbHV0aW9ucy4gTWVtYmVycyBvZiB0aGUgREFPIGFyZSBwaW9uZWVyaW5nIGlubm92YXRvcnMsIGludmVzdG9ycywgYnVpbGRlcnMsIGFuZCBpbXBsZW1lbnRvcnMgd2hvIGNvbnRyaWJ1dGUgY2FwaXRhbCwgd29yaywgb3Blbi1zb3VyY2Ugc29mdHdhcmUsIGFuZCBtYXJrZXQgYWNjZXNzIG9wcG9ydHVuaXRpZXMuIFRoZSBEQU8gdHJlYXN1cnkgcHJvdmlkZXMgZmluYW5jaW5nIGZvciB0aGUgZWNvc3lzdGVtIGJ5IGdyb3dpbmcgYSBwb3J0Zm9saW8gb2YgYXNzZXRzIGFuZCBieSBnZW5lcmF0aW5nIHJldmVudWVzIGZyb20gaXRzIGludmVzdG1lbnRzLiBUaGUgREFPIFRva2VucyByZXByZXNlbnQgZWFjaCBtZW1iZXIncyBwYXJ0aWNpcGF0aW9uIHNoYXJlIGluIHRoZSB0cmVhc3VyeSB0aGF0IGNhbiBiZSBleGNoYW5nZWQgYWZ0ZXIgdGhlIERBTyBicmVha3MgZXZlbi4gTW9yZSByZWNlbnQgaW52ZXN0b3JzIHJlY2l2ZSBhIGxpcXVpZGF0aW9uIHByZWZlcmVuY2UuIn19fQ==',
        url: 'https://devnet-cellnode.ixo.earth/public/lt2nc8vwbsjlfwap84z'
      }
 */

/**
 * Sample: {
        cid: 'bafkreieevelc4vmpo6p2lsqu2zgiuc3xatxuzoquwtvqea6zabufbxmwiu',
        name: 'atJywpxoUyHx',
        ipfs: 'bafkreieevelc4vmpo6p2lsqu2zgiuc3xatxuzoquwtvqea6zabufbxmwiu.ipfs.w3s.link',
        url: 'https://bafkreieevelc4vmpo6p2lsqu2zgiuc3xatxuzoquwtvqea6zabufbxmwiu.ipfs.w3s.link'
      }
 */

var cellNodeChainMapping = exports.cellNodeChainMapping = {
  mainnet: "https://cellnode.ixo.world",
  testnet: "https://cellnode-pandora.ixo.earth",
  devnet: "https://devnet-cellnode.ixo.earth"
};
var getPublicDoc = exports.getPublicDoc = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee(cid, cellnodeUrl) {
    var chainNetwork,
      baseUrl,
      url,
      response,
      _args = arguments;
    return _regenerator().w(function (_context) {
      while (1) switch (_context.n) {
        case 0:
          chainNetwork = _args.length > 2 && _args[2] !== undefined ? _args[2] : "mainnet";
          baseUrl = cellnodeUrl || cellNodeChainMapping[chainNetwork];
          url = "".concat(baseUrl, "/public/").concat(cid);
          _context.n = 1;
          return _axios["default"].get(url);
        case 1:
          response = _context.v;
          return _context.a(2, response.data);
      }
    }, _callee);
  }));
  return function getPublicDoc(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();
var uploadPublicDoc = exports.uploadPublicDoc = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee2(contentType, data, cellnodeUrl) {
    var chainNetwork,
      baseUrl,
      url,
      response,
      _args2 = arguments;
    return _regenerator().w(function (_context2) {
      while (1) switch (_context2.n) {
        case 0:
          chainNetwork = _args2.length > 3 && _args2[3] !== undefined ? _args2[3] : "mainnet";
          baseUrl = cellnodeUrl || cellNodeChainMapping[chainNetwork];
          url = "".concat(baseUrl, "/public/createpublic");
          _context2.n = 1;
          return _axios["default"].post(url, {
            contentType: contentType,
            data: data
          });
        case 1:
          response = _context2.v;
          return _context2.a(2, _objectSpread(_objectSpread({}, response.data), {}, {
            url: "".concat(baseUrl, "/public/").concat(response.data.key)
          }));
      }
    }, _callee2);
  }));
  return function uploadPublicDoc(_x3, _x4, _x5) {
    return _ref2.apply(this, arguments);
  };
}();
var getWeb3Doc = exports.getWeb3Doc = /*#__PURE__*/function () {
  var _ref3 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee3(cid, cellnodeUrl) {
    var chainNetwork,
      baseUrl,
      url,
      response,
      _args3 = arguments;
    return _regenerator().w(function (_context3) {
      while (1) switch (_context3.n) {
        case 0:
          chainNetwork = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : "mainnet";
          baseUrl = cellnodeUrl || cellNodeChainMapping[chainNetwork];
          url = "".concat(baseUrl, "/storage/retrieve/").concat(cid);
          _context3.n = 1;
          return _axios["default"].get(url);
        case 1:
          response = _context3.v;
          return _context3.a(2, response.data);
      }
    }, _callee3);
  }));
  return function getWeb3Doc(_x6, _x7) {
    return _ref3.apply(this, arguments);
  };
}();
var uploadWeb3Doc = exports.uploadWeb3Doc = /*#__PURE__*/function () {
  var _ref4 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee4(name, contentType, data, cellnodeUrl) {
    var chainNetwork,
      baseUrl,
      url,
      response,
      _args4 = arguments;
    return _regenerator().w(function (_context4) {
      while (1) switch (_context4.n) {
        case 0:
          chainNetwork = _args4.length > 4 && _args4[4] !== undefined ? _args4[4] : "mainnet";
          baseUrl = cellnodeUrl || cellNodeChainMapping[chainNetwork];
          url = "".concat(baseUrl, "/storage/store");
          _context4.n = 1;
          return _axios["default"].post(url, {
            name: name,
            contentType: contentType,
            data: data
          });
        case 1:
          response = _context4.v;
          return _context4.a(2, _objectSpread(_objectSpread({}, response.data), {}, {
            url: "https://".concat(response.data.ipfs)
          }));
      }
    }, _callee4);
  }));
  return function uploadWeb3Doc(_x8, _x9, _x0, _x1) {
    return _ref4.apply(this, arguments);
  };
}();