"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/* The "Account" navigation tab in the bar at the top. */
const icon_1 = require("@cocalc/frontend/components/icon");
const antd_1 = require("antd");
const avatar_1 = __importDefault(require("components/account/avatar"));
const header_1 = require("components/landing/header");
const A_1 = __importDefault(require("components/misc/A"));
const post_1 = __importDefault(require("lib/api/post"));
const base_path_1 = __importDefault(require("lib/base-path"));
const customize_1 = require("lib/customize");
const profile_1 = __importDefault(require("lib/hooks/profile"));
const router_1 = require("next/router");
const path_1 = require("path");
function makeItem(key, label, icon, children) {
    if (typeof icon === "string" && (0, icon_1.isIconName)(icon)) {
        icon = (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: icon });
    }
    return {
        key,
        icon,
        children,
        label,
    };
}
function makeGroup(key, label, children) {
    return {
        key,
        children,
        label,
        type: "group",
    };
}
const DIVIDER = {
    type: "divider",
};
function AccountNavTab({ style }) {
    const router = (0, router_1.useRouter)();
    const { isCommercial, shareServer, siteName, sshGateway } = (0, customize_1.useCustomize)();
    const profile = (0, profile_1.default)();
    if (!profile)
        return null;
    const { first_name, last_name, name, account_id, is_admin, is_anonymous } = profile;
    const profile_url = name ? `/${name}` : `/share/accounts/${account_id}`;
    const signedIn = makeItem("signed-in", (0, jsx_runtime_1.jsxs)(A_1.default, { href: is_anonymous ? "/config/search/input" : profile_url, children: ["Signed into ", siteName, " as", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)("b", { children: [first_name, " ", last_name, name ? ` (@${name})` : ""] })] }));
    const docs = makeItem("docs", (0, jsx_runtime_1.jsx)(A_1.default, { href: "https://doc.cocalc.com", external: true, children: "Documentation" }), "book");
    const configuration = makeGroup("configuration", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/config/search/input", children: (0, jsx_runtime_1.jsxs)("span", { style: { color: "#a4acb3" }, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "wrench" }), " Configuration"] }) }), [
        makeItem("account", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/config/account/name", children: "Account" }), "user"),
        makeItem("editor", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/config/editor/appearance", children: "Editor" }), "edit"),
        makeItem("system", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/config/system/appearance", children: "System" }), "gear"),
    ]);
    function profileItems() {
        if (!profile)
            return [];
        const ret = [];
        ret.push(signedIn);
        if (is_anonymous) {
            ret.push(makeItem("sign-up", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/config/search/input", children: (0, jsx_runtime_1.jsx)("b", { children: "Sign Up (save your work)!" }) }), "user"));
        }
        ret.push(docs);
        if (isCommercial) {
            ret.push(makeItem("store", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/store", children: "Store" }), "shopping-cart"));
        }
        ret.push(DIVIDER);
        ret.push(configuration);
        ret.push(DIVIDER);
        return ret;
    }
    function yourPages() {
        const yours = [];
        yours.push(makeItem("projects", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, path_1.join)(base_path_1.default, "projects"), external: true, children: is_anonymous ? "Project" : "Projects" }), "edit"));
        if (!is_anonymous) {
            yours.push(makeItem("licenses", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/licenses", children: "Licenses" }), "key"));
            if (isCommercial) {
                yours.push(makeItem("billing", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/billing", children: "Billing" }), "credit-card"));
            }
            if (sshGateway) {
                yours.push(makeItem("ssh", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, path_1.join)(base_path_1.default, "settings", "ssh-keys"), external: true, children: "SSH keys" }), "key"));
            }
            if (shareServer) {
                yours.push(makeItem("shared", (0, jsx_runtime_1.jsx)(A_1.default, { href: profile?.name ? `/${name}` : `/share/accounts/${account_id}`, external: true, children: "Shared Files" }), "bullhorn"));
                yours.push(makeItem("stars", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/stars", children: "Stars" }), "star-filled"));
            }
        }
        return [
            makeGroup("your", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, path_1.join)(base_path_1.default, "app"), external: true, children: (0, jsx_runtime_1.jsxs)("span", { style: { color: "#a4acb3" }, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "user" }), " Your..."] }) }), yours),
        ];
    }
    function admin() {
        if (!is_admin)
            return [];
        return [
            DIVIDER,
            makeItem("admin", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, path_1.join)(base_path_1.default, "admin"), external: true, children: "Site Administration" }), "settings")
        ];
    }
    const signout = [
        DIVIDER,
        makeItem("sign-out", (0, jsx_runtime_1.jsx)(A_1.default, { onClick: async () => {
                await (0, post_1.default)("/accounts/sign-out", { all: false });
                router.push("/");
            }, children: "Sign Out" })),
    ];
    const items = [
        ...profileItems(),
        ...yourPages(),
        ...admin(),
        ...signout,
    ];
    const menu = (0, jsx_runtime_1.jsx)(antd_1.Menu, { mode: "vertical", theme: "dark", items: items });
    return ((0, jsx_runtime_1.jsx)(antd_1.Dropdown, { overlay: menu, trigger: "click", children: (0, jsx_runtime_1.jsxs)("div", { style: { ...header_1.LinkStyle, cursor: "pointer", ...style }, children: [account_id && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(avatar_1.default, { account_id: account_id, style: { margin: "-10px 0" } }), "\u00A0\u00A0"] })), "Account ", (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "angle-down" })] }) }));
}
exports.default = AccountNavTab;
//# sourceMappingURL=navtab.js.map