"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
 */
/*
Create a new site license.
*/
const icon_1 = require("@cocalc/frontend/components/icon");
const local_storage_1 = require("@cocalc/frontend/misc/local-storage");
const theme_1 = require("@cocalc/util/theme");
const antd_1 = require("antd");
const A_1 = __importDefault(require("components/misc/A"));
const loading_1 = __importDefault(require("components/share/loading"));
const post_1 = __importDefault(require("lib/api/post"));
const config_1 = require("lib/config");
const use_scroll_y_1 = require("lib/use-scroll-y");
const lodash_1 = require("lodash");
const link_1 = __importDefault(require("next/link"));
const router_1 = require("next/router");
const react_1 = require("react");
const add_box_1 = require("./add-box");
const apply_license_to_project_1 = require("./apply-license-to-project");
const compute_cost_1 = require("./compute-cost");
const cost_info_bar_1 = require("./cost-info-bar");
const member_idletime_1 = require("./member-idletime");
const quota_config_1 = require("./quota-config");
const quota_query_params_1 = require("./quota-query-params");
const reset_1 = require("./reset");
const run_limit_1 = require("./run-limit");
const sign_in_to_purchase_1 = require("./sign-in-to-purchase");
const title_description_1 = require("./title-description");
const toggle_explanations_1 = require("./toggle-explanations");
const usage_and_duration_1 = require("./usage-and-duration");
const util_1 = require("./util");
const { Text, Paragraph } = antd_1.Typography;
function Boost(props) {
    const { noAccount } = props;
    const router = (0, router_1.useRouter)();
    const headerRef = (0, react_1.useRef)(null);
    // most likely, user will go to the cart next
    (0, react_1.useEffect)(() => {
        router.prefetch("/store/cart");
    }, []);
    const [offsetHeader, setOffsetHeader] = (0, react_1.useState)(0);
    const scrollY = (0, use_scroll_y_1.useScrollY)();
    (0, react_1.useEffect)(() => {
        if (headerRef.current) {
            setOffsetHeader(headerRef.current.offsetTop);
        }
    }, []);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("h3", { ref: headerRef, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "rocket", style: { marginRight: "5px" } }), " ", router.query.id != null
                        ? "Edit License Booster in Shopping Cart"
                        : "Buy a License Booster"] }), router.query.id == null && ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { marginBottom: "20px" }, children: [(0, jsx_runtime_1.jsxs)(antd_1.Typography, { children: ["A License Booster adds additional quotas to an already existing, valid and currently active regular Site License. A common use case is to increase the memory limit after you already bought a Site License. Create a boost using the form below then add it to your", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/store/cart", children: "shopping cart" }), "."] }), (0, jsx_runtime_1.jsxs)(antd_1.Typography, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "lightbulb", style: { color: theme_1.COLORS.ANTD_ORANGE } }), " If you are teaching a course and have to cover more students, you need to get an additional ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "./site-license", children: "Site License" }), " with a \"Run Limit\" matching the number of additional of students."] })] })), (0, jsx_runtime_1.jsx)(CreateBooster, { showInfoBar: scrollY > offsetHeader, noAccount: noAccount })] }));
}
exports.default = Boost;
// Note -- the back and forth between moment and Date below
// is a *workaround* because of some sort of bug in moment/antd/react.
function CreateBooster({ showInfoBar = false, noAccount = false }) {
    const [cost, setCost] = (0, react_1.useState)(undefined);
    const [loading, setLoading] = (0, react_1.useState)(false);
    const [cartError, setCartError] = (0, react_1.useState)("");
    const [showExplanations, setShowExplanations] = (0, react_1.useState)(true);
    const [shadowMember, setShadowMember] = (0, react_1.useState)(null);
    const [form] = antd_1.Form.useForm();
    const router = (0, router_1.useRouter)();
    // if we "edit", we don't have to check the confirmation
    const [confirmWarning, setConfirmWarning] = (0, react_1.useState)(router.query.id != null);
    const LS_BOOST_CONFIRM_KEY = "store_boost_confirm";
    function confirmBoostWarning(confirmed) {
        setConfirmWarning(confirmed);
        (0, local_storage_1.set_local_storage)(LS_BOOST_CONFIRM_KEY, confirmed ? "t" : "");
    }
    // most likely, user will go to the cart next
    (0, react_1.useEffect)(() => {
        router.prefetch("/store/cart");
    }, []);
    function onChange() {
        const vals = form.getFieldsValue(true);
        (0, quota_query_params_1.encodeFormValues)(router, vals, "boost");
        const conf = { ...vals };
        conf.type = "boost";
        conf.boost = true;
        setCost((0, compute_cost_1.computeCost)(conf));
    }
    async function loadItem(item) {
        const type = (0, util_1.getType)(item);
        if (type !== "boost") {
            throw new Error(`cannot deal with type ${type}`);
        }
        if (item.product == "site-license") {
            form.setFieldsValue({ ...item.description, type });
        }
    }
    (0, react_1.useEffect)(() => {
        const store_site_license_show_explanations = (0, local_storage_1.get_local_storage)("store_site_license_show_explanations");
        if (store_site_license_show_explanations != null) {
            setShowExplanations(!!store_site_license_show_explanations);
        }
        const store_boost_confirm = (0, local_storage_1.get_local_storage)(LS_BOOST_CONFIRM_KEY);
        if (store_boost_confirm != null) {
            setConfirmWarning(!!store_boost_confirm);
        }
        const { id } = router.query;
        if (!noAccount && id != null) {
            // editing something in the shopping cart
            (async () => {
                try {
                    setLoading(true);
                    const item = await (0, post_1.default)("/shopping/cart/get", { id });
                    await loadItem(item);
                }
                catch (err) {
                    setCartError(err.message);
                }
                finally {
                    setLoading(false);
                }
                onChange();
            })();
        }
        else {
            const vals = (0, quota_query_params_1.decodeFormValues)(router, "boost");
            if (!(0, lodash_1.isEmpty)(vals)) {
                form.setFieldsValue(vals);
            }
        }
        onChange();
    }, []);
    if (loading) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, { large: true, center: true });
    }
    function renderConfirmationText() {
        return ((0, jsx_runtime_1.jsxs)(Paragraph, { ellipsis: confirmWarning ? { rows: 3, expandable: false, symbol: null } : false, style: {
                opacity: confirmWarning ? 0.75 : 1,
            }, children: ["Boost licenses only work in combination with regular", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "./site-license", children: "Site Licenses" }), ". The intention of a Boost License is to increase how much resources your project receives, without having to purchase yet another regular license. For example, you can increase just the RAM for some projects for a couple of days, while otherwise you are happy with a smaller license as part of an ongoing subscription. To cover more projects, e.g. additional students in a course, you need to get a regular", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "./site-license", children: "Site License" }), ".", (0, jsx_runtime_1.jsx)("p", { children: "The following conditions must be met in order to benefit from an activated boost license:" }), (0, jsx_runtime_1.jsxs)("ul", { children: [(0, jsx_runtime_1.jsxs)("li", { children: [(0, jsx_runtime_1.jsx)(Text, { strong: true, children: "Active Site License" }), ": one or more", " ", (0, jsx_runtime_1.jsx)("a", { href: "/store/site-license", children: "regular Site License(s)" }), " must be applied to the project and actively providing upgrades. This is evaluated each time a project starts. Boosts are only adding more resources on top of what a regular license already provides!"] }), (0, jsx_runtime_1.jsxs)("li", { children: [(0, jsx_runtime_1.jsx)(Text, { strong: true, children: "Matching Configuration" }), ": the type of hosting quality (\"Member Hosting\") and \"Idle Timeout\" duration must be the same. A booster only works for a site license with a matching upgrade quality."] })] }), "Besides that \u2013 just like a regular license \u2013 you can't exceed the run limit; the boost license must be valid as well, and combining all upgrades and boosts together, you cannot exceed the overall upgrade limits. If you need vastly more resources, consider purchasing a", " ", (0, jsx_runtime_1.jsx)(link_1.default, { href: "./dedicated?type=vm", scroll: false, children: "Dedicated VM" }), "."] }));
    }
    function renderConfirmation() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, { wrapperCol: { offset: 2, span: 20 }, children: (0, jsx_runtime_1.jsxs)("div", { style: {
                    border: confirmWarning ? "1px solid gray" : "3px solid red",
                    borderRadius: "5px",
                    padding: "10px",
                    margin: confirmWarning ? "2px" : 0, // compensate border with from above
                }, children: [renderConfirmationText(), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)(Paragraph, { style: {
                                marginTop: "20px",
                                textAlign: "center",
                                fontWeight: confirmWarning ? "inherit" : "bold",
                                cursor: "pointer",
                            }, onClick: () => confirmBoostWarning(!confirmWarning), children: [(0, jsx_runtime_1.jsx)(antd_1.Switch, { onChange: confirmBoostWarning, checked: confirmWarning }), " ", "Yes, I understand"] }) })] }) }));
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(cost_info_bar_1.InfoBar, { show: showInfoBar, cost: cost, router: router, form: form, cartError: cartError, setCartError: setCartError, noAccount: noAccount }), (0, jsx_runtime_1.jsx)(apply_license_to_project_1.ApplyLicenseToProject, { router: router }), (0, jsx_runtime_1.jsx)(sign_in_to_purchase_1.SignInToPurchase, { noAccount: noAccount }), (0, jsx_runtime_1.jsxs)(antd_1.Form, { form: form, style: {
                    marginTop: "15px",
                    maxWidth: config_1.MAX_WIDTH,
                    margin: "auto",
                    border: "1px solid #ddd",
                    padding: "15px",
                }, name: "basic", labelCol: { span: 6 }, wrapperCol: { span: 18 }, autoComplete: "off", onValuesChange: onChange, children: [renderConfirmation(), (0, jsx_runtime_1.jsx)(toggle_explanations_1.ToggleExplanations, { showExplanations: showExplanations, setShowExplanations: setShowExplanations }), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, { name: "type", initialValue: "boost", noStyle: true, children: (0, jsx_runtime_1.jsx)(antd_1.Input, { type: "hidden" }) }), (0, jsx_runtime_1.jsx)(usage_and_duration_1.UsageAndDuration, { showExplanations: showExplanations, form: form, onChange: onChange, disabled: !confirmWarning }), (0, jsx_runtime_1.jsx)(member_idletime_1.MemberHostingAndIdleTimeout, { showExplanations: showExplanations, form: form, onChange: onChange, shadowMember: shadowMember, setShadowMember: setShadowMember, boost: true, disabled: !confirmWarning }), (0, jsx_runtime_1.jsx)(run_limit_1.RunLimit, { showExplanations: showExplanations, form: form, onChange: onChange, boost: true, disabled: !confirmWarning }), (0, jsx_runtime_1.jsx)(quota_config_1.QuotaConfig, { boost: true, form: form, onChange: onChange, disabled: !confirmWarning, showExplanations: showExplanations }), (0, jsx_runtime_1.jsx)(title_description_1.TitleDescription, { showExplanations: showExplanations, disabled: !confirmWarning }), (0, jsx_runtime_1.jsx)(reset_1.Reset, { addBox: (0, jsx_runtime_1.jsx)(add_box_1.AddBox, { cost: cost, form: form, cartError: cartError, setCartError: setCartError, router: router, noAccount: noAccount }), form: form, onChange: onChange, router: router })] })] }));
}
//# sourceMappingURL=boost.js.map