import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from './tslib.es6-848120af.js';

function autoScroll(element, page) {
  var bottomTimes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    return _regeneratorRuntime.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
});

export { autoScroll as a, scroll as s };
