/* eslint-disable react/default-props-match-prop-types */
import React, { useContext, useEffect } from 'react';
import clsx from 'clsx';
import { Cross, Success } from '@react-vant/icons';
import Popup from '../popup';
import Loading from '../loading';
import { lockClick } from './lock-click';
import { isDef } from '../utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext'; // eslint-disable-next-line react/require-default-props

var Toast = function Toast(props) {
  var _ref;

  var clickable = false;

  var _useContext = useContext(ConfigProviderContext),
      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;
      lockClick(clickable);
    }

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

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

  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.createElement(Cross, null) : /*#__PURE__*/React.createElement(Success, null);
      return /*#__PURE__*/React.cloneElement(icon || buildInIcon, {
        className: clsx(bem('icon')),
        fontSize: iconSize
      });
    }

    if (type === 'loading') {
      return /*#__PURE__*/React.createElement(Loading, {
        className: clsx(bem('loading')),
        type: loadingType
      });
    }

    return null;
  };

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

    if (isDef(message) && message !== '') {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('info'))
      }, message);
    }

    return null;
  };

  return /*#__PURE__*/React.createElement(Popup, {
    className: clsx([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
};
export default Toast;