'use strict';

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

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

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

/**
 * 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) {
  _inherits(ExpandToggle, _React$Component);

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

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

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

      if (_this.props.onToggle) {
        _this.props.onToggle(_this.calcIsExpanded());
      }
      _this.setState({ internalIsExpanded: !_this.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;
  }

  _createClass(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;


      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(
          _reactCollapse.Collapse,
          { 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,
  /**
   * 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
};
exports.default = ExpandToggle;