import { rpx2vw4style as __rpx2vw__ } from "babel-runtime-jsx-style-transform";

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { __rest } from "tslib";
import React, { createElement, forwardRef } from "react";
import classNames from 'classnames';
import { Text } from "@alifd/meet-react-component-one";
import Icon from '../icon';
import View from '../view';
import { isString, isFunction } from '../utils';
import { useValue } from '../utils/hooks';
var ICON_MAP = {
  success: 'success',
  warning: 'warning',
  error: 'error',
  notice: 'prompt',
  help: 'help',
  loading: 'loading'
};

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

  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,
      style = props.style,
      others = __rest(props, ["prefix", "className", "shape", "type", "visible", "defaultVisible", "title", "iconType", "closable", "onClose", "children", "style"]);

  var _useValue = useValue(props, false, {
    valueName: 'visible'
  }),
      _useValue2 = _slicedToArray(_useValue, 3),
      visible = _useValue2[0],
      setVisible = _useValue2[1],
      isControlled = _useValue2[2];

  if (!visible) {
    return null;
  }

  var clsPrefix = "".concat(prefix, "message");
  var toastContent = title || children;
  var isToast = shape === 'toast';
  var hideIcon = iconType === null || iconType === '' || isToast && !type;
  var isEmphatic = shape === 'inline' && title && !children;

  var handleClose = function handleClose() {
    if (!isControlled) {
      setVisible(false);
    }

    if (isFunction(onClose)) {
      onClose();
    }
  };

  return /*#__PURE__*/React.createElement(View, _extends({
    ref: ref,
    className: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(type), "".concat(clsPrefix, "--").concat(shape), className, (_classNames = {}, _defineProperty(_classNames, "".concat(clsPrefix, "--").concat(shape, "-").concat(type), shape && type), _defineProperty(_classNames, "".concat(clsPrefix, "--inline--no-title"), shape === 'inline' && !toastContent), _defineProperty(_classNames, "".concat(clsPrefix, "--toast-with-icon"), shape === 'toast' && !hideIcon), _classNames)),
    style: __rpx2vw__(style)
  }, others), !hideIcon && /*#__PURE__*/React.createElement(View, {
    className: "".concat(clsPrefix, "-icon-container--").concat(shape)
  }, /*#__PURE__*/React.createElement(Icon, {
    name: iconType || ICON_MAP[type] || type,
    className: classNames("".concat(clsPrefix, "-icon"), "".concat(clsPrefix, "-icon--").concat(type), "".concat(clsPrefix, "-icon--").concat(shape))
  })), /*#__PURE__*/React.createElement(View, {
    className: "".concat(clsPrefix, "-container ").concat(clsPrefix, "-container--").concat(shape)
  }, title && !isToast && /*#__PURE__*/React.createElement(View, {
    className: classNames("".concat(clsPrefix, "-title"))
  }, isString(title) ? /*#__PURE__*/React.createElement(Text, {
    className: classNames("".concat(clsPrefix, "-title"), _defineProperty({}, "".concat(clsPrefix, "-title--").concat(type), isEmphatic)),
    numberOfLines: 1
  }, title || children) : title), /*#__PURE__*/React.createElement(View, {
    className: classNames("".concat(clsPrefix, "-content-container"), "".concat(clsPrefix, "-content-container--").concat(shape))
  }, isToast && isString(toastContent) ? /*#__PURE__*/React.createElement(Text, {
    className: classNames("".concat(clsPrefix, "-content"), "".concat(clsPrefix, "-content--").concat(shape)),
    numberOfLines: 20
  }, toastContent) : null, isToast && !isString(toastContent) ? toastContent : null, !isToast && children ? /*#__PURE__*/React.createElement(View, {
    className: classNames("".concat(clsPrefix, "-content--inline"), _defineProperty({}, "".concat(clsPrefix, "-content--no-title"), shape === 'inline' && !title))
  }, children) : null)), closable && !isToast && /*#__PURE__*/React.createElement(Icon, {
    name: "close",
    size: "s",
    className: classNames("".concat(clsPrefix, "-close"), _defineProperty({}, "".concat(clsPrefix, "-close--").concat(type), isEmphatic)),
    onClick: handleClose
  }));
};

Message.displayName = 'Message';
export default /*#__PURE__*/forwardRef(Message);