'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 _recompose = require('recompose');

var _ExpandToggle = require('../../behaviors/ExpandToggle');

var _ExpandToggle2 = _interopRequireDefault(_ExpandToggle);

var _AccordionLabel = require('./styles/AccordionLabel');

var _AccordionLabel2 = _interopRequireDefault(_AccordionLabel);

var _AccordionArrow = require('./styles/AccordionArrow');

var _AccordionArrow2 = _interopRequireDefault(_AccordionArrow);

var _AccordionBorder = require('./styles/AccordionBorder');

var _AccordionBorder2 = _interopRequireDefault(_AccordionBorder);

var _AccordionLabelText = require('./styles/AccordionLabelText');

var _AccordionLabelText2 = _interopRequireDefault(_AccordionLabelText);

var _AccordionContent = require('./styles/AccordionContent');

var _AccordionContent2 = _interopRequireDefault(_AccordionContent);

var _AccordionGroup = require('./AccordionGroup');

var _AccordionGroup2 = _interopRequireDefault(_AccordionGroup);

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

/**
 * Accordion works like a controllable component. Provide the
 * isExpanded prop to control it, or don't to let it control itself.
 */
var Accordion = function (_React$Component) {
  (0, _inherits3.default)(Accordion, _React$Component);

  function Accordion() {
    var _ref,
        _this2 = this;

    var _temp, _this, _ret;

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

    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 = Accordion.__proto__ || (0, _getPrototypeOf2.default)(Accordion)).call.apply(_ref, [this].concat(args))), _this), _this.coalesceIsExpandedProps = function () {
      var _this$props = _this.props,
          isExpanded = _this$props.isExpanded,
          isCollapsed = _this$props.isCollapsed;

      if (isExpanded === null && isCollapsed === null) {
        return null;
      }
      if (isExpanded === null) {
        return !isCollapsed;
      }
      return isExpanded;
    }, _this.renderLabel = function (isExpanded, disabled) {
      return _react2.default.createElement(
        _this2.props.Label,
        { spacing: _this.props.labelSpacing, disabled: disabled },
        _react2.default.createElement(_this2.props.Arrow, { isExpanded: isExpanded, name: 'forward', size: 21 }),
        _react2.default.createElement(
          _this2.props.LabelText,
          null,
          _this.props.label
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Accordion, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          id = _props.id,
          className = _props.className,
          onToggle = _props.onToggle,
          children = _props.children,
          Border = _props.Border,
          Content = _props.Content,
          startExpanded = _props.startExpanded,
          disabled = _props.disabled,
          unmountClosed = _props.unmountClosed,
          springConfig = _props.springConfig;


      return _react2.default.createElement(
        Border,
        { disabled: disabled },
        _react2.default.createElement(
          _ExpandToggle2.default,
          {
            id: id,
            className: className,
            onToggle: onToggle,
            toggleContent: this.renderLabel,
            isExpanded: this.coalesceIsExpandedProps(),
            startExpanded: startExpanded,
            disabled: disabled,
            unmountClosed: unmountClosed,
            springConfig: springConfig
          },
          _react2.default.createElement(
            Content,
            null,
            children
          )
        )
      );
    }
  }]);
  return Accordion;
}(_react2.default.Component);

Accordion.propTypes = {
  /**
   * Content to render when the accordion is collapsed, and in the header of the expanded state.
   */
  label: _propTypes2.default.node.isRequired,
  /**
   * Content inside the collapsible part of the accordion.
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * Pass isExpanded to override the internal collapsing state (makes the expanded state a controlled value, please
   * use onToggle to manage the state yourself or startExpanded if you just want to set the initial state).
   */
  isExpanded: _propTypes2.default.bool,
  /**
   * Use this to set the default initial state of the internal expanded state without
   * turning it into a controlled value.
   */
  startExpanded: _propTypes2.default.bool,
  /**
   * DEPRECATED: the negation of isExpanded, overrides internal collapse state
   */
  isCollapsed: _propTypes2.default.bool,
  /**
   * Add a handler for when the accordion is collapsed or expanded.
   */
  onToggle: _propTypes2.default.func,
  /**
   * If true, the user cannot change the expanded state of this
   * accordion.
   */
  disabled: _propTypes2.default.bool,
  /**
   * If true, contents are unmounted when the accordion is closed. Useful for improving performance when the Accordion
   * contains a large amount of complex contents.
   */
  unmountClosed: _propTypes2.default.bool,
  /**
   * Set a classname for the accordion container element.
   */
  className: _propTypes2.default.string,
  /**
   * Set an id for the accordion container element.
   */
  id: _propTypes2.default.string,
  /**
   * 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
  }),
  /**
   * A component to render the border
   */
  Border: _propTypes2.default.func,
  /**
   * A component to render the label
   */
  Label: _propTypes2.default.func,
  /**
   * A component to render the arrow
   */
  Arrow: _propTypes2.default.func,
  /**
   * A component to render the text inside the label
   */
  LabelText: _propTypes2.default.func,
  /**
   * A component to render the content area
   */
  Content: _propTypes2.default.func
};
Accordion.defaultProps = {
  isExpanded: null,
  startExpanded: false,
  isCollapsed: null,
  onToggle: null,
  className: null,
  id: null,
  disabled: false,
  unmountClosed: false,
  springConfig: null,
  Border: _AccordionBorder2.default,
  Label: _AccordionLabel2.default,
  Arrow: _AccordionArrow2.default,
  LabelText: _AccordionLabelText2.default,
  Content: _AccordionContent2.default,
  labelSpacing: 'lg'
};


Accordion.Small = (0, _recompose.defaultProps)({
  Border: _AccordionBorder2.default.Small,
  Arrow: _AccordionArrow2.default.Small,
  Label: _AccordionLabel2.default.Small,
  labelSpacing: '20px'
})(Accordion);
Accordion.Group = _AccordionGroup2.default;

exports.default = Accordion;