import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Children, cloneElement, Component } from 'react';
import classNames from 'classnames';
import CollapsePanel from './Panel';
import openAnimationFactory from './openAnimationFactory';
import ConfigContext from '../../config-provider/ConfigContext';

function toArray(activeKey) {
  var currentActiveKey = activeKey;

  if (!Array.isArray(currentActiveKey)) {
    currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
  }

  return currentActiveKey;
}

var Collapse = /*#__PURE__*/function (_Component) {
  _inherits(Collapse, _Component);

  var _super = _createSuper(Collapse);

  function Collapse(props) {
    var _this;

    _classCallCheck(this, Collapse);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "onClickItem", function (key) {
      var activeKey = _this.state.activeKey;

      if (_this.props.accordion) {
        activeKey = activeKey[0] === key ? [] : [key];
      } else {
        activeKey = _toConsumableArray(activeKey);
        var index = activeKey.indexOf(key);
        var isActive = index > -1;

        if (isActive) {
          // remove active state
          activeKey.splice(index, 1);
        } else {
          activeKey.push(key);
        }
      }

      _this.setActiveKey(activeKey);
    });

    var _this$props = _this.props,
        _activeKey = _this$props.activeKey,
        defaultActiveKey = _this$props.defaultActiveKey;
    var currentActiveKey = defaultActiveKey;

    if ('activeKey' in _this.props) {
      currentActiveKey = _activeKey;
    }

    _this.state = {
      openAnimation: _this.props.openAnimation || openAnimationFactory(_this.props.prefixCls),
      activeKey: toArray(currentActiveKey)
    };
    return _this;
  }

  _createClass(Collapse, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('activeKey' in nextProps) {
        this.setState({
          activeKey: toArray(nextProps.activeKey)
        });
      }

      if ('openAnimation' in nextProps) {
        this.setState({
          openAnimation: nextProps.openAnimation
        });
      }
    }
  }, {
    key: "getItems",
    value: function getItems() {
      var _this2 = this;

      var activeKey = this.state.activeKey;
      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          accordion = _this$props2.accordion,
          destroyInactivePanel = _this$props2.destroyInactivePanel,
          expandIcon = _this$props2.expandIcon,
          expandIconPosition = _this$props2.expandIconPosition;
      var newChildren = [];
      var getConfig = this.context.getConfig;
      Children.forEach(this.props.children, function (child, index) {
        if (!child) return; // If there is no key provide, use the panel order as default key

        var key = child.key || String(index);
        var _child$props = child.props,
            header = _child$props.header,
            headerClass = _child$props.headerClass,
            disabled = _child$props.disabled,
            collapsible = _child$props.collapsible;
        var isActive = false;

        if (accordion) {
          isActive = activeKey[0] === key;
        } else {
          isActive = activeKey.indexOf(key) > -1;
        }

        var trigger = collapsible || _this2.props.collapsible || _this2.props.trigger || getConfig('collapseTrigger');
        var props = {
          key: key,
          header: header,
          headerClass: headerClass,
          isActive: isActive,
          prefixCls: prefixCls,
          destroyInactivePanel: destroyInactivePanel,
          openAnimation: _this2.state.openAnimation,
          accordion: accordion,
          children: child.props.children,
          onItemClick: disabled ? null : _this2.onClickItem,
          expandIcon: expandIcon,
          expandIconPosition: expandIconPosition,
          trigger: trigger,
          eventKey: key
        };
        newChildren.push( /*#__PURE__*/cloneElement(child, props));
      });
      return newChildren;
    }
  }, {
    key: "setActiveKey",
    value: function setActiveKey(activeKey) {
      if (!('activeKey' in this.props)) {
        this.setState({
          activeKey: activeKey
        });
      }

      this.props.onChange(this.props.accordion ? activeKey[0] : activeKey);
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props3 = this.props,
          prefixCls = _this$props3.prefixCls,
          className = _this$props3.className,
          style = _this$props3.style,
          accordion = _this$props3.accordion;
      var collapseClassName = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, className, !!className), _classNames));
      return /*#__PURE__*/React.createElement("div", {
        className: collapseClassName,
        style: style,
        role: accordion ? 'tablist' : null
      }, this.getItems());
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return Collapse;
}(Component);

_defineProperty(Collapse, "defaultProps", {
  prefixCls: 'rc-collapse',
  onChange: function onChange() {},
  accordion: false,
  destroyInactivePanel: false
});

_defineProperty(Collapse, "Panel", CollapsePanel);

export { Collapse as default };
//# sourceMappingURL=Collapse.js.map
