import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import React, { useEffect, useContext, useRef, useMemo } from "react";
import classNames from "classnames";
import { ComponentDefaults } from "../../utils/typings";
import CollapseContext from "../collapse/context";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    title: null,
    name: '',
    expandIcon: null,
    disabled: false,
    extra: null
});
export var CollapseItem = function(props) {
    var _ref = _object_spread({}, defaultProps, props), children = _ref.children, title = _ref.title, name = _ref.name, disabled = _ref.disabled, expandIcon = _ref.expandIcon, rotate = _ref.rotate, extra = _ref.extra, style = _ref.style, className = _ref.className, rest = _object_without_properties(_ref, [
        "children",
        "title",
        "name",
        "disabled",
        "expandIcon",
        "rotate",
        "extra",
        "style",
        "className"
    ]);
    var classPrefix = 'nut-collapse-item';
    var context = useContext(CollapseContext);
    var wrapperRef = useRef(null);
    var contentRef = useRef(null);
    var expanded = useMemo(function() {
        if (context) {
            return context.isOpen(name);
        }
        return false;
    }, [
        name,
        context.isOpen
    ]);
    var iconStyle = useMemo(function() {
        return expanded ? {
            transform: "translateY(-50%) rotate(".concat(rotate || context.rotate, "deg)")
        } : {
            transform: 'translateY(-50%)'
        };
    }, [
        expanded,
        rotate
    ]);
    var handleClick = function() {
        if (!disabled) {
            context.updateValue(name);
        }
    };
    var onTransitionEnd = function() {
        if (expanded) {
            if (wrapperRef.current) {
                wrapperRef.current.style.height = '';
            }
        }
    };
    var getOffsetHeight = function() {
        var _contentRef_current;
        var height = (_contentRef_current = contentRef.current) === null || _contentRef_current === void 0 ? void 0 : _contentRef_current.offsetHeight;
        return height ? "".concat(height, "px") : '';
    };
    var toggle = function() {
        var start = expanded ? '0px' : getOffsetHeight();
        if (wrapperRef.current) {
            wrapperRef.current.style.height = start;
        }
        requestAnimationFrame(function() {
            requestAnimationFrame(function() {
                var end = expanded ? getOffsetHeight() : '0px';
                if (wrapperRef.current) {
                    wrapperRef.current.style.height = end;
                }
            });
        });
    };
    var init = useRef(true);
    useEffect(function() {
        if (init.current) {
            init.current = false;
            if (!expanded) {
                wrapperRef.current.style.height = '0px';
            }
        } else {
            toggle();
        }
    }, [
        expanded
    ]);
    return /*#__PURE__*/ React.createElement("div", _object_spread({
        className: classNames(classPrefix, className),
        style: style
    }, rest), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(classPrefix, "-header"), {
            disabled: disabled
        }),
        onClick: handleClick
    }, /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-title")
    }, title), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-extra")
    }, extra), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-icon-box")
    }, /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-icon"),
        style: iconStyle
    }, expandIcon || context.expandIcon))), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-content"),
        onTransitionEnd: onTransitionEnd,
        ref: wrapperRef
    }, /*#__PURE__*/ React.createElement("div", {
        ref: contentRef,
        className: "".concat(classPrefix, "-content-text")
    }, children)));
};
CollapseItem.displayName = 'NutCollapseItem';
