import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import React, { useRef, useEffect, useCallback, useContext } from "react";
import { handleClick } from "../sidenavbar/utils";
import { OffsetContext } from "../sidenavbar/context";
var defaultProps = {
    open: true
};
export var SubSideNavBar = function(props) {
    var classPrefix = 'nut-subsidenavbar';
    var _ref = _object_spread({}, defaultProps, props), title = _ref.title, value = _ref.value, children = _ref.children, onClick = _ref.onClick, open = _ref.open, rest = _object_without_properties(_ref, [
        "title",
        "value",
        "children",
        "onClick",
        "open"
    ]);
    var offset = useContext(OffsetContext);
    var listRef = useRef(null);
    var setListLevel = useCallback(function(nodeList) {
        var level = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 1;
        var titleClass = nodeList[0].className;
        if (titleClass.includes("".concat(classPrefix, "-title"))) {
            var left = offset * (level + 1);
            nodeList[0].style.paddingLeft = "".concat(left, "px");
        }
        var childNodes = nodeList[1] && nodeList[1].children && Array.from(nodeList[1].children).filter(function(item) {
            return item.nodeType !== 3 && item.nodeType !== 8;
        });
        childNodes.forEach(function(item) {
            var itemClass = item.className;
            if (itemClass.includes("".concat(classPrefix, "-item"))) {
                var left = offset * (level + 2);
                item.style.paddingLeft = "".concat(left, "px");
            }
            if (itemClass.includes("".concat(classPrefix, "-list"))) {
                var level1 = item.getAttribute('level') ? Number(item.getAttribute('level')) : 1;
                level1 += 1;
                item.setAttribute('level', level1.toString());
                item.children && setListLevel(item.children, level1);
            }
        });
    }, [
        offset
    ]);
    var clickFn = function(e) {
        handleClick(e);
        var currentClass = e.currentTarget.className;
        var isShow = currentClass.includes('sidenavbar-show');
        onClick && onClick({
            title: title,
            value: value,
            isShow: isShow
        });
    };
    useEffect(function() {
        var _listRef_current, _listRef_current1;
        var childNodes = (_listRef_current = listRef.current) === null || _listRef_current === void 0 ? void 0 : _listRef_current.children;
        (_listRef_current1 = listRef.current) === null || _listRef_current1 === void 0 ? void 0 : _listRef_current1.setAttribute('level', '1');
        childNodes && setListLevel(childNodes);
    }, [
        setListLevel
    ]);
    var divClass = open ? "".concat(classPrefix, "-list sidenavbar-show") : "".concat(classPrefix, "-list sidenavbar-hide");
    var iconClass = open ? 'arrow-icon arrow-down' : 'arrow-icon arrow-up';
    return /*#__PURE__*/ React.createElement("div", _object_spread({
        className: divClass,
        ref: listRef,
        onClick: clickFn
    }, rest), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-title ").concat(classPrefix, "-border-bt")
    }, title, " ", /*#__PURE__*/ React.createElement("i", {
        className: iconClass
    })), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-content")
    }, children));
};
