"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 __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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddBox = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var utils_1 = require("@cocalc/util/licenses/purchase/utils");
var dedicated_1 = require("@cocalc/util/upgrades/dedicated");
var antd_1 = require("antd");
var post_1 = __importDefault(require("lib/api/post"));
var site_license_cost_1 = require("./site-license-cost");
function AddBox(props) {
    var cost = props.cost, router = props.router, form = props.form, cartError = props.cartError, setCartError = props.setCartError, _a = props.dedicatedItem, dedicatedItem = _a === void 0 ? false : _a, _b = props.disabled, disabled = _b === void 0 ? false : _b;
    if (!cost)
        return null;
    // if any of the fields in cost that start with the string "cost" are NaN, return null
    if (Object.keys(cost).some(function (k) { return k.startsWith("cost") && isNaN(cost[k]); })) {
        return null;
    }
    function addToCart() {
        return __awaiter(this, void 0, void 0, function () {
            var description, _a, _b, k, machine, diskID, disk, _c, _d, k, err_1;
            var e_1, _e, e_2, _f;
            return __generator(this, function (_g) {
                switch (_g.label) {
                    case 0:
                        description = __assign({}, form.getFieldsValue(true));
                        // unload the type parameter
                        switch (description.type) {
                            case "boost":
                                description.boost = true;
                                description.type = "quota";
                                break;
                            case "vm":
                                try {
                                    for (_a = __values(["disk-name", "disk-size_gb", "disk-speed"]), _b = _a.next(); !_b.done; _b = _a.next()) {
                                        k = _b.value;
                                        delete description[k];
                                    }
                                }
                                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                                finally {
                                    try {
                                        if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
                                    }
                                    finally { if (e_1) throw e_1.error; }
                                }
                                machine = description["vm-machine"];
                                if (dedicated_1.PRICES.vms[machine] == null) {
                                    setCartError("Unknown machine type ".concat(machine));
                                    return [2 /*return*/];
                                }
                                description.dedicated_vm = {
                                    machine: machine,
                                };
                                delete description["vm-machine"];
                                description.type = "vm";
                                break;
                            case "disk":
                                delete description["vm-machine"];
                                diskID = (0, dedicated_1.getDedicatedDiskKey)({
                                    size_gb: description["disk-size_gb"],
                                    speed: description["disk-speed"],
                                });
                                disk = dedicated_1.PRICES.disks[diskID];
                                if (disk == null) {
                                    setCartError("Disk ".concat(diskID, " not found"));
                                    return [2 /*return*/];
                                }
                                description.dedicated_disk = __assign(__assign({}, disk.quota.dedicated_disk), { name: description["disk-name"] });
                                try {
                                    for (_c = __values(["disk-name", "disk-size_gb", "disk-speed"]), _d = _c.next(); !_d.done; _d = _c.next()) {
                                        k = _d.value;
                                        delete description[k];
                                    }
                                }
                                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                                finally {
                                    try {
                                        if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
                                    }
                                    finally { if (e_2) throw e_2.error; }
                                }
                                description.type = "disk";
                                break;
                            case "regular":
                            default:
                                description.type = "quota";
                                description.boost = false;
                        }
                        _g.label = 1;
                    case 1:
                        _g.trys.push([1, 6, , 7]);
                        setCartError("");
                        if (!(router.query.id != null)) return [3 /*break*/, 3];
                        return [4 /*yield*/, (0, post_1.default)("/shopping/cart/edit", {
                                id: router.query.id,
                                description: description,
                            })];
                    case 2:
                        _g.sent();
                        return [3 /*break*/, 5];
                    case 3: return [4 /*yield*/, (0, post_1.default)("/shopping/cart/add", {
                            product: "site-license",
                            description: description,
                        })];
                    case 4:
                        _g.sent();
                        _g.label = 5;
                    case 5:
                        router.push("/store/cart");
                        return [3 /*break*/, 7];
                    case 6:
                        err_1 = _g.sent();
                        setCartError(err_1.message);
                        return [3 /*break*/, 7];
                    case 7: return [2 /*return*/];
                }
            });
        });
    }
    function costPerProject() {
        if (!cost)
            throw new Error("cost is undefined, should not happen.");
        if (!["quota", "regular", "boost"].includes(cost.input.type)) {
            return;
        }
        if (dedicatedItem || cost.input.quantity == null)
            return;
        return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, utils_1.money)(cost.discounted_cost / cost.input.quantity), " per project"] }));
    }
    return ((0, jsx_runtime_1.jsx)("div", __assign({ style: { textAlign: "center" } }, { children: (0, jsx_runtime_1.jsxs)("div", __assign({ style: {
                display: "inline-block",
                maxWidth: "400px",
                background: "white",
                border: "1px solid #ccc",
                padding: "10px 20px",
                borderRadius: "5px",
                margin: "15px 0",
                fontSize: "12pt",
            } }, { children: [(0, jsx_runtime_1.jsx)(site_license_cost_1.DisplayCost, { cost: cost }), costPerProject(), (0, jsx_runtime_1.jsxs)("div", __assign({ style: { textAlign: "center" } }, { children: [router.query.id != null && ((0, jsx_runtime_1.jsx)(antd_1.Button, __assign({ size: "large", style: { marginRight: "5px" }, onClick: function () { return router.push("/store/cart"); }, disabled: disabled }, { children: "Cancel" }))), (0, jsx_runtime_1.jsx)(antd_1.Button, __assign({ size: "large", type: "primary", htmlType: "submit", style: { marginTop: "5px" }, onClick: function () { return addToCart(); }, disabled: !!cartError || cost.cost === 0 || disabled }, { children: router.query.id != null ? "Save Changes" : "Add to Cart" })), cartError && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: cartError })] }))] })) })));
}
exports.AddBox = AddBox;
//# sourceMappingURL=add-box.js.map