import { jsx as ___EmotionJSX } from "@emotion/core";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

import PropTypes from 'prop-types';
import React from 'react';
import { COLORS } from '../../constants';
import ChevronIcon from '../../svgs/icons/chevron-icon.svg';
import Thumbnails from './thumbnails';
import { AccordionBox, ArrowWrapper, TitleWrapper, ExpansionWrapper, Container, Content, Truncate } from './style';

var noop = function noop() {};

var Accordion =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Accordion, _React$Component);

  function Accordion(props) {
    var _this;

    _classCallCheck(this, Accordion);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Accordion).call(this, props));
    _this.contentRef = React.createRef();

    _this.getContentHeight = function (isOpen) {
      return "".concat(isOpen && _this.contentRef.current ? _this.contentRef.current.clientHeight : '0', "px");
    };

    _this.state = {
      contentHeight: _this.getContentHeight(props.isOpen)
    };
    return _this;
  }

  _createClass(Accordion, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.updateHeight();
    } // prettier-ignore

  }, {
    key: "updateHeight",
    value: function updateHeight() {
      var isOpen = this.props.isOpen;
      var contentHeight = this.state.contentHeight;

      if (contentHeight !== this.getContentHeight(isOpen)) {
        this.setState({
          contentHeight: this.getContentHeight(isOpen)
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var contentHeight = this.state.contentHeight;
      var _this$props = this.props,
          title = _this$props.title,
          isOpen = _this$props.isOpen,
          onClick = _this$props.onClick,
          children = _this$props.children,
          noBorder = _this$props.noBorder,
          disabled = _this$props.disabled;
      return ___EmotionJSX(AccordionBox, {
        isOpen: isOpen,
        noBorder: noBorder,
        disabled: disabled
      }, ___EmotionJSX(TitleWrapper, {
        onClick: disabled ? noop : onClick,
        disabled: disabled
      }, ___EmotionJSX(Truncate, null, title), ___EmotionJSX(ArrowWrapper, null, ___EmotionJSX(ChevronIcon, {
        rotate: isOpen ? 90 : 0,
        width: 8,
        height: 8,
        fill: COLORS.purple
      }))), ___EmotionJSX(ExpansionWrapper, {
        contentHeight: contentHeight
      }, ___EmotionJSX("div", {
        ref: this.contentRef
      }, children)));
    }
  }]);

  return Accordion;
}(React.Component);

Accordion.propTypes = {
  title: PropTypes.node.isRequired,
  isOpen: PropTypes.bool.isRequired,
  onClick: PropTypes.func.isRequired,
  children: PropTypes.node.isRequired,
  noBorder: PropTypes.bool,
  disabled: PropTypes.bool
};
Accordion.defaultProps = {
  noBorder: false,
  disabled: false
};
Accordion.Container = Container;
Accordion.Content = Content;
Accordion.Thumbnails = Thumbnails;
Accordion.Truncate = Truncate;
export default Accordion;