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

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

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

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

    var _temp, _this, _ret;

    _classCallCheck(this, Accordion);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Accordion.__proto__ || Object.getPrototypeOf(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) {
      return _react2.default.createElement(
        _this2.props.Label,
        { onClick: _this.handleToggle },
        _react2.default.createElement(_this2.props.Arrow, { isExpanded: isExpanded, name: 'forward', size: 21 }),
        _react2.default.createElement(
          _this2.props.LabelText,
          null,
          _this.props.label
        )
      );
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

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


      return _react2.default.createElement(
        Border,
        null,
        _react2.default.createElement(
          _ExpandToggle2.default,
          {
            id: id,
            className: className,
            onToggle: onToggle,
            toggleContent: this.renderLabel,
            isExpanded: this.coalesceIsExpandedProps()
          },
          _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
   */
  isExpanded: _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,
  /**
   * Set a classname for the accordion container element.
   */
  className: _propTypes2.default.string,
  /**
   * Set an id for the accordion container element.
   */
  id: _propTypes2.default.string,
  /**
   * 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,
  isCollapsed: null,
  onToggle: null,
  className: null,
  id: null,
  Border: _AccordionBorder2.default,
  Label: _AccordionLabel2.default,
  Arrow: _AccordionArrow2.default,
  LabelText: _AccordionLabelText2.default,
  Content: _AccordionContent2.default
};


Accordion.Small = (0, _recompose.withProps)({
  Border: _AccordionBorder2.default.Small,
  Arrow: _AccordionArrow2.default.Small,
  Label: _AccordionLabel2.default.Small,
  Content: _AccordionContent2.default.Small
})(Accordion);
Accordion.Group = _AccordionGroup2.default;

exports.default = Accordion;