'use strict';

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('./tslib.es6-0d92ef81.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

function autoScroll(element, page) {
  var bottomTimes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return page.evaluate(function (_ref) {
            var element = _ref.element,
              bottomTimes = _ref.bottomTimes;
            return new Promise(function (resolve) {
              var UNIT_DISTANCE = 100;
              var ele = document.querySelector(element);
              var totalHeight = 0;
              var curBottomTimes = 0;
              var lastScrollHeight = ele === null || ele === void 0 ? void 0 : ele.scrollHeight;
              var doScroll = function doScroll(ele) {
                totalHeight = ele.scrollTop + UNIT_DISTANCE;
                ele.scrollTop = totalHeight;
              };
              var timer = setInterval(function () {
                var scrollHeight = ele.scrollHeight;
                doScroll(ele);
                if (scrollHeight > lastScrollHeight) {
                  curBottomTimes += 1;
                  lastScrollHeight = scrollHeight;
                }
                console.log('[autoScroll] scrollHeight & totalHeight', scrollHeight, totalHeight);
                if (totalHeight >= scrollHeight || bottomTimes && curBottomTimes > bottomTimes) {
                  console.log('[autoScroll] end');
                  clearInterval(timer);
                  resolve(1);
                }
              }, 100);
            });
          }, {
            element: element,
            bottomTimes: bottomTimes
          });
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

var scroll = /*#__PURE__*/Object.freeze({
    __proto__: null,
    autoScroll: autoScroll
});

exports.autoScroll = autoScroll;
exports.scroll = scroll;
