"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 antd_1 = require("antd");
const A_1 = __importDefault(require("components/misc/A"));
const loading_1 = __importDefault(require("components/share/loading"));
const site_name_1 = __importDefault(require("components/share/site-name"));
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 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_config_presets_1 = require("./quota-config-presets");
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 STYLE = {
    marginTop: "15px",
    maxWidth: config_1.MAX_WIDTH,
    margin: "auto",
    border: "1px solid #ddd",
    padding: "15px",
};
function SiteLicense(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: "key", style: { marginRight: "5px" } }), " ", router.query.id != null
                        ? "Edit Site License in Shopping Cart"
                        : "Buy a Quota Upgrades License"] }), router.query.id == null && ((0, jsx_runtime_1.jsxs)("p", { children: [(0, jsx_runtime_1.jsxs)(A_1.default, { href: "https://doc.cocalc.com/licenses.html", children: [(0, jsx_runtime_1.jsx)(site_name_1.default, {}), " site licenses"] }), " ", "allow you to upgrade any number of projects to run more quickly, have network access, more disk space, memory, or run on a dedicated computer. Quota upgrade licenses can be for a wide range of sizes, ranging from a single hobbyist project to thousands of simultaneous users across an entire department of school. Create a license 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.jsx)(CreateSiteLicense, { showInfoBar: scrollY > offsetHeader, noAccount: noAccount })] }));
}
exports.default = SiteLicense;
// Note -- the back and forth between moment and Date below
// is a *workaround* because of some sort of bug in moment/antd/react.
function CreateSiteLicense({ 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 [configMode, setConfigMode] = (0, react_1.useState)("preset");
    const [form] = antd_1.Form.useForm();
    const router = (0, router_1.useRouter)();
    const [preset, setPreset] = (0, react_1.useState)("standard");
    const [presetAdjusted, setPresetAdjusted] = (0, react_1.useState)(false);
    function onChange() {
        const vals = form.getFieldsValue(true);
        (0, quota_query_params_1.encodeFormValues)(router, vals, "regular");
        setCost((0, compute_cost_1.computeCost)(vals));
    }
    (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 { 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 });
                    if (item.product == "site-license") {
                        form.setFieldsValue({ ...item.description, type: "regular" });
                    }
                }
                catch (err) {
                    setCartError(err.message);
                }
                finally {
                    setLoading(false);
                }
                onChange();
            })();
        }
        else {
            const vals = (0, quota_query_params_1.decodeFormValues)(router, "regular");
            const dflt = quota_config_presets_1.PRESETS["standard"];
            if ((0, lodash_1.isEmpty)(vals)) {
                form.setFieldsValue({ ...dflt, preset: "standard" });
            }
            else {
                // we have to make sure cpu, mem and disk are set, otherwise there is no "cost"
                form.setFieldsValue({ ...dflt, ...vals });
                setConfigMode("expert");
                setPresetAdjusted(true);
            }
        }
        onChange();
    }, []);
    if (loading) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, { large: true, center: true });
    }
    const addBox = ((0, jsx_runtime_1.jsx)(add_box_1.AddBox, { cost: cost, router: router, form: form, cartError: cartError, setCartError: setCartError, noAccount: noAccount }));
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(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.jsx)(cost_info_bar_1.InfoBar, { show: showInfoBar, cost: cost, router: router, form: form, cartError: cartError, setCartError: setCartError, noAccount: noAccount }), (0, jsx_runtime_1.jsxs)(antd_1.Form, { form: form, style: STYLE, name: "basic", labelCol: { span: 6 }, wrapperCol: { span: 18 }, autoComplete: "off", onValuesChange: onChange, children: [(0, jsx_runtime_1.jsx)(antd_1.Form.Item, { wrapperCol: { offset: 0, span: 24 }, children: addBox }), (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: "regular", 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 }), (0, jsx_runtime_1.jsx)(run_limit_1.RunLimit, { showExplanations: showExplanations, form: form, onChange: onChange }), (0, jsx_runtime_1.jsx)(quota_config_1.QuotaConfig, { boost: false, form: form, onChange: onChange, showExplanations: showExplanations, configMode: configMode, setConfigMode: setConfigMode, preset: preset, setPreset: setPreset, presetAdjusted: presetAdjusted, setPresetAdjusted: setPresetAdjusted }), (0, jsx_runtime_1.jsx)(member_idletime_1.MemberHostingAndIdleTimeout, { showExplanations: showExplanations, form: form, onChange: onChange, shadowMember: shadowMember, setShadowMember: setShadowMember, setPresetAdjusted: setPresetAdjusted }), (0, jsx_runtime_1.jsx)(title_description_1.TitleDescription, { showExplanations: showExplanations }), (0, jsx_runtime_1.jsx)(reset_1.Reset, { addBox: addBox, form: form, onChange: onChange, router: router })] })] }));
}
//# sourceMappingURL=site-license.js.map