'use strict';

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('./tslib.es6-0d92ef81.js');
var nodejs_os = require('./nodejs/os.js');

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

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

// eslint-disable-next-line @typescript-eslint/naming-convention
exports.DEVICE_TYPE = void 0;
(function (DEVICE_TYPE) {
  DEVICE_TYPE["PC"] = "PC";
  DEVICE_TYPE["MOBILE_HOR"] = "MOBILE_HORPC";
  DEVICE_TYPE["MOBILE_VERT"] = "MOBILE_VERT";
})(exports.DEVICE_TYPE || (exports.DEVICE_TYPE = {}));
function initBrowser(_ref) {
  var puppeteer = _ref.puppeteer,
    _ref$args = _ref.args,
    args = _ref$args === void 0 ? [] : _ref$args,
    _ref$headless = _ref.headless,
    headless = _ref$headless === void 0 ? true : _ref$headless,
    _ref$devtools = _ref.devtools,
    devtools = _ref$devtools === void 0 ? true : _ref$devtools;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var defaultArgs, type, isLinux, launchArgs, browser;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          defaultArgs = ['--disable-setuid-sandbox'];
          type = nodejs_os.getOs().type();
          isLinux = type == 'Linux';
          if (isLinux) {
            defaultArgs.push('--no-sandbox');
          }
          launchArgs = {
            headless: headless,
            devtools: devtools,
            args: [].concat(defaultArgs, _toConsumableArray__default["default"](args))
          };
          _context.next = 1;
          return puppeteer.launch(launchArgs);
        case 1:
          browser = _context.sent;
          return _context.abrupt("return", browser);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function getNewPage(browser, device) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var page;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return browser.newPage();
        case 1:
          page = _context2.sent;
          if (!(device === exports.DEVICE_TYPE.MOBILE_VERT)) {
            _context2.next = 3;
            break;
          }
          _context2.next = 2;
          return page.setViewport({
            width: 414,
            height: 736,
            isMobile: true
          });
        case 2:
          _context2.next = 4;
          break;
        case 3:
          if (!(device === exports.DEVICE_TYPE.MOBILE_HOR)) {
            _context2.next = 4;
            break;
          }
          _context2.next = 4;
          return page.setViewport({
            width: 736,
            height: 414,
            isMobile: true
          });
        case 4:
          return _context2.abrupt("return", page);
        case 5:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
var openedPageList = new Set();
function openOrFindPage(browser, href, device) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var page, target;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          if (!openedPageList.has(href)) {
            _context3.next = 5;
            break;
          }
          _context3.prev = 1;
          _context3.next = 2;
          return browser.waitForTarget(function (t) {
            return t.url().includes(href);
          }, {
            timeout: 2000
          });
        case 2:
          target = _context3.sent;
          _context3.next = 3;
          return target.page();
        case 3:
          page = _context3.sent;
          return _context3.abrupt("return", page);
        case 4:
          _context3.prev = 4;
          _context3["catch"](1);
        case 5:
          _context3.next = 6;
          return getNewPage(browser, device);
        case 6:
          page = _context3.sent;
          _context3.next = 7;
          return page["goto"](href);
        case 7:
          openedPageList.add(href);
          return _context3.abrupt("return", page);
        case 8:
        case "end":
          return _context3.stop();
      }
    }, _callee3, null, [[1, 4]]);
  }));
}
function setUserAgent(useragent, page) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
    return _regeneratorRuntime__default["default"].wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          _context4.next = 1;
          return page.setUserAgent(useragent);
        case 1:
          _context4.next = 2;
          return page.evaluate(function () {
            return location.reload();
          });
        case 2:
          _context4.next = 3;
          return page.waitForNavigation();
        case 3:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  }));
}
function setSessionStorage(key, value, 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:
          _context5.next = 1;
          return page.evaluate(function (_ref2) {
            var key = _ref2.key,
              value = _ref2.value;
            sessionStorage.setItem(key, value);
          }, {
            key: key,
            value: value
          });
        case 1:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  }));
}
function setRoute(page) {
  var route = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '/';
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee6() {
    return _regeneratorRuntime__default["default"].wrap(function (_context6) {
      while (1) switch (_context6.prev = _context6.next) {
        case 0:
          if (page) {
            _context6.next = 1;
            break;
          }
          return _context6.abrupt("return");
        case 1:
          _context6.next = 2;
          return page.evaluate(function (route) {
            return window.app.$router.push(route);
          }, route);
        case 2:
        case "end":
          return _context6.stop();
      }
    }, _callee6);
  }));
}

var page = /*#__PURE__*/Object.freeze({
    __proto__: null,
    get DEVICE_TYPE () { return exports.DEVICE_TYPE; },
    initBrowser: initBrowser,
    getNewPage: getNewPage,
    openOrFindPage: openOrFindPage,
    setUserAgent: setUserAgent,
    setSessionStorage: setSessionStorage,
    setRoute: setRoute
});

exports.getNewPage = getNewPage;
exports.initBrowser = initBrowser;
exports.openOrFindPage = openOrFindPage;
exports.page = page;
exports.setRoute = setRoute;
exports.setSessionStorage = setSessionStorage;
exports.setUserAgent = setUserAgent;
