"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddToCartButton = exports.AddBox = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var utils_1 = require("@cocalc/util/licenses/purchase/utils");
var antd_1 = require("antd");
var add_to_cart_1 = require("./add-to-cart");
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, noAccount = props.noAccount;
    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 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"] }));
    }
    function renderButton() {
        if (noAccount)
            return null;
        return ((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)(AddToCartButton, { cartError: cartError, cost: cost, disabled: disabled, form: form, router: router, setCartError: setCartError }), cartError && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: cartError })] })));
    }
    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(), renderButton()] })) })));
}
exports.AddBox = AddBox;
function AddToCartButton(props) {
    var cost = props.cost, form = props.form, router = props.router, setCartError = props.setCartError, _a = props.disabled, disabled = _a === void 0 ? false : _a, cartError = props.cartError, _b = props.variant, variant = _b === void 0 ? "primary" : _b;
    var style = variant === "primary" ? { marginTop: "5px" } : {};
    return ((0, jsx_runtime_1.jsx)(antd_1.Button, __assign({ size: variant === "small" ? "small" : "large", type: "primary", htmlType: "submit", style: style, onClick: function () { return (0, add_to_cart_1.addToCart)({ form: form, setCartError: setCartError, router: router }); }, disabled: disabled || !!cartError || cost == null || cost.cost === 0 }, { children: router.query.id != null ? "Save Changes" : "Add to Cart" })));
}
exports.AddToCartButton = AddToCartButton;
//# sourceMappingURL=add-box.js.map