"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SubSideNavBar", {
    enumerable: true,
    get: function() {
        return SubSideNavBar;
    }
});
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _utils = require("../sidenavbar/utils");
var _context = require("../sidenavbar/context");
var defaultProps = {
    open: true
};
var SubSideNavBar = function SubSideNavBar(props) {
    var classPrefix = 'nut-subsidenavbar';
    var _ref = (0, _object_spread._)({}, defaultProps, props), title = _ref.title, value = _ref.value, children = _ref.children, onClick = _ref.onClick, open = _ref.open, rest = (0, _object_without_properties._)(_ref, [
        "title",
        "value",
        "children",
        "onClick",
        "open"
    ]);
    var offset = (0, _react.useContext)(_context.OffsetContext);
    var listRef = (0, _react.useRef)(null);
    var setListLevel = (0, _react.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 clickFn(e) {
        (0, _utils.handleClick)(e);
        var currentClass = e.currentTarget.className;
        var isShow = currentClass.includes('sidenavbar-show');
        onClick && onClick({
            title: title,
            value: value,
            isShow: isShow
        });
    };
    (0, _react.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.default.createElement("div", (0, _object_spread._)({
        className: divClass,
        ref: listRef,
        onClick: clickFn
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-title ").concat(classPrefix, "-border-bt")
    }, title, " ", /*#__PURE__*/ _react.default.createElement("i", {
        className: iconClass
    })), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-content")
    }, children));
};
