'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Icon = require('../../base/Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _Heading = require('../../atoms/headings/Heading');

var _Heading2 = _interopRequireDefault(_Heading);

var _Collapse = require('../../animations/Collapse');

var _Collapse2 = _interopRequireDefault(_Collapse);

require('./style.css');

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

var AccordionItem = function (_React$Component) {
  _inherits(AccordionItem, _React$Component);

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.state = {
      open: false
    };
    _this.handleClick = _this.handleClick.bind(_this);
    return _this;
  }

  AccordionItem.prototype.handleClick = function handleClick() {
    var open = this.state.open;

    this.setState({
      open: !open
    });
  };

  AccordionItem.prototype.render = function render() {
    var _props = this.props,
        secondary = _props.secondary,
        border = _props.border,
        emphasize = _props.emphasize,
        icon = _props.icon,
        title = _props.title,
        headerLevel = _props.headerLevel,
        children = _props.children,
        id = _props.id,
        info = _props.info;
    var open = this.state.open;

    var accordionClasses = (0, _classnames2.default)({
      'ma__accordion-item': !secondary,
      'ma__accordion-item--secondary': secondary,
      'ma__accordion-item--borderless': !border && !secondary
    });
    var buttonClasses = (0, _classnames2.default)({
      'ma__accordion-header__button': !secondary,
      'ma__accordion-header__button--secondary': secondary,
      'is-open': open,
      'ma__accordion-header__button--solid': emphasize && !secondary,
      'ma__accordion-header__button--trans': !emphasize && !secondary
    });
    var headingClasses = (0, _classnames2.default)({
      'ma__accordion-header__title': !secondary,
      'ma__accordion-header__title--secondary': secondary
    });

    return _react2.default.createElement(
      'div',
      { className: accordionClasses },
      _react2.default.createElement(
        'header',
        { className: 'ma__accordion-header' },
        _react2.default.createElement(
          'button',
          {
            className: buttonClasses,
            'aria-label': info,
            onClick: this.handleClick,
            'aria-expanded': open,
            'aria-controls': 'expandable-section-' + id,
            id: 'button-' + id
          },
          icon && !secondary && _react2.default.createElement(
            'div',
            { className: 'ma__accordion-header__icon' },
            icon
          ),
          secondary && _react2.default.createElement(
            'div',
            { className: 'ma__accordion-header__icon--secondary' },
            _react2.default.createElement(_Icon2.default, { name: 'chevron', svgHeight: 20, svgWidth: 20 })
          ),
          _react2.default.createElement(_Heading2.default, { 'class': headingClasses, text: title, level: headerLevel })
        )
      ),
      _react2.default.createElement(
        _Collapse2.default,
        { 'in': open, dimension: 'height' },
        _react2.default.createElement(
          'div',
          { className: 'ma__accordion-content__body', id: 'expandable-section-' + id, 'aria-labelledby': 'button-' + id },
          children
        )
      )
    );
  };

  return AccordionItem;
}(_react2.default.Component);

AccordionItem.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The title of the accordion header */
  title: _propTypes2.default.string.isRequired,
  /** Accessible aria label */
  info: _propTypes2.default.string.isRequired,
  /** The icon to display in the collapsible header */
  icon: _propTypes2.default.element,
  /** Whether the accordion should have a border or not, default is true (border) */
  border: _propTypes2.default.bool,
  /** Where the accordion header's background should remain colored when expanded */
  emphasize: _propTypes2.default.bool,
  /** Content rendered in the collapsed section. Only Paragraph, Table, Heading, OrderedList
      and UnorderList are valid children components to pass to AccordionItem */
  children: _propTypes2.default.element.isRequired,
  /** Whether to style the accordion as secondary or not. */
  secondary: _propTypes2.default.bool,
  /** Heading level for accordion title */
  headerLevel: _propTypes2.default.number,
  /** Expand button id and section id, used for a11y */
  id: _propTypes2.default.string
} : {};

AccordionItem.defaultProps = {
  icon: _react2.default.createElement(_Icon2.default, { name: 'circlechevron', svgWidth: 35, svgHeight: 35 }),
  border: true,
  emphasize: true,
  secondary: false,
  headerLevel: 2
};

exports.default = AccordionItem;
module.exports = exports['default'];