import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { 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';
const CollapseItem = forwardRef((_a, ref) => {
  var {
    className,
    style
  } = _a,
      props = __rest(_a, ["className", "style"]);

  const {
    index
  } = props;
  const parent = useContext(CollapseContext);
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('collapse-item', prefixCls);
  const wrapperRef = useRef(null);
  const contentRef = useRef(null);
  const name = useMemo(() => {
    var _a;

    return (_a = props.name) !== null && _a !== void 0 ? _a : index;
  }, [index, props.name]);
  const expanded = useMemo(() => {
    if (parent) {
      return parent.isExpanded(name);
    }

    return null;
  }, [parent, name]);
  const [show, setShow] = useState(() => expanded);
  const lazyRender = useLazyRender(show);

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

  useUpdateEffect(() => {
    if (expanded) {
      setShow(true);
    }

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

      const {
        offsetHeight
      } = contentRef.current;

      if (offsetHeight) {
        const contentHeight = `${offsetHeight}px`;
        wrapperRef.current.style.height = expanded ? 0 : contentHeight; // use double raf to ensure animation can start

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

  const toggle = (value = !expanded) => {
    parent.toggle(name, value);
  };

  const onClickTitle = () => {
    if (!props.disabled && !props.readonly) {
      toggle();
    }
  };

  const renderTitle = () => {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const {
      border,
      disabled,
      children,
      readonly
    } = props,
          others = __rest(props, ["border", "disabled", "children", "readonly"]);

    return _jsx(Cell, Object.assign({
      className: clsx(bem('title', {
        disabled,
        expanded,
        borderless: !border
      })),
      "aria-expanded": String(expanded),
      onClick: onClickTitle
    }, others, {
      isLink: readonly ? false : others.isLink,
      clickable: disabled || readonly ? false : others.clickable
    }));
  };

  const renderContent = lazyRender(() => _jsx("div", Object.assign({
    ref: wrapperRef,
    className: clsx(bem('wrapper')),
    onTransitionEnd: onTransitionEnd
  }, {
    children: _jsx("div", Object.assign({
      ref: contentRef,
      className: clsx(bem('content'))
    }, {
      children: props.children
    }))
  })));
  useImperativeHandle(ref, () => ({
    toggle
  }));
  return _jsxs("div", Object.assign({
    style: style,
    className: clsx(className, bem({
      border: index && props.border
    }))
  }, {
    children: [renderTitle(), renderContent()]
  }));
});
CollapseItem.defaultProps = {
  isLink: true,
  border: true
};
export default CollapseItem;