'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 _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _reactCollapse = require('react-collapse');

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

/**
 * A prototypical accordion element with no styling. Renders one element which can be
 * clicked to toggle expanded state, and an expandable content area.
 */
var ExpandToggle = function (_React$Component) {
  (0, _inherits3.default)(ExpandToggle, _React$Component);

  function ExpandToggle(props) {
    (0, _classCallCheck3.default)(this, ExpandToggle);

    var _this = (0, _possibleConstructorReturn3.default)(this, (ExpandToggle.__proto__ || (0, _getPrototypeOf2.default)(ExpandToggle)).call(this, props));

    _this.handleToggle = function () {
      if (_this.props.disabled) {
        return;
      }

      if (_this.props.onToggle) {
        _this.props.onToggle(_this.calcIsExpanded());
      }
      _this.setState(function (state) {
        return { internalIsExpanded: !state.internalIsExpanded };
      });
    };

    _this.calcIsExpanded = function () {
      var isExpanded = _this.props.isExpanded;
      var internalIsExpanded = _this.state.internalIsExpanded;

      if (isExpanded === null) {
        return internalIsExpanded;
      }

      return isExpanded;
    };

    _this.renderToggle = function () {
      var _this$props = _this.props,
          toggleContent = _this$props.toggleContent,
          disabled = _this$props.disabled;

      if (_lodash2.default.isFunction(toggleContent)) {
        return toggleContent(_this.calcIsExpanded(), disabled);
      }
      return toggleContent;
    };

    _this.state = {
      internalIsExpanded: props.startExpanded
    };
    return _this;
  }

  (0, _createClass3.default)(ExpandToggle, [{
    key: 'render',
    value: function render() {
      var isExpanded = this.calcIsExpanded();
      var _props = this.props,
          children = _props.children,
          id = _props.id,
          className = _props.className,
          springConfig = _props.springConfig,
          disabled = _props.disabled,
          unmountClosed = _props.unmountClosed;

      var CollapseType = unmountClosed ? _reactCollapse.UnmountClosed : _reactCollapse.Collapse;

      return _react2.default.createElement(
        'div',
        { id: id, className: className },
        _react2.default.createElement(
          'div',
          {
            style: { cursor: disabled ? 'auto' : 'pointer' },
            onClick: this.handleToggle
          },
          this.renderToggle()
        ),
        _react2.default.createElement(
          CollapseType,
          {
            hasNestedCollapse: true,
            isOpened: isExpanded,
            springConfig: springConfig
          },
          children
        )
      );
    }
  }]);
  return ExpandToggle;
}(_react2.default.Component);

ExpandToggle.propTypes = {
  /**
   * Sets the id of the containing collapsible element
   */
  id: _propTypes2.default.string,
  /**
   * Sets the className of the containing collapsible element
   */
  className: _propTypes2.default.string,
  /**
   * Callback for toggle events
   */
  onToggle: _propTypes2.default.func,
  /**
   * Content to render within the toggle area. You may optionally pass a function,
   * which will be called with the current toggle on/off state.
   */
  toggleContent: _propTypes2.default.node.isRequired,
  /**
   * Override ('control') toggling behavior, disabling default internal toggle state
   */
  isExpanded: _propTypes2.default.bool,
  /**
   * Content to render inside the collapsible area
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * Whether to start expanded
   */
  startExpanded: _propTypes2.default.bool,
  /**
   * If true, the user cannot change the state
   */
  disabled: _propTypes2.default.bool,
  /**
   * If true, contents are unmounted when the accordion is closed.
   */
  unmountClosed: _propTypes2.default.bool,
  /**
   * react-motion config, see https://github.com/chenglou/react-motion#--spring-val-number-config-springhelperconfig--opaqueconfig
   */
  springConfig: _propTypes2.default.shape({
    stiffness: _propTypes2.default.number,
    damping: _propTypes2.default.number,
    precision: _propTypes2.default.number
  })
};
ExpandToggle.defaultProps = {
  id: null,
  className: null,
  onToggle: function onToggle() {
    return null;
  },
  isExpanded: null,
  startExpanded: false,
  springConfig: null,
  disabled: false,
  toggleContent: null,
  unmountClosed: false
};
exports.default = ExpandToggle;