"use strict";

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

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

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

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

var _rcMotion = _interopRequireDefault(require("rc-motion"));

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

var _PanelContent = _interopRequireDefault(require("./PanelContent"));

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 _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }

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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

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

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();
/* eslint-disable react/prop-types */


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

  function CollapsePanel() {
    var _this = _super !== null && _super.apply(this, arguments) || this;

    _this.onItemClick = function () {
      var _a = _this.props,
          onItemClick = _a.onItemClick,
          panelKey = _a.panelKey;

      if (typeof onItemClick === 'function') {
        onItemClick(panelKey);
      }
    };

    _this.handleKeyPress = function (e) {
      if (e.key === 'Enter' || e.keyCode === 13 || e.which === 13) {
        _this.onItemClick();
      }
    };

    _this.renderIcon = function () {
      var _a = _this.props,
          showArrow = _a.showArrow,
          expandIcon = _a.expandIcon,
          prefixCls = _a.prefixCls,
          collapsible = _a.collapsible;

      if (!showArrow) {
        return null;
      }

      var iconNode = typeof expandIcon === 'function' ? expandIcon(_this.props) : /*#__PURE__*/React.createElement("i", {
        className: "arrow"
      });
      return iconNode && /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-expand-icon"),
        onClick: collapsible === 'header' ? _this.onItemClick : null
      }, iconNode);
    };

    _this.renderTitle = function () {
      var _a = _this.props,
          header = _a.header,
          prefixCls = _a.prefixCls,
          collapsible = _a.collapsible;
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-header-text"),
        onClick: collapsible === 'header' ? _this.onItemClick : null
      }, header);
    };

    return _this;
  }

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

  CollapsePanel.prototype.render = function () {
    var _a, _b;

    var _c = this.props,
        className = _c.className,
        id = _c.id,
        style = _c.style,
        prefixCls = _c.prefixCls,
        headerClass = _c.headerClass,
        children = _c.children,
        isActive = _c.isActive,
        destroyInactivePanel = _c.destroyInactivePanel,
        accordion = _c.accordion,
        forceRender = _c.forceRender,
        openMotion = _c.openMotion,
        extra = _c.extra,
        collapsible = _c.collapsible;
    var disabled = collapsible === 'disabled';
    var collapsibleHeader = collapsible === 'header';
    var itemCls = (0, _classnames["default"])((_a = {}, _a["".concat(prefixCls, "-item")] = true, _a["".concat(prefixCls, "-item-active")] = isActive, _a["".concat(prefixCls, "-item-disabled")] = disabled, _a), className);
    var headerCls = (0, _classnames["default"])("".concat(prefixCls, "-header"), (_b = {}, _b[headerClass] = headerClass, _b["".concat(prefixCls, "-header-collapsible-only")] = collapsibleHeader, _b));
    /** header 节点属性 */

    var headerProps = {
      className: headerCls,
      'aria-expanded': isActive,
      'aria-disabled': disabled,
      onKeyPress: this.handleKeyPress
    };

    if (!collapsibleHeader) {
      headerProps.onClick = this.onItemClick;
      headerProps.role = accordion ? 'tab' : 'button';
      headerProps.tabIndex = disabled ? -1 : 0;
    }

    var ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean';
    return /*#__PURE__*/React.createElement("div", {
      className: itemCls,
      id: id,
      style: style
    }, /*#__PURE__*/React.createElement("div", headerProps, this.renderIcon(), this.renderTitle(), ifExtraExist && /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-extra")
    }, extra)), /*#__PURE__*/React.createElement(_rcMotion["default"], _extends({
      leavedClassName: "".concat(prefixCls, "-content-hidden"),
      visible: isActive
    }, openMotion, {
      forceRender: forceRender,
      removeOnLeave: destroyInactivePanel
    }), function (_a, ref) {
      var motionClassName = _a.className,
          motionStyle = _a.style;
      return /*#__PURE__*/React.createElement(_PanelContent["default"], {
        ref: ref,
        className: motionClassName,
        forceRender: forceRender,
        isActive: isActive,
        prefixCls: prefixCls,
        role: accordion ? 'tabpanel' : null,
        style: motionStyle
      }, children);
    }));
  };

  CollapsePanel.defaultProps = {
    showArrow: true,
    isActive: false,
    onItemClick: function onItemClick() {},
    headerClass: '',
    forceRender: false
  };
  return CollapsePanel;
}(React.Component);

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