"use strict";

var _assert = _interopRequireDefault(require("assert"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

// Custom RPC commands that are exposed on top of c-lightning's built-in ones
module.exports = function (ln) {
  return {
    connectfund: function () {
      var _connectfund = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(peeruri, satoshi, feerate) {
        var peerid, res;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                peerid = peeruri.split('@')[0];
                _context.next = 3;
                return ln.connect(peeruri);

              case 3:
                _context.next = 5;
                return ln.fundchannel(peerid, satoshi, feerate);

              case 5:
                res = _context.sent;
                (0, _assert["default"])(res && res.channel_id, 'cannot open channel');
                return _context.abrupt("return", getChannel(ln, peerid, res.channel_id));

              case 8:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      function connectfund(_x, _x2, _x3) {
        return _connectfund.apply(this, arguments);
      }

      return connectfund;
    }(),
    closeget: function () {
      var _closeget = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(peerid, chanid, force, timeout) {
        var res, _yield$getChannel, peer, chan;

        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return ln.close(chanid, force, timeout);

              case 2:
                res = _context2.sent;
                (0, _assert["default"])(res && res.txid, 'cannot close channel');
                _context2.next = 6;
                return getChannel(ln, peerid, chanid);

              case 6:
                _yield$getChannel = _context2.sent;
                peer = _yield$getChannel.peer;
                chan = _yield$getChannel.chan;
                return _context2.abrupt("return", {
                  peer: peer,
                  chan: chan,
                  closing: res
                });

              case 10:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      function closeget(_x4, _x5, _x6, _x7) {
        return _closeget.apply(this, arguments);
      }

      return closeget;
    }()
  };
};

var getChannel = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(ln, peerid, chanid) {
    var peer, chan;
    return regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.next = 2;
            return ln.listpeers(peerid).then(function (r) {
              return r.peers[0];
            });

          case 2:
            peer = _context3.sent;
            (0, _assert["default"])(peer, 'cannot find peer');
            chan = peer.channels.find(function (chan) {
              return chan.channel_id == chanid;
            });
            (0, _assert["default"])(chan, 'cannot find channel');
            delete peer.channels;
            return _context3.abrupt("return", {
              peer: peer,
              chan: chan
            });

          case 8:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));

  return function getChannel(_x8, _x9, _x10) {
    return _ref.apply(this, arguments);
  };
}();