"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuotaConfig = void 0;
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
 */
var icon_1 = require("@cocalc/frontend/components/icon");
var site_license_1 = require("@cocalc/util/consts/site-license");
var misc_1 = require("@cocalc/util/misc");
var consts_1 = require("@cocalc/util/upgrades/consts");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var integer_slider_1 = __importDefault(require("components/misc/integer-slider"));
var quota_config_presets_1 = require("./quota-config-presets");
var Text = antd_1.Typography.Text;
var TabPane = antd_1.Tabs.TabPane;
var EXPERT_CONFIG = "Expert configuration";
var QuotaConfig = function (props) {
    var showExplanations = props.showExplanations, form = props.form, _a = props.disabled, disabled = _a === void 0 ? false : _a, onChange = props.onChange, _b = props.boost, boost = _b === void 0 ? false : _b, configMode = props.configMode, setConfigMode = props.setConfigMode, preset = props.preset, setPreset = props.setPreset, presetAdjusted = props.presetAdjusted, setPresetAdjusted = props.setPresetAdjusted;
    function title() {
        if (boost) {
            return "Booster";
        }
        else {
            return "Quota upgrades";
        }
    }
    var PARAMS = boost ? consts_1.BOOST : consts_1.REGULAR;
    function explainRam() {
        if (!showExplanations)
            return;
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["This quota limits the total amount of memory a project can use. Note that RAM may be limited, if many other users are using the same host \u2013 though member hosting significantly reduces competition for RAM. We recommend at least 2G! Beyond the overall maximum of ", consts_1.MAX_RAM_GB, "G, we also offer", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/store/dedicated?type=vm" }, { children: "dedicated virtual machines" })), " with larger memory options."] }));
    }
    /**
     * When a quota is changed, we warn the user that the preset was adjusted. (the text updates, though, since it rerenders every time). Explanation in the details could make no sense, though – that's why this is added.
     */
    function presetWasAdjusted() {
        setPresetAdjusted === null || setPresetAdjusted === void 0 ? void 0 : setPresetAdjusted(true);
    }
    function ram() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ label: "Shared RAM", name: "ram", initialValue: PARAMS.ram.dflt, extra: explainRam() }, { children: (0, jsx_runtime_1.jsx)(integer_slider_1.default, { disabled: disabled, min: PARAMS.ram.min, max: PARAMS.ram.max, onChange: function (ram) {
                    form.setFieldsValue({ ram: ram });
                    presetWasAdjusted();
                    onChange();
                }, units: "G RAM", presets: boost ? [0, 2, 4, 8, 10] : [1, 2, 4, 8, 16] }) })));
    }
    function cpu() {
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ label: "Shared CPUs", name: "cpu", initialValue: PARAMS.cpu.dflt, extra: showExplanations ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://cloud.google.com/compute/docs/faq#virtualcpu" }, { children: "Google cloud vCPU's." })), " ", "To keep prices low, these vCPU's may be shared with other projects, though member hosting very significantly reduces competition for CPUs. We also offer", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/store/dedicated?type=vm" }, { children: "dedicated virtual machines" })), " ", "with more CPU options."] })) : undefined }, { children: (0, jsx_runtime_1.jsx)(integer_slider_1.default, { disabled: disabled, min: PARAMS.cpu.min, max: PARAMS.cpu.max, onChange: function (cpu) {
                    form.setFieldsValue({ cpu: cpu });
                    presetWasAdjusted();
                    onChange();
                }, units: "vCPU", presets: boost ? [0, 1, 2] : [1, 2, 3] }) })));
    }
    function disk() {
        // 2022-06: price increase "version 2": minimum disk we sell (also the free quota) is 3gb, not 1gb
        return ((0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ label: "Disk space", name: "disk", initialValue: PARAMS.disk.dflt, extra: showExplanations ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Extra disk space lets you store a larger number of files. Snapshots and file edit history is included at no additional charge. Each project receives at least ", consts_1.DISK_DEFAULT_GB, "G of storage space. We also offer much larger", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/store/dedicated?type=disk" }, { children: "dedicated disks" })), "."] })) : undefined }, { children: (0, jsx_runtime_1.jsx)(integer_slider_1.default, { disabled: disabled, min: PARAMS.disk.min, max: PARAMS.disk.max, onChange: function (disk) {
                    form.setFieldsValue({ disk: disk });
                    presetWasAdjusted();
                    onChange();
                }, units: "G Disk", presets: boost ? [0, 3, 6, PARAMS.disk.max] : [3, 5, 10, PARAMS.disk.max] }) })));
    }
    function infoText() {
        if (preset == null) {
            return ((0, jsx_runtime_1.jsx)(Text, __assign({ type: "danger" }, { children: "Currently, no preset selection is active. Select a preset above to reset your recent changes." })));
        }
        var cpuValue = form.getFieldValue("cpu");
        var ramValue = form.getFieldValue("ram");
        var diskValue = form.getFieldValue("disk");
        var memberValue = form.getFieldValue("member");
        var uptimeValue = form.getFieldValue("uptime");
        if (cpuValue == null ||
            ramValue == null ||
            diskValue == null ||
            memberValue == null ||
            uptimeValue == null)
            return;
        var presetData = quota_config_presets_1.PRESETS[preset];
        if (presetData == null) {
            return ((0, jsx_runtime_1.jsxs)("div", { children: ["Error: preset ", (0, jsx_runtime_1.jsx)("code", { children: preset }), " is not known."] }));
        }
        var name = presetData.name, descr = presetData.descr, details = presetData.details;
        function presetDescription() {
            if (!descr) {
                return "";
            }
            else {
                return (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [descr, ". It"] });
            }
        }
        function renderDetails() {
            if (details) {
                return details;
            }
        }
        function renderProvides() {
            var basic = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["provides up to", " ", (0, jsx_runtime_1.jsxs)(Text, __assign({ strong: true }, { children: [cpuValue, " CPU ", (0, misc_1.plural)(cpuValue, "core")] })), ", ", (0, jsx_runtime_1.jsxs)(Text, __assign({ strong: true }, { children: [ramValue, "G memory"] })), ", and", " ", (0, jsx_runtime_1.jsxs)(Text, __assign({ strong: true }, { children: [diskValue, "G disk space"] })), " for each project."] }));
            var mh = memberValue === false ? ((0, jsx_runtime_1.jsx)(Text, __assign({ strong: true }, { children: "member hosting is disabled" }))) : null;
            var ut = uptimeValue !== "short" ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [mh != null ? " and" : "", " the project's", " ", (0, jsx_runtime_1.jsxs)(Text, __assign({ strong: true }, { children: ["idle timeout is ", (0, site_license_1.displaySiteLicense)(uptimeValue)] }))] })) : null;
            var any = mh != null || ut != null;
            return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [basic, " ", any ? "Additionally, " : "", mh, ut, any ? "." : ""] }));
        }
        function presetIsAdjusted() {
            if (!presetAdjusted)
                return;
            return ((0, jsx_runtime_1.jsx)(antd_1.Typography, __assign({ style: { marginBottom: "10px" } }, { children: (0, jsx_runtime_1.jsx)(Text, __assign({ type: "warning" }, { children: "The preset has been adjusted and parts of the description might no longer be applicable. If you select another one, your modifications will be reset." })) })));
        }
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [presetIsAdjusted(), (0, jsx_runtime_1.jsxs)(antd_1.Typography, { children: ["Preset ", (0, jsx_runtime_1.jsxs)(Text, __assign({ strong: true }, { children: ["\"", name, "\""] })), " ", presetDescription(), " ", renderProvides(), " ", renderDetails()] })] }));
    }
    function presetsCommon() {
        if (!showExplanations)
            return null;
        return ((0, jsx_runtime_1.jsxs)(Text, __assign({ type: "secondary" }, { children: ["After selecting a preset, feel free to fine tune the selection in the \"", EXPERT_CONFIG, "\" or change the \"Member hosting\" or the \"Idle timeout\" configuration below. Subsequent preset selections will reset your adjustments."] })));
    }
    function presetExtra() {
        return ((0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical" }, { children: [(0, jsx_runtime_1.jsx)("div", {}), (0, jsx_runtime_1.jsx)("div", { children: infoText() }), presetsCommon()] })));
    }
    function onPresetChange(newVal) {
        var val = newVal.target.value;
        if (val == null || setPreset == null)
            return;
        setPreset(val);
        setPresetAdjusted === null || setPresetAdjusted === void 0 ? void 0 : setPresetAdjusted(false);
        var preset = quota_config_presets_1.PRESETS[val];
        if (preset != null) {
            var cpu_1 = preset.cpu, ram_1 = preset.ram, disk_1 = preset.disk, _a = preset.uptime, uptime = _a === void 0 ? "short" : _a, _b = preset.member, member = _b === void 0 ? true : _b;
            form.setFieldsValue({ uptime: uptime, member: member, cpu: cpu_1, ram: ram_1, disk: disk_1 });
        }
        onChange();
    }
    function presets() {
        return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ label: "Presets", shouldUpdate: true, extra: presetExtra() }, { children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, __assign({ onChange: onPresetChange, value: preset }, { children: (0, jsx_runtime_1.jsx)(antd_1.Space, __assign({ size: [10, 10], wrap: true }, { children: Object.keys(quota_config_presets_1.PRESETS).map(function (p) {
                            var _a;
                            var presetData = quota_config_presets_1.PRESETS[p];
                            return ((0, jsx_runtime_1.jsxs)(antd_1.Radio.Button, __assign({ value: p }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: (_a = presetData.icon) !== null && _a !== void 0 ? _a : "arrow-up" }), " ", presetData.name] }), p));
                        }) })) })) })) }));
    }
    function detailed() {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [ram(), cpu(), disk()] }));
    }
    function main() {
        if (boost) {
            return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(antd_1.Row, { children: (0, jsx_runtime_1.jsx)(antd_1.Col, __assign({ xs: 16, offset: 6, style: { marginBottom: "20px" } }, { children: (0, jsx_runtime_1.jsx)(Text, __assign({ type: "secondary" }, { children: "Configure the quotas you want to add on top of your existing license. E.g. if your license provides a limit of 2G of RAM and you add a matching boost license with 3G of RAM, you'll end up with a total quota limit of 5G of RAM." })) })) }), detailed()] }));
        }
        else {
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tabs, __assign({ activeKey: configMode, onChange: setConfigMode, type: "card", tabPosition: "top", size: "middle", centered: true }, { children: [(0, jsx_runtime_1.jsx)(TabPane, __assign({ tab: (0, jsx_runtime_1.jsxs)("span", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "lightbulb" }), "Quota presets"] }) }, { children: presets() }), "preset"), (0, jsx_runtime_1.jsx)(TabPane, __assign({ tab: (0, jsx_runtime_1.jsxs)("span", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "wrench" }), EXPERT_CONFIG] }) }, { children: detailed() }), "expert")] })));
        }
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(antd_1.Divider, __assign({ plain: true }, { children: title() })), main()] }));
};
exports.QuotaConfig = QuotaConfig;
//# sourceMappingURL=quota-config.js.map