"use strict";

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

require("core-js/modules/es6.object.assign");

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

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

var _ = require("./..");

var _lodash = require("lodash");

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

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

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

var AlertContainer =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(AlertContainer, _React$Component);

  function AlertContainer() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      alertList: []
    };

    _this.info = function (title, message, onHide, otherProps) {
      _this.handleAlert(title, message, onHide, 'info', otherProps);
    };

    _this.success = function (title, message, onHide, otherProps) {
      _this.handleAlert(title, message, onHide, 'success', otherProps);
    };

    _this.warning = function (title, message, onHide, otherProps) {
      _this.handleAlert(title, message, onHide, 'warning', otherProps);
    };

    _this.error = function (title, message, onHide, otherProps) {
      _this.handleAlert(title, message, onHide, 'error', otherProps);
    };

    _this.handleAlert = function (title, message, onHide, type, otherProps) {
      var orderNewest = _this.props.orderNewest;
      var key = (0, _lodash.uniqueId)('alert_');
      var nextAlert = {
        key: key,
        title: title,
        message: message,
        onHide: (0, _lodash.bind)(_this.handleOnHide, _assertThisInitialized(_assertThisInitialized(_this)), key, onHide),
        type: type,
        show: true,
        closable: true,
        otherProps: otherProps
      };

      _this.setState(function (state) {
        return {
          alertList: orderNewest ? [nextAlert].concat(state.alertList) : state.alertList.concat([nextAlert])
        };
      });
    };

    _this.handleOnHide = function (key, onHide, e) {
      _this.setState(function (state) {
        onHide && onHide(e);
        return {
          alertList: (0, _lodash.reject)(state.alertList, {
            key: key
          })
        };
      });
    };

    return _this;
  }

  var _proto = AlertContainer.prototype;

  _proto.render = function render() {
    var position = this.props.position;
    return _react.default.createElement("div", {
      className: "cui-alert__container cui-alert__container--" + position,
      role: "alert"
    }, this.state.alertList.map(function (alert) {
      return _react.default.createElement(_.Alert, _extends({
        key: alert.key,
        title: alert.title,
        message: alert.message,
        type: alert.type,
        show: alert.show,
        closable: alert.closable,
        onHide: alert.onHide
      }, alert.otherProps));
    }));
  };

  return AlertContainer;
}(_react.default.Component);

AlertContainer.displayName = 'AlertContainer';
AlertContainer.defaultProps = {
  orderNewest: true,
  position: 'bottom-right'
};
AlertContainer.propTypes = {
  /** Display new alert messages at the top or bottom of the queue */
  orderNewest: _propTypes.default.bool,

  /** Position alert will display */
  position: _propTypes.default.oneOf(['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'])
};
var _default = AlertContainer;
exports.default = _default;