import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import isPlainObject from 'lodash/isPlainObject';
import isString from 'lodash/isString';
import * as React from 'react';
import { renderImperatively } from '../utils/dom';
import Toast from './Toast';
var currentHandler = null;
var currentTimer = null;
var defaultProps = {
  duration: 2000,
  mask: true,
  maskClickable: true
};
export var show = function show(props) {
  var rest = _objectSpread(_objectSpread({}, defaultProps), isString(props) ? {
    content: props
  } : props);

  var element = /*#__PURE__*/React.createElement(Toast, _extends({}, rest, {
    onClose: function onClose() {
      currentHandler = null;
    }
  }));

  if (currentHandler) {
    currentHandler.replace(element);
  } else {
    currentHandler = renderImperatively(element);
  }

  currentTimer && window.clearTimeout(currentTimer);

  if (rest.duration !== 0) {
    currentTimer = window.setTimeout(function () {
      clear();
    }, rest.duration);
  }

  return currentHandler;
};
export var clear = function clear() {
  var _currentHandler;

  (_currentHandler = currentHandler) === null || _currentHandler === void 0 ? void 0 : _currentHandler.close();
  currentHandler = null;
};
var ALLOW_KEYS = ['duration', 'mask', 'maskClassName', 'maskStyle', 'maskColor', 'maskOpacity', 'maskClickable', 'mountContainer'];
export var config = function config(props) {
  if (!isPlainObject(props)) return;
  Object.entries(props).forEach(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        key = _ref2[0],
        value = _ref2[1];

    if (ALLOW_KEYS.indexOf(key) === -1) return;

    if (value !== undefined) {
      defaultProps[key] = value;
    }
  });
};