var _excluded = ["border", "disabled", "children", "readonly"];

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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useMemo, useRef, useContext, useState, forwardRef, useImperativeHandle } from 'react';
import clsx from 'clsx';
import Cell from '../cell';
import CollapseContext from './CollapseContext';
import useLazyRender from '../hooks/use-lazy-render';
import { raf, doubleRaf } from '../utils/raf';
import { useUpdateEffect } from '../hooks';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var CollapseItem = /*#__PURE__*/forwardRef(function (props, ref) {
  var index = props.index;
  var parent = useContext(CollapseContext);

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('collapse-item', prefixCls),
      bem = _createNamespace[0];

  var wrapperRef = useRef(null);
  var contentRef = useRef(null);
  var name = useMemo(function () {
    var _props$name;

    return (_props$name = props.name) != null ? _props$name : index;
  }, [index, props.name]);
  var expanded = useMemo(function () {
    if (parent) {
      return parent.isExpanded(name);
    }

    return null;
  }, [parent, name]);

  var _useState = useState(function () {
    return expanded;
  }),
      show = _useState[0],
      setShow = _useState[1];

  var lazyRender = useLazyRender(show);

  var onTransitionEnd = function onTransitionEnd() {
    if (!expanded) {
      setShow(false);
    } else {
      wrapperRef.current.style.height = '';
    }
  };

  useUpdateEffect(function () {
    if (expanded) {
      setShow(true);
    }

    raf(function () {
      if (!contentRef.current || !wrapperRef.current) {
        return;
      }

      var offsetHeight = contentRef.current.offsetHeight;

      if (offsetHeight) {
        var contentHeight = offsetHeight + "px";
        wrapperRef.current.style.height = expanded ? 0 : contentHeight; // use double raf to ensure animation can start

        doubleRaf(function () {
          wrapperRef.current.style.height = expanded ? contentHeight : 0;
        });
      } else {
        onTransitionEnd();
      }
    });
  }, [expanded]);

  var toggle = function toggle(value) {
    if (value === void 0) {
      value = !expanded;
    }

    parent.toggle(name, value);
  };

  var onClickTitle = function onClickTitle() {
    if (!props.disabled && !props.readonly) {
      toggle();
    }
  };

  var renderTitle = function renderTitle() {
    var border = props.border,
        disabled = props.disabled,
        children = props.children,
        readonly = props.readonly,
        others = _objectWithoutPropertiesLoose(props, _excluded);

    return /*#__PURE__*/React.createElement(Cell, _extends({
      className: clsx(bem('title', {
        disabled: disabled,
        expanded: expanded,
        borderless: !border
      })),
      "aria-expanded": String(expanded),
      onClick: onClickTitle
    }, others, {
      isLink: readonly ? false : others.isLink,
      clickable: disabled || readonly ? false : others.clickable
    }));
  };

  var renderContent = lazyRender(function () {
    return /*#__PURE__*/React.createElement("div", {
      ref: wrapperRef,
      className: clsx(bem('wrapper')),
      onTransitionEnd: onTransitionEnd
    }, /*#__PURE__*/React.createElement("div", {
      ref: contentRef,
      className: clsx(bem('content'))
    }, props.children));
  });
  useImperativeHandle(ref, function () {
    return {
      toggle: toggle
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(bem({
      border: index && props.border
    }))
  }, renderTitle(), renderContent());
});
CollapseItem.defaultProps = {
  isLink: true,
  border: true
};
export default CollapseItem;