"use strict";

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

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

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

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

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

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

var _Toast = _interopRequireDefault(require("./Toast"));

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

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 _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 defaultOptions = {
  message: '',
  className: '',
  type: 'info',
  position: 'middle',
  forbidClick: false,
  duration: 2000,
  teleport: function teleport() {
    return document.body;
  }
};
var toastArray = [];
var allowMultiple = false;
var currentOptions = (0, _utils.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 = (0, _getContainer.resolveContainer)(teleport);
  bodyContainer.appendChild(container);

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

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

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


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

      var unmountResult = _reactDom.default.unmountComponentAtNode(container);

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

    var destroy = (0, _react.useCallback)(function () {
      setVisible(false);
      if (onClose) onClose();
    }, []);
    update.clear = internalOnClosed;
    update.config = (0, _react.useCallback)(function (nextState) {
      setState(function (prev) {
        return typeof nextState === 'function' ? _extends({}, prev, nextState(prev)) : _extends({}, prev, nextState);
      });
    }, [setState]);
    (0, _react.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.default.createElement(_Toast.default, _extends({}, state, {
      visible: visible,
      teleport: function teleport() {
        return container;
      },
      onClose: destroy,
      onClosed: internalOnClosed
    }));
  };

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

  return update;
};

function parseOptions(message) {
  if ((0, _utils.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 {
    (0, _utils.extend)(currentOptions, type);
  }
}

Toast.setDefaultOptions = setDefaultOptions;

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

var combineToast = Toast;
exports.Toast = combineToast;
var _default = combineToast;
exports.default = _default;