"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addToCart = void 0;
const local_storage_1 = require("@cocalc/frontend/misc/local-storage");
const dedicated_1 = require("@cocalc/util/upgrades/dedicated");
const post_1 = __importDefault(require("lib/api/post"));
const util_1 = require("./util");
const quota_query_params_1 = require("./quota-query-params");
// this is used by the "addBox" and the thin "InfoBar" to add/modify the selected license configuration to the cart
async function addToCart(props) {
    const { form, setCartError, router } = props;
    // we make a copy, because otherwise this actually modifies the fields (user sees brief red errors)
    const description = {
        ...form.getFieldsValue(true),
    };
    // exclude extra fields that are for UI only. See https://github.com/sagemathinc/cocalc/issues/6258
    for (const field in description) {
        if (!quota_query_params_1.ALL_FIELDS.has(field)) {
            delete description[field];
        }
    }
    // unload the type parameter
    switch (description.type) {
        case "boost":
            description.boost = true;
            description.type = "quota";
            break;
        case "vm":
            for (const k of ["disk-name", "disk-size_gb", "disk-speed"]) {
                delete description[k];
            }
            const machine = description["vm-machine"];
            if (dedicated_1.PRICES.vms[machine] == null) {
                setCartError(`Unknown machine type ${machine}`);
                return;
            }
            description.dedicated_vm = {
                machine,
            };
            delete description["vm-machine"];
            description.type = "vm";
            break;
        case "disk":
            delete description["vm-machine"];
            const diskID = (0, dedicated_1.getDedicatedDiskKey)({
                size_gb: description["disk-size_gb"],
                speed: description["disk-speed"],
            });
            const disk = dedicated_1.PRICES.disks[diskID];
            if (disk == null) {
                setCartError(`Disk ${diskID} not found`);
                return;
            }
            description.dedicated_disk = {
                ...disk.quota.dedicated_disk,
                name: description["disk-name"],
            };
            for (const k of ["disk-name", "disk-size_gb", "disk-speed"]) {
                delete description[k];
            }
            description.type = "disk";
            break;
        case "regular":
        default:
            description.type = "quota";
            description.boost = false;
    }
    try {
        setCartError("");
        if (router.query.id != null) {
            await (0, post_1.default)("/shopping/cart/edit", {
                id: router.query.id,
                description,
            });
        }
        else {
            // we get the project_id from local storage and save it to the new/edited license
            const project_id = (0, local_storage_1.get_local_storage)(util_1.LS_KEY_LICENSE_PROJECT);
            (0, local_storage_1.delete_local_storage)(util_1.LS_KEY_LICENSE_PROJECT);
            await (0, post_1.default)("/shopping/cart/add", {
                product: "site-license",
                description,
                project_id,
            });
        }
        router.push("/store/cart");
    }
    catch (err) {
        setCartError(err.message);
    }
}
exports.addToCart = addToCart;
//# sourceMappingURL=add-to-cart.js.map