'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Alert = undefined;

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

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _closeButton = require('kitten/components/buttons/close-button');

var _elementHelper = require('kitten/helpers/dom/element-helper');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Alert = exports.Alert = function (_Component) {
  _inherits(Alert, _Component);

  function Alert(props) {
    _classCallCheck(this, Alert);

    var _this = _possibleConstructorReturn(this, (Alert.__proto__ || Object.getPrototypeOf(Alert)).call(this, props));

    _this.state = {
      show: props.show,
      height: 'auto'
    };

    _this.handleCloseClick = _this.handleCloseClick.bind(_this);
    _this.handleAnimationEnd = _this.handleAnimationEnd.bind(_this);
    return _this;
  }

  _createClass(Alert, [{
    key: 'handleCloseClick',
    value: function handleCloseClick() {
      this.setState({
        show: false,

        // The css animation on the close button requires a fixed height.
        height: _elementHelper.domElementHelper.getComputedHeight(this.container)
      });
    }
  }, {
    key: 'handleAnimationEnd',
    value: function handleAnimationEnd() {
      this.props.onAfterClose();
    }
  }, {
    key: 'renderCloseButton',
    value: function renderCloseButton() {
      if (!this.props.closeButton) return;

      return _react2.default.createElement(_closeButton.CloseButton, {
        modifier: 'carbon',
        className: 'k-Alert__close',
        closeButtonLabel: this.props.closeButtonLabel,
        onClick: this.handleCloseClick
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      if (!this.props.show) return null;

      var _props = this.props,
          className = _props.className,
          show = _props.show,
          error = _props.error,
          success = _props.success,
          closeButton = _props.closeButton,
          closeButtonLabel = _props.closeButtonLabel,
          children = _props.children,
          onAfterClose = _props.onAfterClose,
          others = _objectWithoutProperties(_props, ['className', 'show', 'error', 'success', 'closeButton', 'closeButtonLabel', 'children', 'onAfterClose']);

      var alertClassName = (0, _classnames2.default)('k-Alert', {
        'k-Alert--success': success,
        'k-Alert--error': error,
        'k-Alert--hidden': !this.state.show
      }, className);

      return _react2.default.createElement(
        'div',
        _extends({
          ref: function ref(div) {
            return _this2.container = div;
          },
          role: 'alert',
          style: { height: this.state.height },
          className: alertClassName,
          onAnimationEnd: this.handleAnimationEnd
        }, others),
        _react2.default.createElement(
          'div',
          { className: 'k-Alert__container' },
          _react2.default.createElement(
            'div',
            { className: 'k-Alert__row' },
            _react2.default.createElement(
              'div',
              { className: 'k-Alert__content' },
              children
            )
          )
        ),
        this.renderCloseButton()
      );
    }
  }]);

  return Alert;
}(_react.Component);

Alert.defaultProps = {
  className: null,
  show: true,
  error: false,
  success: false,
  closeButton: false,
  closeButtonLabel: 'Close',
  onAfterClose: function onAfterClose() {}
};