import _extends from "@babel/runtime/helpers/extends";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import React from 'react';
import { createBEM } from '@zarm-design/bem';
import { ConfigContext } from '../config-provider';
import { useSafeLayoutEffect } from '../utils/hooks';
var CollapseItem = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["title", "className", "disabled", "animated", "isActive", "children", "onChange"]);

  var content = ref || /*#__PURE__*/React.createRef();
  var collapseItemRef = ref || /*#__PURE__*/React.createRef();

  var _React$useContext = React.useContext(ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var 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 = _toConsumableArray(ele.children);

    return contentChildren.reduce(function (res, next) {
      res += next.offsetHeight;
      return res;
    }, 0);
  };

  var setStyle = React.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]);
  useSafeLayoutEffect(function () {
    setStyle();
  }, [setStyle]);
  return /*#__PURE__*/React.createElement("div", _extends({
    className: cls
  }, rest, {
    ref: collapseItemRef
  }), /*#__PURE__*/React.createElement("div", {
    className: bem('header'),
    onClick: onClickItem
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('title')
  }, title), /*#__PURE__*/React.createElement("div", {
    className: bem('arrow')
  })), /*#__PURE__*/React.createElement("div", {
    className: bem('content'),
    ref: content
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('content__inner')
  }, children)));
});
CollapseItem.displayName = 'CollapseItem';
CollapseItem.defaultProps = {
  animated: false,
  disabled: false
};
export default CollapseItem;