"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CollapseItem", {
    enumerable: true,
    get: function() {
        return CollapseItem;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _typings = require("../../utils/typings");
var _context = /*#__PURE__*/ _interop_require_default._(require("../collapse/context"));
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    title: null,
    name: '',
    expandIcon: null,
    disabled: false,
    extra: null
});
var CollapseItem = function CollapseItem(props) {
    var _ref = (0, _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 = (0, _object_without_properties._)(_ref, [
        "children",
        "title",
        "name",
        "disabled",
        "expandIcon",
        "rotate",
        "extra",
        "style",
        "className"
    ]);
    var classPrefix = 'nut-collapse-item';
    var context = (0, _react.useContext)(_context.default);
    var wrapperRef = (0, _react.useRef)(null);
    var contentRef = (0, _react.useRef)(null);
    var expanded = (0, _react.useMemo)(function() {
        if (context) {
            return context.isOpen(name);
        }
        return false;
    }, [
        name,
        context.isOpen
    ]);
    var iconStyle = (0, _react.useMemo)(function() {
        return expanded ? {
            transform: "translateY(-50%) rotate(".concat(rotate || context.rotate, "deg)")
        } : {
            transform: 'translateY(-50%)'
        };
    }, [
        expanded,
        rotate
    ]);
    var handleClick = function handleClick() {
        if (!disabled) {
            context.updateValue(name);
        }
    };
    var onTransitionEnd = function onTransitionEnd() {
        if (expanded) {
            if (wrapperRef.current) {
                wrapperRef.current.style.height = '';
            }
        }
    };
    var getOffsetHeight = function getOffsetHeight() {
        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 toggle() {
        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 = (0, _react.useRef)(true);
    (0, _react.useEffect)(function() {
        if (init.current) {
            init.current = false;
            if (!expanded) {
                wrapperRef.current.style.height = '0px';
            }
        } else {
            toggle();
        }
    }, [
        expanded
    ]);
    return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        className: (0, _classnames.default)(classPrefix, className),
        style: style
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(classPrefix, "-header"), {
            disabled: disabled
        }),
        onClick: handleClick
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-title")
    }, title), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-extra")
    }, extra), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-icon-box")
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-icon"),
        style: iconStyle
    }, expandIcon || context.expandIcon))), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-content"),
        onTransitionEnd: onTransitionEnd,
        ref: wrapperRef
    }, /*#__PURE__*/ _react.default.createElement("div", {
        ref: contentRef,
        className: "".concat(classPrefix, "-content-text")
    }, children)));
};
CollapseItem.displayName = 'NutCollapseItem';
