"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.config = exports.clear = exports.show = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));

var _isString = _interopRequireDefault(require("lodash/isString"));

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

var _dom = require("../utils/dom");

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

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) { (0, _defineProperty2.default)(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; }

var currentHandler = null;
var currentTimer = null;
var defaultProps = {
  duration: 2000,
  mask: true,
  maskClickable: true
};

var show = function show(props) {
  var rest = _objectSpread(_objectSpread({}, defaultProps), (0, _isString.default)(props) ? {
    content: props
  } : props);

  var element = /*#__PURE__*/React.createElement(_Toast.default, (0, _extends2.default)({}, rest, {
    onClose: function onClose() {
      currentHandler = null;
    }
  }));

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

  currentTimer && window.clearTimeout(currentTimer);

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

  return currentHandler;
};

exports.show = show;

var clear = function clear() {
  var _currentHandler;

  (_currentHandler = currentHandler) === null || _currentHandler === void 0 ? void 0 : _currentHandler.close();
  currentHandler = null;
};

exports.clear = clear;
var ALLOW_KEYS = ['duration', 'mask', 'maskClassName', 'maskStyle', 'maskColor', 'maskOpacity', 'maskClickable', 'mountContainer'];

var config = function config(props) {
  if (!(0, _isPlainObject.default)(props)) return;
  Object.entries(props).forEach(function (_ref) {
    var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
        key = _ref2[0],
        value = _ref2[1];

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

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

exports.config = config;