'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _ModalActionContent = require('./styles/ModalActionContent');

var _ModalActionContent2 = _interopRequireDefault(_ModalActionContent);

var _ModalBlocker = require('./styles/ModalBlocker');

var _ModalBlocker2 = _interopRequireDefault(_ModalBlocker);

var _ModalContent = require('./styles/ModalContent');

var _ModalContent2 = _interopRequireDefault(_ModalContent);

var _ModalTitle = require('./styles/ModalTitle');

var _ModalTitle2 = _interopRequireDefault(_ModalTitle);

var _ModalWindow = require('./styles/ModalWindow');

var _ModalWindow2 = _interopRequireDefault(_ModalWindow);

var _ModalCloseButton = require('./styles/ModalCloseButton');

var _ModalCloseButton2 = _interopRequireDefault(_ModalCloseButton);

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

var Modal = function (_React$Component) {
  (0, _inherits3.default)(Modal, _React$Component);

  function Modal() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Modal);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Modal.__proto__ || (0, _getPrototypeOf2.default)(Modal)).call.apply(_ref, [this].concat(args))), _this), _this.handleModalClicked = function (event) {
      // prevents click event bubbling to blocker and triggering blockerclicked callback
      event.stopPropagation();
    }, _this.renderTitle = function () {
      var _this$props = _this.props,
          title = _this$props.title,
          Title = _this$props.Title;

      if (!title) return null;
      return _react2.default.createElement(
        Title,
        null,
        title
      );
    }, _this.renderCloseButton = function () {
      var _this$props2 = _this.props,
          CloseButton = _this$props2.CloseButton,
          onClose = _this$props2.onClose;

      if (!onClose) return null;
      return _react2.default.createElement(CloseButton, { onClose: onClose });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Modal, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          id = _props.id,
          className = _props.className,
          onClose = _props.onClose,
          onBlockerClicked = _props.onBlockerClicked,
          children = _props.children,
          actionContent = _props.actionContent,
          Blocker = _props.Blocker,
          Content = _props.Content,
          Window = _props.Window,
          ActionContent = _props.ActionContent;


      return _react2.default.createElement(
        Blocker,
        { onClick: onClose || onBlockerClicked },
        _react2.default.createElement(
          Window,
          { onClick: this.handleModalClicked, id: id, className: className },
          this.renderTitle(),
          this.renderCloseButton(),
          _react2.default.createElement(
            Content,
            null,
            children
          ),
          actionContent && _react2.default.createElement(
            ActionContent,
            null,
            actionContent
          )
        )
      );
    }
  }]);
  return Modal;
}(_react2.default.Component);

Modal.propTypes = {
  /**
   * Content to render in the modal.
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * An optional title for the top of the modal.
   */
  title: _propTypes2.default.string,
  /**
   * Handles click events on the backdrop behind the modal or on the X in the title bar.
   */
  onClose: _propTypes2.default.func,
  /**
   * @deprecated
   * Handles click events on the backdrop, deprecated in favor of onClose
   */
  onBlockerClicked: _propTypes2.default.func,
  /**
   * Sets a natural CSS width for the modal window. Defaults to 'auto'.
   */
  naturalWidth: _propTypes2.default.string,
  /**
   * Adds a class name to the modal window element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the modal window element.
   */
  id: _propTypes2.default.string,
  /**
   * Content to render in a fixed position at the bottom of the modal - meant for action buttons, like close.
   */
  actionContent: _propTypes2.default.node,
  /**
   * A component to render the action content area
   */
  ActionContent: _propTypes2.default.func,
  /**
   * A component to render the full-screen blocker behind the modal
   */
  Blocker: _propTypes2.default.func,
  /**
   * A component to render the content inside the modal window
   */
  Content: _propTypes2.default.func,
  /**
   * A component to render the title at the top of the modal
   */
  Title: _propTypes2.default.func,
  /**
   * A component to render the window container of the modal
   */
  Window: _propTypes2.default.func,
  /**
   * A component to render the close button of the modal
   */
  CloseButton: _propTypes2.default.func
};
Modal.defaultProps = {
  title: null,
  onBlockerClicked: function onBlockerClicked() {
    return null;
  },
  className: null,
  id: null,
  actionContent: null,
  ActionContent: _ModalActionContent2.default,
  Blocker: _ModalBlocker2.default,
  Content: _ModalContent2.default,
  Title: _ModalTitle2.default,
  Window: _ModalWindow2.default,
  CloseButton: _ModalCloseButton2.default
};
exports.default = Modal;