"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _shallowequal = _interopRequireDefault(require("shallowequal"));

var _toArray = _interopRequireDefault(require("rc-util/es/Children/toArray"));

var _Panel = _interopRequireDefault(require("./Panel"));

var _motionUtil = _interopRequireDefault(require("./motionUtil"));

require("./index.less");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __spreadArrays = void 0 && (void 0).__spreadArrays || function () {
  for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
    s += arguments[i].length;
  }

  for (var r = Array(s), k = 0, i = 0; i < il; i++) {
    for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
      r[k] = a[j];
    }
  }

  return r;
};
/* eslint-disable react/prop-types */


function getActiveKeysArray(activeKey) {
  var currentActiveKey = activeKey;

  if (!Array.isArray(currentActiveKey)) {
    var activeKeyType = _typeof(currentActiveKey);

    currentActiveKey = activeKeyType === 'number' || activeKeyType === 'string' ? [currentActiveKey] : [];
  }

  return currentActiveKey.map(function (key) {
    return String(key);
  });
}

var Collapse =
/** @class */
function (_super) {
  __extends(Collapse, _super);

  function Collapse(props) {
    var _this = _super.call(this, props) || this;

    _this.onClickItem = function (key) {
      var activeKey = _this.state.activeKey;

      if (_this.props.accordion) {
        activeKey = activeKey[0] === key ? [] : [key];
      } else {
        activeKey = __spreadArrays(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);
    };

    _this.getNewChild = function (child, index) {
      if (!child) return null;
      var activeKey = _this.state.activeKey;
      var _a = _this.props,
          prefixCls = _a.prefixCls,
          _b = _a.openMotion,
          openMotion = _b === void 0 ? _motionUtil["default"] : _b,
          accordion = _a.accordion,
          rootDestroyInactivePanel = _a.destroyInactivePanel,
          expandIcon = _a.expandIcon,
          collapsible = _a.collapsible; // If there is no key provide, use the panel order as default key

      var key = child.key || String(index);
      var _c = child.props,
          header = _c.header,
          headerClass = _c.headerClass,
          destroyInactivePanel = _c.destroyInactivePanel,
          childCollapsible = _c.collapsible;
      var isActive = false;

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

      var mergeCollapsible = childCollapsible !== null && childCollapsible !== void 0 ? childCollapsible : collapsible;
      var props = {
        key: key,
        panelKey: key,
        header: header,
        headerClass: headerClass,
        isActive: isActive,
        prefixCls: prefixCls,
        destroyInactivePanel: destroyInactivePanel !== null && destroyInactivePanel !== void 0 ? destroyInactivePanel : rootDestroyInactivePanel,
        openMotion: openMotion,
        accordion: accordion,
        children: child.props.children,
        onItemClick: mergeCollapsible === 'disabled' ? null : _this.onClickItem,
        expandIcon: expandIcon,
        collapsible: mergeCollapsible
      }; // https://github.com/ant-design/ant-design/issues/20479

      if (typeof child.type === 'string') {
        return child;
      }

      Object.keys(props).forEach(function (propName) {
        if (typeof props[propName] === 'undefined') {
          delete props[propName];
        }
      });
      return /*#__PURE__*/React.cloneElement(child, props);
    };

    _this.getItems = function () {
      var children = _this.props.children;
      return (0, _toArray["default"])(children).map(_this.getNewChild);
    };

    _this.setActiveKey = function (activeKey) {
      if (!('activeKey' in _this.props)) {
        _this.setState({
          activeKey: activeKey
        });
      }

      _this.props.onChange(_this.props.accordion ? activeKey[0] : activeKey);
    };

    var activeKey = props.activeKey,
        defaultActiveKey = props.defaultActiveKey;
    var currentActiveKey = defaultActiveKey;

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

    _this.state = {
      activeKey: getActiveKeysArray(currentActiveKey)
    };
    return _this;
  }

  Collapse.prototype.shouldComponentUpdate = function (nextProps, nextState) {
    return !(0, _shallowequal["default"])(this.props, nextProps) || !(0, _shallowequal["default"])(this.state, nextState);
  };

  Collapse.getDerivedStateFromProps = function (nextProps) {
    var newState = {};

    if ('activeKey' in nextProps) {
      newState.activeKey = getActiveKeysArray(nextProps.activeKey);
    }

    return newState;
  };

  Collapse.prototype.render = function () {
    var _a;

    var _b = this.props,
        prefixCls = _b.prefixCls,
        className = _b.className,
        style = _b.style,
        accordion = _b.accordion;
    var collapseClassName = (0, _classnames["default"])((_a = {}, _a[prefixCls] = true, _a[className] = !!className, _a));
    return /*#__PURE__*/React.createElement("div", {
      className: collapseClassName,
      role: accordion ? 'tablist' : null,
      style: style
    }, this.getItems());
  };

  Collapse.defaultProps = {
    prefixCls: 'rc-collapse',
    onChange: function onChange() {},
    accordion: false,
    destroyInactivePanel: false
  };
  Collapse.Panel = _Panel["default"];
  return Collapse;
}(React.Component);

var _default = Collapse;
exports["default"] = _default;