var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_cell = __toESM(require("../cell"));
var import_CollapseContext = __toESM(require("./CollapseContext"));
var import_use_lazy_render = __toESM(require("../hooks/use-lazy-render"));
var import_raf = require("../utils/raf");
var import_hooks = require("../hooks");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
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);
}
const CollapseItem = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    index
  } = props;
  const parent = (0, import_react.useContext)(import_CollapseContext.default);
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("collapse-item", prefixCls);
  const wrapperRef = (0, import_react.useRef)(null);
  const contentRef = (0, import_react.useRef)(null);
  const name = (0, import_react.useMemo)(() => {
    var _a;
    return (_a = props.name) != null ? _a : index;
  }, [props.name]);
  const expanded = (0, import_react.useMemo)(() => {
    if (parent) {
      return parent.isExpanded(name);
    }
    return null;
  }, [parent, name]);
  const [show, setShow] = (0, import_react.useState)(() => expanded);
  const lazyRender = (0, import_use_lazy_render.default)(show);
  const onTransitionEnd = () => {
    if (!expanded) {
      setShow(false);
    } else {
      wrapperRef.current.style.height = "";
    }
  };
  (0, import_hooks.useUpdateEffect)(() => {
    if (expanded) {
      setShow(true);
    }
    (0, import_raf.raf)(() => {
      if (!contentRef.current || !wrapperRef.current) {
        return;
      }
      const {
        offsetHeight
      } = contentRef.current;
      if (offsetHeight) {
        const contentHeight = `${offsetHeight}px`;
        wrapperRef.current.style.height = expanded ? 0 : contentHeight;
        (0, import_raf.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 = () => {
    const _a = props, {
      border,
      disabled,
      children,
      readonly
    } = _a, others = __objRest(_a, [
      "border",
      "disabled",
      "children",
      "readonly"
    ]);
    return /* @__PURE__ */ import_react.default.createElement(import_cell.default, _extends({
      className: (0, import_classnames.default)(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(() => /* @__PURE__ */ import_react.default.createElement("div", {
    ref: wrapperRef,
    className: (0, import_classnames.default)(bem("wrapper")),
    onTransitionEnd
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: contentRef,
    className: (0, import_classnames.default)(bem("content"))
  }, props.children)));
  (0, import_react.useImperativeHandle)(ref, () => ({
    toggle
  }));
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem({
      border: index && props.border
    }))
  }, renderTitle(), renderContent());
});
CollapseItem.defaultProps = {
  isLink: true,
  border: true
};
CollapseItem.displayName = "CollapseItem";
var stdin_default = CollapseItem;
