"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 __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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 });
exports.MemberHostingAndIdleTimeout = 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 site_license_1 = require("@cocalc/util/consts/site-license");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var Text = antd_1.Typography.Text;
function MemberHostingAndIdleTimeout(_a) {
    var shadowMember = _a.shadowMember, setShadowMember = _a.setShadowMember, form = _a.form, showExplanations = _a.showExplanations, onChange = _a.onChange, _b = _a.boost, boost = _b === void 0 ? false : _b, _c = _a.disabled, disabled = _c === void 0 ? false : _c;
    function memberExplanation() {
        if (!showExplanations)
            return;
        if (boost) {
            return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "The state of Member Hosting must match the corresponding Site License you want to boost." }));
        }
        else {
            return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Member hosting significanlty reduces competition for resources, and we prioritize", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "support/new", external: true }, { children: "support requests" })), " ", "much higher. All licensed projects, with or without member hosting, have network access, so they can connect to the network to clone Git repositories, download data files and install software.", (0, site_license_1.requiresMemberhosting)(form.getFieldValue("uptime")) && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Text, __assign({ italic: true, type: "secondary" }, { children: "Note: this level of idle timeout requires member hosting." }))] })), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Text, __assign({ italic: true, type: "secondary" }, { children: "Please be aware: licenses of different member hosting service levels cannot be combined!" }))] }));
        }
    }
    function idleTimeoutExplanation() {
        if (!showExplanations)
            return;
        if (boost) {
            return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "The Idle timeout of this Boost license must match the corresponding Site License you want to boost." }));
        }
        var uptime = form.getFieldValue("uptime");
        var bottom = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(Text, __assign({ italic: true, type: "secondary" }, { children: "Please be aware: licenses with different idle timeouts cannot be combined!" }))] }));
        var main = (function () {
            if (uptime === "always_running") {
                return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Text, __assign({ strong: true, type: "secondary" }, { children: "Keep projects running:" })), " ", "Once started your project stays running, so you can run very long computations and also never have to wait for your project to start. This effectively disables", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/software-development.html#idle-timeout" }, { children: "idle timeout" })), ", since your project will restart automatically if it stops. See", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/project-init.html" }, { children: "project init scripts" })), ". (Note: this is NOT guaranteed 100% uptime, since projects may sometimes restart for security and maintenance reasons.)"] }));
            }
            else {
                return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Projects stop automatically if they are not actively used. Increasing", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/software-development.html#idle-timeout" }, { children: "idle timeout" })), " ", "will allow you to run longer calculations without you having to be active while they run. However, this is not 100% guaranteed, because projects may still restart due to maintenance or security reasons."] }));
            }
        })();
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [main, bottom] }));
    }
    function uptimeOptions() {
        var e_1, _a;
        var ret = [];
        try {
            for (var _b = __values(Object.entries(site_license_1.LicenseIdleTimeouts)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var _d = __read(_c.value, 2), key = _d[0], it = _d[1];
                ret.push((0, jsx_runtime_1.jsx)(antd_1.Radio.Button, __assign({ value: key }, { children: it.label }), key));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        ret.push((0, jsx_runtime_1.jsx)(antd_1.Radio.Button, __assign({ value: "always_running" }, { children: "Always running" }), "always_running"));
        return ret;
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Divider, __assign({ plain: true }, { children: [boost ? "Matching" : "", " Hosting quality and Idle timeout"] })), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ initialValue: true, label: "Member hosting", name: "member", valuePropName: "checked", dependencies: ["uptime"], rules: [
                    function (_a) {
                        var getFieldValue = _a.getFieldValue, setFieldsValue = _a.setFieldsValue;
                        return ({
                            validator: function (_, value) {
                                // we force member true if the uptime is higher than medium
                                var uptime = getFieldValue("uptime");
                                if ((0, site_license_1.requiresMemberhosting)(uptime)) {
                                    if (value !== true) {
                                        setShadowMember(value);
                                        setFieldsValue({ member: true });
                                    }
                                }
                                else {
                                    // if the user toggles back to a lower idle timeout,
                                    // we use shadowMember to restore the previous member value.
                                    if (shadowMember != null) {
                                        setFieldsValue({ member: shadowMember });
                                        setShadowMember(null);
                                    }
                                }
                            },
                        });
                    },
                ], extra: memberExplanation() }, { children: (0, jsx_runtime_1.jsx)(antd_1.Checkbox, __assign({ disabled: disabled || (0, site_license_1.requiresMemberhosting)(form.getFieldValue("uptime")) }, { children: "Run project on a much better host with network access" })) })), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, __assign({ initialValue: "short", name: "uptime", label: "Idle timeout", extra: idleTimeoutExplanation() }, { children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, __assign({ disabled: disabled, onChange: function (e) {
                        form.setFieldsValue({ uptime: e.target.value });
                        onChange();
                    } }, { children: uptimeOptions() })) }))] }));
}
exports.MemberHostingAndIdleTimeout = MemberHostingAndIdleTimeout;
//# sourceMappingURL=member-idletime.js.map