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

import React, { useCallback, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { extend, isObject } from '../utils';
import { resolveContainer } from '../utils/dom/getContainer';
import { lockClick } from './lock-click';
import BaseToast from './Toast';
import "./style/index.css";
var defaultOptions = {
  message: '',
  className: '',
  type: 'info',
  position: 'middle',
  forbidClick: false,
  duration: 2000,
  teleport: function teleport() {
    return document.body;
  }
};
var toastArray = [];
var allowMultiple = false;
var currentOptions = extend({}, defaultOptions); // default options of specific type

var defaultOptionsMap = new Map(); // 同步的销毁

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

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


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


var Toast = function Toast(p) {
  var props = parseOptions(p);
  var update = {
    config: function config() {},
    clear: function clear() {
      return null;
    }
  };
  var timer = 0;
  var onClose = props.onClose,
      teleport = props.teleport;
  var container = document.createElement('div');
  var bodyContainer = resolveContainer(teleport);
  bodyContainer.appendChild(container);

  var TempToast = function TempToast() {
    var options = _extends({
      duration: 2000
    }, props);

    var _useState = useState(false),
        visible = _useState[0],
        setVisible = _useState[1];

    var _useState2 = useState(_extends({}, options)),
        state = _useState2[0],
        setState = _useState2[1]; // clearDOM after animation


    var internalOnClosed = useCallback(function () {
      if (state.forbidClick) {
        lockClick(false);
      }

      var unmountResult = ReactDOM.unmountComponentAtNode(container);

      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    }, [container]); // close with animation

    var destroy = useCallback(function () {
      setVisible(false);
      if (onClose) onClose();
    }, []);
    update.clear = internalOnClosed;
    update.config = useCallback(function (nextState) {
      setState(function (prev) {
        return typeof nextState === 'function' ? _extends({}, prev, nextState(prev)) : _extends({}, prev, nextState);
      });
    }, [setState]);
    useEffect(function () {
      setVisible(true);
      if (!allowMultiple) syncClear();
      toastArray.push(internalOnClosed);

      if (state.duration !== 0 && 'duration' in state) {
        timer = window.setTimeout(destroy, +state.duration);
      }

      return function () {
        if (timer !== 0) {
          window.clearTimeout(timer);
        }
      };
    }, []);
    return /*#__PURE__*/React.createElement(BaseToast, _extends({}, state, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      onClose: destroy,
      onClosed: internalOnClosed
    }));
  };

  ReactDOM.render( /*#__PURE__*/React.createElement(TempToast, null), container);
  return update;
};

function parseOptions(message) {
  if (isObject(message)) {
    return message;
  }

  return {
    message: message
  };
}

var createMethod = function createMethod(type) {
  return function (options) {
    return Toast(_extends({}, currentOptions, defaultOptionsMap.get(type), parseOptions(options), {
      type: type
    }));
  };
};

['info', 'loading', 'success', 'fail'].forEach(function (method) {
  Toast[method] = createMethod(method);
});

Toast.allowMultiple = function (value) {
  if (value === void 0) {
    value = true;
  }

  allowMultiple = value;
};

Toast.clear = nextTickClear;

function setDefaultOptions(type, options) {
  if (typeof type === 'string') {
    defaultOptionsMap.set(type, options);
  } else {
    extend(currentOptions, type);
  }
}

Toast.setDefaultOptions = setDefaultOptions;

Toast.resetDefaultOptions = function (type) {
  if (typeof type === 'string') {
    defaultOptionsMap.delete(type);
  } else {
    currentOptions = extend({}, defaultOptions);
    defaultOptionsMap.clear();
  }
};

var combineToast = Toast;
export { combineToast as Toast };
export default combineToast;