"use strict";

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

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

var _clsx = _interopRequireDefault(require("clsx"));

var _icons = require("@react-vant/icons");

var _popup = _interopRequireDefault(require("../popup"));

var _loading = _interopRequireDefault(require("../loading"));

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

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

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

/* eslint-disable react/default-props-match-prop-types */
// eslint-disable-next-line react/require-default-props
var Toast = function Toast(props) {
  var _ref;

  var clickable = false;

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('toast', prefixCls),
      bem = _createNamespace[0];

  var toggleClickable = function toggleClickable() {
    var newValue = props.visible && props.forbidClick;

    if (clickable !== newValue) {
      clickable = newValue;
      (0, _lockClick.lockClick)(clickable);
    }

    if (!props.visible) {
      (0, _lockClick.lockClick)(false);
    }
  };

  var onClick = function onClick() {
    if (props.closeOnClick) {
      props.onClose();
    }
  };

  (0, _react.useEffect)(function () {
    toggleClickable();
  }, [props.visible, props.forbidClick]);

  var renderIcon = function renderIcon() {
    var icon = props.icon,
        type = props.type,
        iconSize = props.iconSize,
        loadingType = props.loadingType;
    var hasIcon = icon || type === 'success' || type === 'fail';

    if (hasIcon) {
      var buildInIcon = type === 'fail' ? /*#__PURE__*/_react.default.createElement(_icons.Cross, null) : /*#__PURE__*/_react.default.createElement(_icons.Success, null);
      return /*#__PURE__*/_react.default.cloneElement(icon || buildInIcon, {
        className: (0, _clsx.default)(bem('icon')),
        fontSize: iconSize
      });
    }

    if (type === 'loading') {
      return /*#__PURE__*/_react.default.createElement(_loading.default, {
        className: (0, _clsx.default)(bem('loading')),
        type: loadingType
      });
    }

    return null;
  };

  var renderMessage = function renderMessage() {
    var message = props.message;

    if ((0, _utils.isDef)(message) && message !== '') {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('info'))
      }, message);
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement(_popup.default, {
    className: (0, _clsx.default)([bem([props.position, (_ref = {}, _ref[props.type] = !props.icon, _ref)]), props.className]),
    visible: props.visible,
    overlay: props.overlay,
    transition: props.transition,
    overlayClass: props.overlayClass,
    overlayStyle: props.overlayStyle,
    closeOnClickOverlay: props.closeOnClickOverlay,
    lockScroll: false,
    onClick: onClick,
    onClose: props.onClose,
    onClosed: props.onClosed,
    onOpened: props.onOpened,
    teleport: props.teleport
  }, renderIcon(), renderMessage());
};

Toast.defaultProps = {
  type: 'info',
  duration: 2000,
  position: 'middle',
  transition: 'rv-fade',
  loadingType: 'circular',
  overlay: false
};
var _default = Toast;
exports.default = _default;