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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
import { func, obj } from '../util';
import Panel from './panel';
/** Collapse */

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

  function Collapse(props) {
    var _this;

    _classCallCheck(this, Collapse);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Collapse).call(this, props));
    var expandedKeys;

    if ('expandedKeys' in props) {
      expandedKeys = props.expandedKeys;
    } else {
      expandedKeys = props.defaultExpandedKeys;
    }

    _this.state = {
      expandedKeys: typeof expandedKeys === 'undefined' ? [] : expandedKeys
    };
    return _this;
  }

  _createClass(Collapse, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('expandedKeys' in nextProps) {
        this.setState({
          expandedKeys: typeof nextProps.expandedKeys === 'undefined' ? [] : nextProps.expandedKeys
        });
      }
    }
  }, {
    key: "onItemClick",
    value: function onItemClick(key) {
      var expandedKeys = this.state.expandedKeys;

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

        if (isExpanded) {
          expandedKeys.splice(index, 1);
        } else {
          expandedKeys.push(key);
        }
      }

      this.setExpandedKey(expandedKeys);
    }
  }, {
    key: "genratePanelId",
    value: function genratePanelId(itemId, index) {
      var collapseId = this.props.id;
      var id;

      if (itemId) {
        // 优先用 item自带的id
        id = itemId;
      } else if (collapseId) {
        // 其次用 collapseId 和 index 生成id
        id = "".concat(collapseId, "-panel-").concat(index);
      }

      return id;
    }
  }, {
    key: "getProps",
    value: function getProps(item, index, key) {
      var _this2 = this;

      var expandedKeys = this.state.expandedKeys;
      var title = item.title;
      var disabled = this.props.disabled;

      if (!disabled) {
        disabled = item.disabled;
      }

      var isExpanded = false;

      if (this.props.accordion) {
        isExpanded = expandedKeys[0] === key;
      } else {
        isExpanded = expandedKeys.some(function (expandedKey) {
          if (expandedKey === null || expandedKey === undefined || key === null || key === undefined) {
            return false;
          }

          if (expandedKey === key || expandedKey.toString() === key.toString()) {
            return true;
          }

          return false;
        });
      }

      var id = this.genratePanelId(item.id, index);
      return {
        key: key,
        title: title,
        isExpanded: isExpanded,
        disabled: disabled,
        id: id,
        onClick: disabled ? null : function () {
          _this2.onItemClick(key);

          if ('onClick' in item) {
            item.onClick(key);
          }
        }
      };
    }
  }, {
    key: "getItemsByDataSource",
    value: function getItemsByDataSource() {
      var _this3 = this;

      var props = this.props;
      var dataSource = props.dataSource; // 是否有dataSource.item传入过key

      var hasKeys = dataSource.some(function (item) {
        return 'key' in item;
      });
      return dataSource.map(function (item, index) {
        // 传入过key就用item.key 没传入则统一使用index为key
        var key = hasKeys ? item.key : "".concat(index);
        return React.createElement(Panel, _extends({}, _this3.getProps(item, index, key), {
          key: key
        }), item.content);
      });
    }
  }, {
    key: "getItemsByChildren",
    value: function getItemsByChildren() {
      var _this4 = this;

      // 是否有child传入过key
      var allKeys = React.Children.map(this.props.children, function (child) {
        return child.key;
      });
      var hasKeys = Boolean(allKeys.length);
      return React.Children.map(this.props.children, function (child, index) {
        if (child && typeof child.type === 'function' && child.type.isNextPanel) {
          // 传入过key就用child.key 没传入则统一使用index为key
          var key = hasKeys ? child.key : "".concat(index);
          return React.cloneElement(child, _this4.getProps(child.props, index, key));
        } else {
          return child;
        }
      });
    }
  }, {
    key: "setExpandedKey",
    value: function setExpandedKey(expandedKeys) {
      if (!('expandedKeys' in this.props)) {
        this.setState({
          expandedKeys: expandedKeys
        });
      }

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

      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          style = _this$props.style,
          disabled = _this$props.disabled,
          dataSource = _this$props.dataSource,
          id = _this$props.id,
          rtl = _this$props.rtl;
      var collapseClassName = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "collapse"), true), _defineProperty(_classNames, "".concat(prefix, "collapse-disabled"), disabled), _defineProperty(_classNames, className, Boolean(className)), _classNames));
      var others = obj.pickOthers(Collapse.propTypes, this.props);
      return React.createElement("div", _extends({
        id: id,
        className: collapseClassName,
        style: style
      }, others, {
        role: "presentation",
        dir: rtl ? 'rtl' : undefined
      }), dataSource ? this.getItemsByDataSource() : this.getItemsByChildren());
    }
  }]);

  return Collapse;
}(React.Component);

_defineProperty(Collapse, "propTypes", {
  /**
   * 样式前缀
   */
  prefix: PropTypes.string,

  /**
   * 组件接受行内样式
   */
  style: PropTypes.object,

  /**
   * 使用数据模型构建
   */
  dataSource: PropTypes.array,

  /**
   * 默认展开keys
   */
  defaultExpandedKeys: PropTypes.array,

  /**
   * 受控展开keys
   */
  expandedKeys: PropTypes.array,

  /**
   * 展开状态发升变化时候的回调
   */
  onExpand: PropTypes.func,

  /**
   * 所有禁用
   */
  disabled: PropTypes.bool,

  /**
   * 扩展class
   */
  className: PropTypes.string,

  /**
   * 手风琴模式，一次只能打开一个
   */
  accordion: PropTypes.bool,
  children: PropTypes.node,
  id: PropTypes.string,
  rtl: PropTypes.bool
});

_defineProperty(Collapse, "defaultProps", {
  accordion: false,
  prefix: 'next-',
  onExpand: func.noop
});

_defineProperty(Collapse, "contextTypes", {
  prefix: PropTypes.string
});

export default ConfigProvider.config(Collapse);