'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);

var DEFAULT_TIMEOUT = 6000;
function waitEle(element, page) {
  var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_TIMEOUT;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var dom;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          _context.next = 1;
          return page.waitForSelector(element, {
            timeout: timeout
          });
        case 1:
          dom = _context.sent;
          _context.next = 3;
          break;
        case 2:
          _context.prev = 2;
          _context["catch"](0);
        case 3:
          return _context.abrupt("return", dom);
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[0, 2]]);
  }));
}
function clickBtn(btn) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          if (!btn) {
            _context2.next = 2;
            break;
          }
          _context2.next = 1;
          return btn.evaluate(function (b) {
            return b.click();
          });
        case 1:
          return _context2.abrupt("return", true);
        case 2:
          return _context2.abrupt("return", false);
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
function findAndClick(element, page) {
  var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_TIMEOUT;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var btn;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 1;
          return waitEle(element, page, timeout);
        case 1:
          btn = _context3.sent;
          _context3.next = 2;
          return clickBtn(btn);
        case 2:
          return _context3.abrupt("return", btn);
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}
function justWait(time) {
  return new Promise(function (resolve) {
    return setTimeout(resolve, time);
  });
}
function closeBlankPage(browser) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
    var BLANK_PAGE_HREF, target, page;
    return _regeneratorRuntime__default["default"].wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          BLANK_PAGE_HREF = 'about:blank';
          _context4.next = 1;
          return browser.waitForTarget(function (t) {
            return t.url() === BLANK_PAGE_HREF;
          }, {
            timeout: DEFAULT_TIMEOUT
          });
        case 1:
          target = _context4.sent;
          if (target) {
            _context4.next = 2;
            break;
          }
          return _context4.abrupt("return");
        case 2:
          _context4.next = 3;
          return target.page();
        case 3:
          page = _context4.sent;
          if (page) {
            _context4.next = 4;
            break;
          }
          return _context4.abrupt("return");
        case 4:
          page.close();
        case 5:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  }));
}
function getRect(element, page) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5() {
    return _regeneratorRuntime__default["default"].wrap(function (_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          if (!(typeof element === 'string')) {
            _context5.next = 2;
            break;
          }
          _context5.next = 1;
          return waitEle(element, page);
        case 1:
          element = _context5.sent;
        case 2:
          _context5.next = 3;
          return page.evaluate(function (ele) {
            var _ele$getBoundingClien = ele.getBoundingClientRect(),
              top = _ele$getBoundingClien.top,
              left = _ele$getBoundingClien.left,
              bottom = _ele$getBoundingClien.bottom,
              right = _ele$getBoundingClien.right,
              x = _ele$getBoundingClien.x,
              y = _ele$getBoundingClien.y,
              width = _ele$getBoundingClien.width,
              height = _ele$getBoundingClien.height;
            return {
              top: top,
              left: left,
              bottom: bottom,
              right: right,
              x: x,
              y: y,
              width: width,
              height: height
            };
          }, element);
        case 3:
          return _context5.abrupt("return", _context5.sent);
        case 4:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  }));
}
function getInnerText(element, page) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee6() {
    var ele, text;
    return _regeneratorRuntime__default["default"].wrap(function (_context6) {
      while (1) switch (_context6.prev = _context6.next) {
        case 0:
          _context6.next = 1;
          return waitEle(element, page);
        case 1:
          ele = _context6.sent;
          if (ele) {
            _context6.next = 2;
            break;
          }
          return _context6.abrupt("return", null);
        case 2:
          _context6.next = 3;
          return page.evaluate(function (ele) {
            return ele.innerText;
          }, ele);
        case 3:
          text = _context6.sent;
          return _context6.abrupt("return", text);
        case 4:
        case "end":
          return _context6.stop();
      }
    }, _callee6);
  }));
}
function findListItemAndClick(_ref) {
  var page = _ref.page,
    element = _ref.element,
    innerText = _ref.innerText;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee7() {
    return _regeneratorRuntime__default["default"].wrap(function (_context7) {
      while (1) switch (_context7.prev = _context7.next) {
        case 0:
          _context7.next = 1;
          return page.evaluate(function (_ref2) {
            var element = _ref2.element,
              innerText = _ref2.innerText;
            var buttons = document.querySelectorAll(element);
            var btn = Array.from(buttons).find(function (item) {
              return item.innerText === innerText;
            });
            if (btn) {
              btn.click();
            }
          }, {
            element: element,
            innerText: innerText
          });
        case 1:
        case "end":
          return _context7.stop();
      }
    }, _callee7);
  }));
}
function getHref(page) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee8() {
    return _regeneratorRuntime__default["default"].wrap(function (_context8) {
      while (1) switch (_context8.prev = _context8.next) {
        case 0:
          return _context8.abrupt("return", page.evaluate(function () {
            return location.href;
          }));
        case 1:
        case "end":
          return _context8.stop();
      }
    }, _callee8);
  }));
}

var element = /*#__PURE__*/Object.freeze({
    __proto__: null,
    waitEle: waitEle,
    clickBtn: clickBtn,
    findAndClick: findAndClick,
    justWait: justWait,
    closeBlankPage: closeBlankPage,
    getRect: getRect,
    getInnerText: getInnerText,
    findListItemAndClick: findListItemAndClick,
    getHref: getHref
});

exports.clickBtn = clickBtn;
exports.closeBlankPage = closeBlankPage;
exports.element = element;
exports.findAndClick = findAndClick;
exports.findListItemAndClick = findListItemAndClick;
exports.getHref = getHref;
exports.getInnerText = getInnerText;
exports.getRect = getRect;
exports.justWait = justWait;
exports.waitEle = waitEle;
