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

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Icon from '../../base/Icon';
import Heading from '../../atoms/headings/Heading';
import Collapse from '../../animations/Collapse';
import './style.css';

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 = classNames({
      'ma__accordion-item': !secondary,
      'ma__accordion-item--secondary': secondary,
      'ma__accordion-item--borderless': !border && !secondary
    });
    var buttonClasses = classNames({
      '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 = classNames({
      'ma__accordion-header__title': !secondary,
      'ma__accordion-header__title--secondary': secondary
    });

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

  return AccordionItem;
}(React.Component);

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

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

export default AccordionItem;