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 classNames from 'classnames';
import React, { createElement, forwardRef, useEffect, useState, Children } from "react";
import { Text } from "@alifd/meet-react-component-one";
import Icon from '../icon';
import View from '../view';
import { isString } from '../utils';
var ICON_MAP = {
  success: 'success',
  warning: 'warning',
  error: 'error',
  notice: 'prompt',
  help: 'help',
  loading: 'loading'
};

var Message = function Message(props, ref) {
  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 = __rest(props, ["prefix", "className", "shape", "type", "visible", "defaultVisible", "title", "iconType", "closable", "onClose", "children"]);

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

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

  if (!visible) {
    return null;
  }

  var clsPrefix = "".concat(prefix, "message");
  var toastContent = title || children;
  var isToast = shape === 'toast';
  var hideIcon = iconType === '' || isToast && !type;
  return /*#__PURE__*/React.createElement(View, _extends({
    ref: ref,
    className: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(type), "".concat(clsPrefix, "--").concat(shape), "".concat(clsPrefix, "--").concat(shape, "-").concat(type), className, _defineProperty({}, "".concat(clsPrefix, "--inline--no-title"), shape === 'inline' && !toastContent))
  }, other), !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), !children || Children.count(children) === 0)),
    numberOfLines: 1
  }, title) : 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) : toastContent : children)), closable && !isToast && /*#__PURE__*/React.createElement(Icon, {
    name: "close",
    size: "s",
    className: "".concat(clsPrefix, "-close"),
    onClick: onClose
  }));
};

export default /*#__PURE__*/forwardRef(Message);