"use strict";

exports.__esModule = true;
exports.default = exports.Notify = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _utils = require("../utils");

var _Notify = _interopRequireDefault(require("./Notify"));

var _getContainer = require("../utils/dom/getContainer");

var _lockClick = require("../toast/lock-click");

require("./style/index.css");

var _excluded = ["onClose", "duration"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var NotifyNamespace = {};

function parseOptions(message) {
  return (0, _utils.isObject)(message) ? message : {
    message: message
  };
}

var notifyArray = []; // 同步的销毁

function syncClear() {
  var fn = notifyArray.pop();

  while (fn) {
    fn();
    (0, _lockClick.lockClick)(false);
    fn = notifyArray.pop();
  }
} // 针对 toast 还没弹出来就立刻销毁的情况，将销毁放到下一个 event loop 中，避免销毁失败。


function nextTickClear() {
  setTimeout(syncClear);
} // 可返回用于销毁此弹窗的方法


var show = function show(p) {
  var props = parseOptions(p);

  var interProps = _extends({}, NotifyNamespace.currentOptions, props);

  var _interProps$onClose = interProps.onClose,
      onClose = _interProps$onClose === void 0 ? _utils.noop : _interProps$onClose,
      duration = interProps.duration,
      restProps = _objectWithoutPropertiesLoose(interProps, _excluded);

  var timer = 0;
  var userContainer = (0, _getContainer.resolveContainer)(props.teleport);
  var container = document.createElement('div');
  userContainer.appendChild(container);
  var destroy = _utils.noop;

  var TempNotify = function TempNotify() {
    var _useState = (0, _react.useState)(false),
        visible = _useState[0],
        setVisible = _useState[1];

    destroy = function destroy() {
      setVisible(false);
      if (onClose) onClose();
    }; // clearDOM after animation


    var internalOnClosed = function internalOnClosed() {
      var unmountResult = _reactDom.default.unmountComponentAtNode(container);

      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    };

    var internalAfterClose = (0, _react.useCallback)((0, _utils.once)(function () {
      internalOnClosed();
    }), [onClose, container]);
    (0, _react.useEffect)(function () {
      setVisible(true);
      syncClear();
      notifyArray.push(internalAfterClose);

      if (duration && +duration > 0) {
        timer = window.setTimeout(destroy, duration);
      }

      return function () {
        if (timer !== 0) {
          window.clearTimeout(timer);
        }
      };
    }, []);
    return /*#__PURE__*/_react.default.createElement(_Notify.default, _extends({}, restProps, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      onClose: onClose,
      onClosed: internalOnClosed
    }));
  };

  _reactDom.default.render( /*#__PURE__*/_react.default.createElement(TempNotify, null), container);

  return destroy;
};

function defaultOptions() {
  return {
    type: 'danger',
    color: undefined,
    message: '',
    onClose: undefined,
    onClick: undefined,
    duration: 3000,
    className: '',
    lockScroll: false,
    background: undefined
  };
}

NotifyNamespace.currentOptions = defaultOptions();

var setDefaultOptions = function setDefaultOptions(options) {
  (0, _utils.extend)(NotifyNamespace.currentOptions, options);
};

var resetDefaultOptions = function resetDefaultOptions() {
  NotifyNamespace.currentOptions = defaultOptions();
};

var clear = nextTickClear;
var exportNotifyNamespace = Object.assign(_Notify.default, {
  show: show,
  setDefaultOptions: setDefaultOptions,
  resetDefaultOptions: resetDefaultOptions,
  clear: clear
});
exports.Notify = exportNotifyNamespace;
var _default = exportNotifyNamespace;
exports.default = _default;