"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var 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.
*/
var icon_1 = require("@cocalc/frontend/components/icon");
var local_storage_1 = require("@cocalc/frontend/misc/local-storage");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var loading_1 = __importDefault(require("components/share/loading"));
var post_1 = __importDefault(require("lib/api/post"));
var config_1 = require("lib/config");
var link_1 = __importDefault(require("next/link"));
var router_1 = require("next/router");
var react_1 = require("react");
var add_box_1 = require("./add-box");
var member_idletime_1 = require("./member-idletime");
var quota_config_1 = require("./quota-config");
var reset_1 = require("./reset");
var run_limit_1 = require("./run-limit");
var site_license_cost_1 = require("./site-license-cost");
var title_description_1 = require("./title-description");
var toggle_explanations_1 = require("./toggle-explanations");
var usage_and_duration_1 = require("./usage-and-duration");
var util_1 = require("./util");
var theme_1 = require("@cocalc/util/theme");
var Text = antd_1.Typography.Text, Paragraph = antd_1.Typography.Paragraph;
function Boost() {
    var router = (0, router_1.useRouter)();
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("h3", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "rocket", style: { marginRight: "5px" } }), " ", router.query.id != null
                        ? "Edit Boost License in Shopping Cart"
                        : "Buy a Boost License"] }), router.query.id == null && ((0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical", style: { marginBottom: "20px" } }, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Typography, { children: ["A Boost License 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, __assign({ 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, __assign({ href: "./site-license" }, { children: "Site License" })), " with a \"Run Limit\" matching the number of additional of students."] })] }))), (0, jsx_runtime_1.jsx)(CreateBooster, {})] }));
}
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() {
    var _this = this;
    var _a = __read((0, react_1.useState)(undefined), 2), cost = _a[0], setCost = _a[1];
    var _b = __read((0, react_1.useState)(false), 2), loading = _b[0], setLoading = _b[1];
    var _c = __read((0, react_1.useState)(""), 2), cartError = _c[0], setCartError = _c[1];
    var _d = __read((0, react_1.useState)(true), 2), showExplanations = _d[0], setShowExplanations = _d[1];
    var _e = __read((0, react_1.useState)(null), 2), shadowMember = _e[0], setShadowMember = _e[1];
    var _f = __read(antd_1.Form.useForm(), 1), form = _f[0];
    var router = (0, router_1.useRouter)();
    // if we "edit", we don't have to check the confirmation
    var _g = __read((0, react_1.useState)(router.query.id != null), 2), confirmWarning = _g[0], setConfirmWarning = _g[1];
    var 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)(function () {
        router.prefetch("/store/cart");
    }, []);
    function onChange() {
        var conf = __assign({}, form.getFieldsValue(true));
        conf.type = "boost";
        conf.boost = true;
        setCost((0, site_license_cost_1.computeCost)(conf));
    }
    function loadItem(item) {
        return __awaiter(this, void 0, void 0, function () {
            var type;
            return __generator(this, function (_a) {
                type = (0, util_1.getType)(item);
                if (type !== "boost") {
                    throw new Error("cannot deal with type ".concat(type));
                }
                if (item.product == "site-license") {
                    form.setFieldsValue(__assign(__assign({}, item.description), { type: type }));
                }
                return [2 /*return*/];
            });
        });
    }
    (0, react_1.useEffect)(function () {
        var 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);
        }
        var store_boost_confirm = (0, local_storage_1.get_local_storage)(LS_BOOST_CONFIRM_KEY);
        if (store_boost_confirm != null) {
            setConfirmWarning(!!store_boost_confirm);
        }
        var id = router.query.id;
        if (id != null) {
            // editing something in the shopping cart
            (function () { return __awaiter(_this, void 0, void 0, function () {
                var item, err_1;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            _a.trys.push([0, 3, 4, 5]);
                            setLoading(true);
                            return [4 /*yield*/, (0, post_1.default)("/shopping/cart/get", { id: id })];
                        case 1:
                            item = _a.sent();
                            return [4 /*yield*/, loadItem(item)];
                        case 2:
                            _a.sent();
                            return [3 /*break*/, 5];
                        case 3:
                            err_1 = _a.sent();
                            setCartError(err_1.message);
                            return [3 /*break*/, 5];
                        case 4:
                            setLoading(false);
                            return [7 /*endfinally*/];
                        case 5:
                            onChange();
                            return [2 /*return*/];
                    }
                });
            }); })();
        }
        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, __assign({ 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, __assign({ 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, __assign({ 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, __assign({ strong: true }, { children: "Active Site License" })), ": one or more", " ", (0, jsx_runtime_1.jsx)("a", __assign({ 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, __assign({ 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, __assign({ href: "./dedicated?type=vm", scroll: false }, { children: "Dedicated VM" })), "."] })));
    }
    function renderConfirmation() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ wrapperCol: { offset: 2, span: 20 } }, { children: (0, jsx_runtime_1.jsxs)("div", __assign({ 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, __assign({ style: {
                                marginTop: "20px",
                                textAlign: "center",
                                fontWeight: confirmWarning ? "inherit" : "bold",
                                cursor: "pointer",
                            }, onClick: function () { return confirmBoostWarning(!confirmWarning); } }, { children: [(0, jsx_runtime_1.jsx)(antd_1.Switch, { onChange: confirmBoostWarning, checked: confirmWarning }), " ", "Yes, I understand"] })) })] })) })));
    }
    return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)(antd_1.Form, __assign({ 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", onChange: 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, __assign({ 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 }), form: form, onChange: onChange, router: router })] })) }));
}
//# sourceMappingURL=boost.js.map