"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _bem = require("@zarm-design/bem");

var _configProvider = require("../config-provider");

var _hooks = require("../utils/hooks");

var CollapseItem = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var title = props.title,
      className = props.className,
      disabled = props.disabled,
      animated = props.animated,
      isActive = props.isActive,
      children = props.children,
      onChange = props.onChange,
      rest = (0, _objectWithoutProperties2.default)(props, ["title", "className", "disabled", "animated", "isActive", "children", "onChange"]);

  var content = ref || /*#__PURE__*/_react.default.createRef();

  var collapseItemRef = ref || /*#__PURE__*/_react.default.createRef();

  var _React$useContext = _react.default.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('collapse-item', {
    prefixCls: prefixCls
  });

  var onClickItem = function onClickItem() {
    if (disabled) return;
    typeof onChange === 'function' && onChange(isActive);
  };

  var getContentHeight = function getContentHeight(ele) {
    var contentChildren = (0, _toConsumableArray2.default)(ele.children);
    return contentChildren.reduce(function (res, next) {
      res += next.offsetHeight;
      return res;
    }, 0);
  };

  var setStyle = _react.default.useCallback(function () {
    if (!content.current) return;
    content.current.style.height = isActive ? "".concat(getContentHeight(content.current), "px") : '0px';
  }, [content, isActive]);

  var cls = bem([{
    active: isActive,
    disabled: disabled
  }, className]);
  (0, _hooks.useSafeLayoutEffect)(function () {
    setStyle();
  }, [setStyle]);
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    className: cls
  }, rest, {
    ref: collapseItemRef
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('header'),
    onClick: onClickItem
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('title')
  }, title), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('arrow')
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('content'),
    ref: content
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('content__inner')
  }, children)));
});

CollapseItem.displayName = 'CollapseItem';
CollapseItem.defaultProps = {
  animated: false,
  disabled: false
};
var _default = CollapseItem;
exports.default = _default;