"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
var antd_1 = require("antd");
var icon_1 = require("@cocalc/frontend/components/icon");
var router_1 = require("next/router");
var react_1 = require("react");
var register_1 = require("./register");
var misc_1 = require("@cocalc/util/misc");
var SubMenu = antd_1.Menu.SubMenu;
function menuBody() {
    var _a;
    var v = [];
    for (var main in register_1.menu) {
        var title = (0, misc_1.capitalize)(main);
        var icon = (_a = register_1.topIcons[main]) !== null && _a !== void 0 ? _a : "gear";
        var w = [];
        for (var sub in register_1.menu[main]) {
            var _b = register_1.menu[main][sub], title_1 = _b.title, icon_2 = _b.icon, danger = _b.danger;
            w.push((0, jsx_runtime_1.jsxs)(antd_1.Menu.Item, __assign({ danger: danger }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: icon_2, style: { marginRight: "5px" } }), " ", title_1] }), main + "/" + sub));
        }
        v.push((0, jsx_runtime_1.jsx)(SubMenu, __assign({ icon: (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: icon }), title: title }, { children: w }), main));
    }
    return v;
}
function ConfigMenu(_a) {
    var main = _a.main, sub = _a.sub;
    var router = (0, router_1.useRouter)();
    var _b = __read((0, react_1.useState)([main]), 2), openKeys = _b[0], setOpenKeys = _b[1];
    // 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)(function () {
        if (openKeys.indexOf(main) == -1) {
            setOpenKeys(openKeys.concat([main]));
        }
    }, [main]);
    return ((0, jsx_runtime_1.jsx)(antd_1.Menu, __assign({ mode: "inline", openKeys: openKeys, onOpenChange: function (keys) {
            setOpenKeys(keys);
        }, selectedKeys: [main + "/" + sub], style: { height: "100%" }, onSelect: function (e) {
            router.push("/config/".concat(e.keyPath[0]), undefined, {
                scroll: false,
            });
        } }, { children: menuBody() })));
}
exports.default = ConfigMenu;
//# sourceMappingURL=menu.js.map