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

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

/* eslint-disable @typescript-eslint/naming-convention */
import React, { useCallback, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { extend, isObject, noop, once } from '../utils';
import Notify from './Notify';
import { resolveContainer } from '../utils/dom/getContainer';
import { lockClick } from '../toast/lock-click';
import "./style/index.css";
var NotifyNamespace = {};

function parseOptions(message) {
  return isObject(message) ? message : {
    message: message
  };
}

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

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

  while (fn) {
    fn();
    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 ? noop : _interProps$onClose,
      duration = interProps.duration,
      restProps = _objectWithoutPropertiesLoose(interProps, _excluded);

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

  var TempNotify = function TempNotify() {
    var _useState = 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.unmountComponentAtNode(container);

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

    var internalAfterClose = useCallback(once(function () {
      internalOnClosed();
    }), [onClose, container]);
    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.createElement(Notify, _extends({}, restProps, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      onClose: onClose,
      onClosed: internalOnClosed
    }));
  };

  ReactDOM.render( /*#__PURE__*/React.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) {
  extend(NotifyNamespace.currentOptions, options);
};

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

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