"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _view = _interopRequireDefault(require("../view"));
var _icon = _interopRequireDefault(require("../icon"));
var _utils = require("../utils");
var _hooks = require("../utils/hooks");
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; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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, _classNames3, _classNames4;
  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,
    type = 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,
    zIndex = props.zIndex,
    style = props.style,
    others = (0, _tslib.__rest)(props, ["prefix", "className", "shape", "type", "visible", "defaultVisible", "title", "iconType", "closable", "onClose", "children", "zIndex", "style"]);
  var newStyle = Object.assign({
    zIndex: zIndex
  }, style);
  var _useValue = (0, _hooks.useValue)(props, false, {
      valueName: 'visible'
    }),
    visible = _useValue[0],
    setVisible = _useValue[1],
    isControlled = _useValue[2];
  if (!visible) {
    return null;
  }
  var clsPrefix = prefix + "message";
  var toastContent = title || children;
  var isToast = shape === 'toast';
  var hideIcon = iconType === null || iconType === '' || isToast && !type;
  var isSimpleMode = shape === 'inline' && !title && children;
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref,
    className: (0, _classnames.default)(clsPrefix, clsPrefix + "--" + type, clsPrefix + "--" + shape, className, (_classNames = {}, _classNames[clsPrefix + "--" + shape + "-" + type] = shape && type, _classNames[clsPrefix + "--inline--no-title"] = shape === 'inline' && !toastContent, _classNames[clsPrefix + "--toast-with-icon"] = shape === 'toast' && !hideIcon, _classNames)),
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(newStyle)
  }, others), !hideIcon && /*#__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] = isSimpleMode, _classNames2)),
    numberOfLines: 1
  }, title || children) : 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) : null, isToast && !(0, _utils.isString)(toastContent) ? toastContent : null, !isToast && children ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-content--inline", (_classNames3 = {}, _classNames3[clsPrefix + "-content--no-title"] = shape === 'inline' && !title, _classNames3[clsPrefix + "-content--inline-" + type] = isSimpleMode, _classNames3[clsPrefix + "-content--inline-simple"] = isSimpleMode, _classNames3))
  }, children) : null)), closable && !isToast && /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    name: "close",
    size: "s",
    className: (0, _classnames.default)(clsPrefix + "-close", (_classNames4 = {}, _classNames4[clsPrefix + "-close--" + type] = isSimpleMode, _classNames4)),
    onClick: function handleClose() {
      if (!isControlled) {
        setVisible(false);
      }
      if ((0, _utils.isFunction)(onClose)) {
        onClose();
      }
    }
  }));
};
Message.displayName = 'Message';
var _default = /*#__PURE__*/(0, _react.forwardRef)(Message);
exports.default = _default;