"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _toast = require("./toast.schema");

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

var _button = _interopRequireDefault(require("../button"));

var _util = require("../util");

var _configProvider = _interopRequireDefault(require("../config-provider"));

var _transition = require("./base/transition");

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

var ToastItem =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ToastItem, _React$Component);

  function ToastItem() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ToastItem);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ToastItem)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "handleClose", function () {
      if (_util.func.isFunction(_this.props.onClose)) {
        _this.props.onClose();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handlePause", function () {
      if (_util.func.isFunction(_this.props.onPause)) {
        _this.props.onPause();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleStart", function () {
      if (_util.func.isFunction(_this.props.onStart)) {
        _this.props.onStart();
      }
    });

    return _this;
  }

  _createClass(ToastItem, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.props.onRemove();
    }
  }, {
    key: "getSuffix",
    value: function getSuffix() {
      switch (this.props.status) {
        case _toast.ToastStatus.Error:
          return 'error';

        case _toast.ToastStatus.Success:
          return 'success';

        case _toast.ToastStatus.Info:
          return 'info';

        case _toast.ToastStatus.Warning:
          return 'warning';

        default:
          return 'info';
      }
    }
  }, {
    key: "getStatusClassName",
    value: function getStatusClassName(classNamePrefix) {
      var suffix = this.getSuffix();
      return (0, _classnames["default"])(classNamePrefix, suffix ? "".concat(classNamePrefix, "-is-").concat(suffix) : null);
    }
  }, {
    key: "getStatusIconName",
    value: function getStatusIconName() {
      switch (this.props.status) {
        case _toast.ToastStatus.Success:
          return 'tick-o-fill';

        case _toast.ToastStatus.Warning:
          return 'warning-o-fill';

        case _toast.ToastStatus.Error:
          return 'remove-o-fill';

        default:
          return 'info-o-fill';
      }
    }
  }, {
    key: "renderContent",
    value: function renderContent(toastPrefix) {
      var cls = "".concat(toastPrefix, "-content");
      return _react["default"].createElement("div", {
        className: cls
      }, this.renderTitle(toastPrefix));
    }
  }, {
    key: "renderTitle",
    value: function renderTitle(toastPrefix) {
      var titleCls = "".concat(toastPrefix, "-title");
      var textCls = "".concat(toastPrefix, "-title-text");
      return _react["default"].createElement("div", {
        className: titleCls
      }, this.renderIcon(toastPrefix), _react["default"].createElement("div", {
        className: textCls
      }, this.renderMessage(toastPrefix), this.renderHandlers(toastPrefix), this.renderDescription(toastPrefix)), this.renderCloseHandler(toastPrefix));
    }
  }, {
    key: "renderHandlers",
    value: function renderHandlers(toastPrefix) {
      var _this$props = this.props,
          handlers = _this$props.handlers,
          onClickHandlers = _this$props.onClickHandlers;

      if (!handlers) {
        return null;
      }

      var handlerCls = "".concat(toastPrefix, "-handlers");

      if (typeof handlers !== 'string') {
        return _react["default"].createElement("span", {
          className: handlerCls
        }, handlers);
      }

      return _react["default"].createElement(_button["default"], {
        className: handlerCls,
        type: "primary",
        text: true,
        onClick: onClickHandlers
      }, handlers);
    }
  }, {
    key: "renderCloseHandler",
    value: function renderCloseHandler(toastPrefix) {
      var closeable = this.props.closeable;

      if (!closeable) {
        return null;
      }

      var iconCls = "".concat(toastPrefix, "-close-handler");
      return _react["default"].createElement(_icon["default"], {
        className: iconCls,
        type: "remove",
        onClick: this.handleClose,
        size: "small"
      });
    }
  }, {
    key: "renderStatusBar",
    value: function renderStatusBar(toastPrefix) {
      var cls = "".concat(toastPrefix, "-solid-bar");
      return _react["default"].createElement("hr", {
        className: this.getStatusClassName(cls)
      });
    }
  }, {
    key: "renderIcon",
    value: function renderIcon(toastPrefix) {
      if (this.props.icon !== undefined) {
        return this.props.icon;
      }

      var iconName = this.getStatusIconName();

      if (typeof iconName !== 'string') {
        return null;
      }

      var iconCls = "".concat(toastPrefix, "-symbol");
      return _react["default"].createElement(_icon["default"], {
        className: this.getStatusClassName(iconCls),
        type: iconName
      });
    }
  }, {
    key: "renderMessage",
    value: function renderMessage(toastPrefix) {
      var title = this.props.title;

      if ((typeof title !== 'string' || title.length === 0) && _typeof(title) !== 'object') {
        return null;
      }

      var msgCls = "".concat(toastPrefix, "-message");
      return _react["default"].createElement("h4", {
        className: msgCls
      }, title);
    }
  }, {
    key: "renderDescription",
    value: function renderDescription(toastPrefix) {
      var content = this.props.content;

      if ((typeof content !== 'string' || content.length === 0) && _typeof(content) !== 'object') {
        return null;
      }

      var descCls = "".concat(toastPrefix, "-description");
      return _react["default"].createElement("p", {
        className: descCls
      }, content);
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props2 = this.props,
          prefix = _this$props2.prefix,
          className = _this$props2.className,
          style = _this$props2.style;
      var toastPrefix = "".concat(prefix, "toast");
      var classes = (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(toastPrefix, "-component"), true), _defineProperty(_cx, toastPrefix, true), _defineProperty(_cx, className, !!className), _cx));
      return _react["default"].createElement("div", {
        className: classes,
        onMouseEnter: this.handlePause,
        onMouseLeave: this.handleStart,
        style: style
      }, this.renderStatusBar(toastPrefix), this.renderContent(toastPrefix));
    }
  }]);

  return ToastItem;
}(_react["default"].Component);

_defineProperty(ToastItem, "propTypes", {
  prefix: _propTypes["default"].string,
  className: _propTypes["default"].string,
  style: _propTypes["default"].object,
  title: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].node]),
  content: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].node]),
  handlers: _propTypes["default"].array,
  status: _propTypes["default"].any,
  icon: _propTypes["default"].string,
  closeable: _propTypes["default"].bool,
  onClose: _propTypes["default"].func,
  onPause: _propTypes["default"].func,
  onStart: _propTypes["default"].func,
  onRemove: _propTypes["default"].func,
  onClickHandlers: _propTypes["default"].func
});

_defineProperty(ToastItem, "defaultProps", {
  prefix: 'next-'
});

var CSSMotion = {
  timeout: 320,
  enter: (0, _transition.motion)('.32s cubic-bezier(0, 0.63, 0.26, 1.12)', {
    height: '0',
    opacity: '0',
    transform: 'scale(0.3) translateY(60px)'
  }, {
    height: '*',
    opacity: '1',
    transform: 'scale(1) translateY(0px)'
  }),
  exit: (0, _transition.motion)('.32s cubic-bezier(0.8, 0, 0.4, 1)', {
    opacity: '1',
    height: '*',
    transform: 'scale(1)'
  }, {
    opacity: '0',
    height: '0',
    transform: 'scale(0.5)'
  })
};

var _default = _configProvider["default"].config((0, _transition.Transition)(CSSMotion, true)(ToastItem));

exports["default"] = _default;