"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddToCartButton = exports.AddBox = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const utils_1 = require("@cocalc/util/licenses/purchase/utils");
const antd_1 = require("antd");
const add_to_cart_1 = require("./add-to-cart");
const site_license_cost_1 = require("./site-license-cost");
function AddBox(props) {
    const { cost, router, form, cartError, setCartError, dedicatedItem = false, noAccount, } = props;
    let disabled = props.disabled ?? false;
    // if any of the fields in cost that start with the string "cost" are NaN, disable submission:
    if (!cost ||
        Object.keys(cost).some((k) => k.startsWith("cost") && isNaN(cost[k]))) {
        disabled = true;
    }
    function costPerProject() {
        if (!cost)
            return null;
        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", { style: { textAlign: "center" }, children: [router.query.id != null && ((0, jsx_runtime_1.jsx)(antd_1.Button, { size: "large", style: { marginRight: "5px" }, onClick: () => 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", { style: { textAlign: "center" }, children: (0, jsx_runtime_1.jsxs)("div", { style: {
                display: "inline-block",
                maxWidth: "400px",
                background: "white",
                border: "1px solid #ccc",
                padding: "10px 20px",
                borderRadius: "5px",
                margin: "15px 0",
                fontSize: "12pt",
            }, children: [cost && (0, jsx_runtime_1.jsx)(site_license_cost_1.DisplayCost, { cost: cost }), cost && costPerProject(), renderButton()] }) }));
}
exports.AddBox = AddBox;
function AddToCartButton(props) {
    const { cost, form, router, setCartError, cartError, variant = "primary", } = props;
    const style = variant === "primary" ? { marginTop: "5px" } : {};
    const disabled = (props.disabled ?? false) || !!cartError || cost == null || cost.cost === 0;
    return ((0, jsx_runtime_1.jsx)(antd_1.Button, { size: variant === "small" ? "small" : "large", type: "primary", htmlType: "submit", style: style, onClick: () => (0, add_to_cart_1.addToCart)({ form, setCartError, router }), disabled: disabled, children: disabled
            ? "Finish configuring the license..."
            : router.query.id != null
                ? "Save Changes"
                : "Add to Cart" }));
}
exports.AddToCartButton = AddToCartButton;
//# sourceMappingURL=add-box.js.map