import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

var getYoutubeVideoInformation = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(remote, splittedURLsArray) {
    var progressCallback,
        ytdl,
        checkedVideos,
        _loop,
        i,
        _args2 = arguments;

    return _regeneratorRuntime.wrap(function _callee$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            progressCallback = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : function () {
              return null;
            };
            ytdl = remote.require("ytdl-core");
            checkedVideos = [];

            if (!(splittedURLsArray.length > 0)) {
              _context2.next = 11;
              break;
            }

            _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop(i) {
              var url, video;
              return _regeneratorRuntime.wrap(function _loop$(_context) {
                while (1) {
                  switch (_context.prev = _context.next) {
                    case 0:
                      url = splittedURLsArray[i];
                      _context.next = 3;
                      return new Promise(function (resolve, reject) {
                        ytdl.getBasicInfo(url, function (err, info) {
                          if (info && !err) {
                            resolve({
                              url: url,
                              title: info.title
                            });
                          } else {
                            var errorText = "Error with video at \"".concat(url, "\"\n\n").concat(err.toString());
                            reject(new Error(errorText));
                          }
                        });
                      });

                    case 3:
                      video = _context.sent;
                      checkedVideos.push(video);
                      progressCallback({
                        progress: i / (splittedURLsArray.length - 1) * 100,
                        text: "Inspecting Video Information..."
                      });

                    case 6:
                    case "end":
                      return _context.stop();
                  }
                }
              }, _loop);
            });
            i = 0;

          case 6:
            if (!(i < splittedURLsArray.length)) {
              _context2.next = 11;
              break;
            }

            return _context2.delegateYield(_loop(i), "t0", 8);

          case 8:
            i++;
            _context2.next = 6;
            break;

          case 11:
            return _context2.abrupt("return", checkedVideos);

          case 12:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee);
  }));

  return function getYoutubeVideoInformation(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

export default getYoutubeVideoInformation;