"use strict";

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

var _tslib = require("tslib");

var _classnames = _interopRequireDefault(require("classnames"));

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

var _meetReactComponentOne = require("@alifd/meet-react-component-one");

var _icon = _interopRequireDefault(require("../icon"));

var _view = _interopRequireDefault(require("../view"));

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var ICON_MAP = {
  success: 'success',
  warning: 'warning',
  error: 'error',
  notice: 'prompt',
  help: 'help',
  loading: 'loading'
};

var Message = function (props, ref) {
  var _classNames, _classNames2;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      className = props.className,
      _props$shape = props.shape,
      shape = _props$shape === void 0 ? 'inline' : _props$shape,
      _props$type = props.type,
      type = _props$type === void 0 ? 'success' : _props$type,
      visibleProp = props.visible,
      _props$defaultVisible = props.defaultVisible,
      defaultVisible = _props$defaultVisible === void 0 ? false : _props$defaultVisible,
      title = props.title,
      iconType = props.iconType,
      _props$closable = props.closable,
      closable = _props$closable === void 0 ? false : _props$closable,
      onClose = props.onClose,
      children = props.children,
      other = (0, _tslib.__rest)(props, ["prefix", "className", "shape", "type", "visible", "defaultVisible", "title", "iconType", "closable", "onClose", "children"]);

  var _useState = (0, _react.useState)('visible' in props ? !!visibleProp : defaultVisible),
      visible = _useState[0],
      setVisible = _useState[1];

  (0, _react.useEffect)(function () {
    if ('visible' in props) {
      setVisible(!!visibleProp);
    }
  }, [visibleProp]);

  if (!visible) {
    return null;
  }

  var clsPrefix = prefix + "message";
  var toastContent = title || children;
  var isToast = shape === 'toast';
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref,
    className: (0, _classnames.default)(clsPrefix, clsPrefix + "--" + type, clsPrefix + "--" + shape, clsPrefix + "--" + shape + "-" + type, className, (_classNames = {}, _classNames[clsPrefix + "--inline--no-title"] = shape === 'inline' && !toastContent, _classNames))
  }, other), !(iconType === '' || isToast && !type) && /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-icon-container--" + shape
  }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    name: iconType || ICON_MAP[type] || type,
    className: (0, _classnames.default)(clsPrefix + "-icon", clsPrefix + "-icon--" + type, clsPrefix + "-icon--" + shape)
  })), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-container " + clsPrefix + "-container--" + shape
  }, title && !isToast && /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-title")
  }, (0, _utils.isString)(title) ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: (0, _classnames.default)(clsPrefix + "-title", (_classNames2 = {}, _classNames2[clsPrefix + "-title--" + type] = !children || _react.Children.count(children) === 0, _classNames2)),
    numberOfLines: 1
  }, title) : title), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-content-container", clsPrefix + "-content-container--" + shape)
  }, isToast ? (0, _utils.isString)(toastContent) ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: (0, _classnames.default)(clsPrefix + "-content", clsPrefix + "-content--" + shape),
    numberOfLines: 20
  }, toastContent) : toastContent : children)), closable && !isToast && /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    name: "close",
    size: "s",
    className: clsPrefix + "-close",
    onClick: onClose
  }));
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(Message);

exports.default = _default;