'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.jumpToNewTab = undefined;

var _config = require('../../config');

var _config2 = _interopRequireDefault(_config);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _windowHandler = {}; /**
                          * Created by baoyinghai on 12/30/16.
                          */

window._windowHandler = _windowHandler;

var originalOnunload = window.onunload;

window.onunload = function () {
  window.___windowCallback && window.___windowCallback();
  originalOnunload && originalOnunload();
};

var pushWindowHandler = function pushWindowHandler(key, value) {
  _windowHandler[key] = value;
};

var getWindowHandler = function getWindowHandler(key) {
  return _windowHandler[key] || {};
};

var deleteWindowHandler = function deleteWindowHandler(key) {
  delete _windowHandler[key];
};

var jumpToNewTab = exports.jumpToNewTab = function jumpToNewTab(url, props) {
  // TODO: 同一个按钮 参数一样的  map 优化  刷新就会丢失
  var winKey = props.buttonCode + url;

  var _getWindowHandler = getWindowHandler(winKey),
      openerHandle = _getWindowHandler.openerHandle;

  if (openerHandle) {
    // 已经打开的页面 在这里关闭掉它
    openerHandle.___windowCallback = null;
    openerHandle.close();
  }
  var newOpenHandle = window.open(url);
  if (newOpenHandle) {
    newOpenHandle.___windowCallback = function () {
      deleteWindowHandler(winKey);
      if (props.needFreshPrePage === 0) return;
      // props.onRefresh(...p);
      if (props.refreshCallback) {
        props.refreshCallback();
      } else {
        location.reload(); // 舍弃
      }
    };
  }
  pushWindowHandler(winKey, { openerHandle: newOpenHandle });
};