'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isNum = _interopDefault(require('./isNum'));
var getScreenPosition = _interopDefault(require('./getScreenPosition'));

/**
 * 打开一个相对屏幕居中的新窗口
 * @param {*} url
 * @param {*} options
 *
 * @example
 * openWindow('https://www.bilibili.com', '哔哩哔哩', 1200, 760);
 */
function openWindow(url, options) {
  options = options || {};
  var _options = options,
      left = _options.left,
      top = _options.top,
      width = _options.width,
      height = _options.height,
      windowName = _options.windowName;
  if (!windowName) windowName = 'window';
  if (!isNum(width)) width = 800;
  if (!isNum(height)) height = 520;
  var ua = navigator.userAgent;
  var sW = window.screen.width;
  var sH = window.screen.height;

  var _getScreenPosition = getScreenPosition(),
      screenLeft = _getScreenPosition.screenLeft,
      screenTop = _getScreenPosition.screenTop;

  var isIE = /compatible/i.test(ua) && /msie/i.test(ua);
  if (!isNum(left)) left = sW > width ? (sW - width) / 2 : 0;
  if (!isNum(top)) top = sH > height ? (sH - height) / 2 : 0;
  left += screenLeft;
  top += screenTop;

  if (isIE) {
    var props = "resizable=1,scrollbars=no,location=no,width=".concat(width, ",height=").concat(height, ",left=").concat(left, ",top=").concat(top);
    window.open(url, windowName, props);
  } else {
    var _props = "resizable=yes,dialog=yes,modal=yes,scrollbars=yes,width=".concat(width, ",height=").concat(height, ",left=").concat(left, ",top=").concat(top);

    var win = window.open(url, 'ZyiisPopup', _props);

    // eslint-disable-next-line no-eval
    eval('try { win.resizeTo(width, height); } catch(e) {}');
    win.focus();
  }
}

module.exports = openWindow;
