"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");
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
const misc_1 = require("@cocalc/util/misc");
const theme_1 = require("@cocalc/util/theme");
const antd_1 = require("antd");
const anonymous_1 = __importDefault(require("components/misc/anonymous"));
const loading_1 = __importDefault(require("components/share/loading"));
const site_name_1 = __importDefault(require("components/share/site-name"));
const config_1 = require("lib/config");
const profile_1 = __importDefault(require("lib/hooks/profile"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
const router_1 = require("next/router");
const react_1 = require("react");
const boost_1 = __importDefault(require("./boost"));
const cart_1 = __importDefault(require("./cart"));
const checkout_1 = __importDefault(require("./checkout"));
const congrats_1 = __importDefault(require("./congrats"));
const dedicated_1 = __importDefault(require("./dedicated"));
const menu_1 = __importDefault(require("./menu"));
const overview_1 = __importDefault(require("./overview"));
const site_license_1 = __importDefault(require("./site-license"));
const store_inplace_signup_1 = require("./store-inplace-signup");
const { Content } = antd_1.Layout;
function StoreLayout({ page }) {
    const { isCommercial } = (0, use_customize_1.default)();
    const router = (0, router_1.useRouter)();
    const profile = (0, profile_1.default)({ noCache: true });
    (0, react_1.useEffect)(() => {
        router.prefetch("/store/site-license");
    }, []);
    function renderNotCommercial() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Alert, { showIcon: true, style: {
                margin: "30px auto",
                maxWidth: "400px",
                fontSize: "12pt",
                padding: "15px 30px",
            }, type: "warning", message: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["The ", (0, jsx_runtime_1.jsx)(site_name_1.default, {}), " store is not enabled."] }) }));
    }
    if (!isCommercial) {
        return renderNotCommercial();
    }
    if (!profile) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, { large: true, center: true });
    }
    const { account_id, is_anonymous } = profile;
    const noAccount = account_id == null;
    // wrapper, only the pages showing the prices will be shown to the general public or anonymous users
    function requireAccount(StorePage) {
        if (noAccount) {
            return ((0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { margin: "15px auto" }, type: "warning", message: (0, jsx_runtime_1.jsx)(store_inplace_signup_1.StoreInplaceSignInOrUp, {}) }));
        }
        return (0, jsx_runtime_1.jsx)(StorePage, {});
    }
    const [main] = page;
    function body() {
        if (main == null)
            return (0, jsx_runtime_1.jsx)(overview_1.default, {});
        if (is_anonymous) {
            return (0, jsx_runtime_1.jsx)(anonymous_1.default, {});
        }
        switch (main) {
            case "site-license":
                return (0, jsx_runtime_1.jsx)(site_license_1.default, { noAccount: noAccount });
            case "boost":
                return (0, jsx_runtime_1.jsx)(boost_1.default, { noAccount: noAccount });
            case "dedicated":
                return (0, jsx_runtime_1.jsx)(dedicated_1.default, { noAccount: noAccount });
            case "cart":
                return requireAccount(cart_1.default);
            case "checkout":
                return requireAccount(checkout_1.default);
            case "congrats":
                return requireAccount(congrats_1.default);
            default:
                (0, misc_1.unreachable)(main);
        }
    }
    // this layout is the same as ../licenses/layout.tsx and ../billing/layout.tsx
    function renderMain() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Layout, { style: {
                padding: "0 24px 24px",
                backgroundColor: "white",
                color: theme_1.COLORS.GRAY_D,
            }, children: (0, jsx_runtime_1.jsx)(Content, { style: {
                    margin: 0,
                    minHeight: "60vh",
                }, children: (0, jsx_runtime_1.jsxs)("div", { style: { maxWidth: config_1.MAX_WIDTH, margin: "auto" }, children: [(0, jsx_runtime_1.jsx)(menu_1.default, { main: main }), body()] }) }) }));
    }
    return renderMain();
}
exports.default = StoreLayout;
//# sourceMappingURL=index.js.map