import React, { useContext, useEffect } from "react";
import classnames from "classnames";
import Icon from "../icon";
import Popup from "../popup";
import Loading from "../loading";
import { lockClick } from "./lock-click";
import { isDef } from "../utils";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const Toast = (props) => {
  let clickable = false;
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("toast", prefixCls);
  const toggleClickable = () => {
    const newValue = props.visible && props.forbidClick;
    if (clickable !== newValue) {
      clickable = newValue;
      lockClick(clickable);
    }
    if (!props.visible) {
      lockClick(false);
    }
  };
  const handleClick = () => {
    var _a;
    if (props.closeOnClick) {
      (_a = props.onClose) == null ? void 0 : _a.call(props);
    }
  };
  useEffect(() => {
    toggleClickable();
  }, [props.visible, props.forbidClick]);
  const renderIcon = () => {
    const {
      icon,
      type,
      iconPrefix,
      iconSize,
      loadingType
    } = props;
    const hasIcon = icon || type === "success" || type === "fail";
    if (hasIcon) {
      return typeof icon === "string" ? /* @__PURE__ */ React.createElement(Icon, {
        name: icon || (type === "fail" ? "cross" : type),
        size: iconSize,
        className: classnames(bem("icon")),
        classPrefix: iconPrefix
      }) : icon;
    }
    if (type === "loading") {
      return /* @__PURE__ */ React.createElement(Loading, {
        className: classnames(bem("loading")),
        type: loadingType
      });
    }
    return null;
  };
  const renderMessage = () => {
    const {
      message
    } = props;
    if (isDef(message) && message !== "") {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("info"))
      }, message);
    }
    return null;
  };
  return /* @__PURE__ */ React.createElement(Popup, {
    className: classnames([bem([props.position, {
      [props.type]: !props.icon
    }]), props.className]),
    visible: props.visible,
    overlay: props.overlay,
    transition: props.transition,
    overlayClass: props.overlayClass,
    overlayStyle: props.overlayStyle,
    closeOnClickOverlay: props.closeOnClickOverlay,
    lockScroll: false,
    onClick: handleClick,
    onClose: props.onClose,
    onClosed: props.onClosed,
    onOpened: props.onOpened,
    teleport: props.teleport
  }, renderIcon(), renderMessage());
};
Toast.defaultProps = {
  type: "info",
  duration: 2e3,
  position: "middle",
  transition: "rc-fade",
  loadingType: "circular",
  overlay: false
};
var stdin_default = Toast;
export {
  stdin_default as default
};
