"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const antd_1 = require("antd");
const icon_1 = require("@cocalc/frontend/components/icon");
const router_1 = require("next/router");
const react_1 = require("react");
const register_1 = require("./register");
const misc_1 = require("@cocalc/util/misc");
const { SubMenu } = antd_1.Menu;
function menuBody() {
    const v = [];
    for (const main in register_1.menu) {
        const title = (0, misc_1.capitalize)(main);
        const icon = register_1.topIcons[main] ?? "gear";
        const w = [];
        for (const sub in register_1.menu[main]) {
            const { title, icon, danger } = register_1.menu[main][sub];
            w.push((0, jsx_runtime_1.jsxs)(antd_1.Menu.Item, { danger: danger, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: icon, style: { marginRight: "5px" } }), " ", title] }, main + "/" + sub));
        }
        v.push((0, jsx_runtime_1.jsx)(SubMenu, { icon: (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: icon }), title: title, children: w }, main));
    }
    return v;
}
function ConfigMenu({ main, sub }) {
    const router = (0, router_1.useRouter)();
    const [openKeys, setOpenKeys] = (0, react_1.useState)([main]);
    // This useEffect is to ensure that the selected section (main)
    // is always expanded, e.g., when you get there by clicking on
    // a search result:
    (0, react_1.useEffect)(() => {
        if (openKeys.indexOf(main) == -1) {
            setOpenKeys(openKeys.concat([main]));
        }
    }, [main]);
    return ((0, jsx_runtime_1.jsx)(antd_1.Menu, { mode: "inline", openKeys: openKeys, onOpenChange: (keys) => {
            setOpenKeys(keys);
        }, selectedKeys: [main + "/" + sub], style: { height: "100%" }, onSelect: (e) => {
            router.push(`/config/${e.keyPath[0]}`, undefined, {
                scroll: false,
            });
        }, children: menuBody() }));
}
exports.default = ConfigMenu;
//# sourceMappingURL=menu.js.map