"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.describePeriod = exports.describeItem = exports.DisplayCost = exports.computeCost = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var icon_1 = require("@cocalc/frontend/components/icon");
var billing_1 = require("@cocalc/util/consts/billing");
var site_license_1 = require("@cocalc/util/consts/site-license");
var describe_quota_1 = require("@cocalc/util/licenses/describe-quota");
var compute_cost_1 = require("@cocalc/util/licenses/purchase/compute-cost");
var utils_1 = require("@cocalc/util/licenses/purchase/utils");
var misc_1 = require("@cocalc/util/misc");
var timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
var dedicated_1 = require("@cocalc/util/upgrades/dedicated");
var utils_2 = require("@cocalc/util/upgrades/utils");
var timestamp_1 = __importDefault(require("components/misc/timestamp"));
function computeDedicatedDiskCost(props) {
    if (props.type !== "disk") {
        throw new Error("compute cost for disk only");
    }
    if (props.dedicated_disk == null)
        throw new Error("missing props.dedicated_disk");
    var dedicated_disk = props.dedicated_disk;
    if (props.period != "monthly")
        throw new Error("period must be monthly");
    if (dedicated_disk === false)
        throw new Error("should not happen");
    try {
        return __assign({ input: __assign(__assign({}, props), { subscription: props.period }) }, (0, compute_cost_1.compute_cost_dedicated)({
            dedicated_disk: dedicated_disk,
            subscription: props.period,
        }));
    }
    catch (err) {
        console.log("problem calculating dedicated price: ".concat(err));
    }
}
function computeDedicatedVMCost(props) {
    var _a, _b;
    if (props.type !== "vm") {
        throw new Error("compute cost for VM only");
    }
    if (props.dedicated_vm == null) {
        throw new Error("missing props.dedicated_vm");
    }
    var range = props.range, dedicated_vm = props.dedicated_vm;
    var machine = dedicated_vm.machine;
    if (range == null || range[0] == null || range[1] == null)
        return;
    var price_day = (_a = dedicated_1.PRICES.vms[machine]) === null || _a === void 0 ? void 0 : _a.price_day;
    if (price_day == null)
        return;
    var days = (0, timecalcs_1.getDays)({ start: range[0], end: range[1] });
    var price = days * price_day;
    return {
        cost: price,
        cost_per_unit: price,
        discounted_cost: price,
        cost_per_project_per_month: billing_1.AVG_MONTH_DAYS * price_day,
        cost_sub_month: billing_1.AVG_MONTH_DAYS * price_day,
        cost_sub_year: 12 * billing_1.AVG_MONTH_DAYS * price_day,
        input: __assign(__assign({}, props), { subscription: "no", start: (_b = range[0]) !== null && _b !== void 0 ? _b : new Date(), end: range === null || range === void 0 ? void 0 : range[1] }),
        period: "range",
    };
}
function computeCost(props) {
    var _a, _b;
    var type = (_a = props.type) !== null && _a !== void 0 ? _a : "quota";
    switch (type) {
        case "disk":
            return computeDedicatedDiskCost(props);
        case "vm":
            return computeDedicatedVMCost(props);
        case "quota":
        default:
            if (props.type === "disk" || props.type === "vm") {
                throw Error("must be a quota upgrade license");
            }
            var user = props.user, run_limit = props.run_limit, period = props.period, range = props.range, ram = props.ram, cpu = props.cpu, disk = props.disk, always_running = props.always_running, member = props.member, uptime = props.uptime, _c = props.boost, boost = _c === void 0 ? false : _c;
            if (period == "range" && (range === null || range === void 0 ? void 0 : range[1]) == null) {
                return undefined;
            }
            var input = {
                type: "quota",
                user: user,
                upgrade: "custom",
                quantity: run_limit,
                subscription: (period == "range" ? "no" : period),
                start: (_b = range === null || range === void 0 ? void 0 : range[0]) !== null && _b !== void 0 ? _b : new Date(),
                end: range === null || range === void 0 ? void 0 : range[1],
                custom_ram: ram,
                custom_dedicated_ram: 0,
                custom_cpu: cpu,
                custom_dedicated_cpu: 0,
                custom_disk: disk,
                custom_always_running: always_running,
                custom_member: member,
                custom_uptime: uptime,
                boost: boost,
            };
            return __assign(__assign({}, (0, compute_cost_1.compute_cost)(input)), { input: input, period: period });
    }
}
exports.computeCost = computeCost;
function DisplayCost(_a) {
    var cost = _a.cost, simple = _a.simple, oneLine = _a.oneLine;
    if (isNaN(cost.cost) || isNaN(cost.discounted_cost)) {
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "\u2013" });
    }
    var discount_pct = (0, utils_1.percent_discount)(cost);
    if (simple) {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, utils_1.money)(cost.discounted_cost), cost.period != "range" ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [oneLine ? " " : (0, jsx_runtime_1.jsx)("br", {}), cost.period] })) : (""), oneLine ? null : (0, jsx_runtime_1.jsx)("br", {}), " ", discount_pct > 0 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["(includes ", discount_pct, "% self-service discount)"] }))] }));
    }
    var desc;
    if (cost.discounted_cost < cost.cost) {
        desc = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", __assign({ style: { textDecoration: "line-through" } }, { children: (0, utils_1.money)(cost.cost) })), " or ", (0, jsx_runtime_1.jsxs)("b", { children: [(0, utils_1.money)(cost.discounted_cost), cost.input.subscription != "no" ? " " + cost.input.subscription : ""] }), ", if you purchase here (", discount_pct, "% self-service discount)."] }));
    }
    else {
        desc = "".concat((0, utils_1.money)(cost.cost), " ").concat(cost.period != "range" ? cost.period : "");
    }
    return ((0, jsx_runtime_1.jsxs)("span", { children: [describeItem(cost.input), (0, jsx_runtime_1.jsx)("hr", {}), (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "money-check" }), " Cost: ", desc] }));
}
exports.DisplayCost = DisplayCost;
function describeItem(info) {
    if (info.type === "disk") {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Dedicated Disk (", (0, utils_2.dedicatedDiskDisplay)(info.dedicated_disk), ")", " ", describePeriod(info)] }));
    }
    if (info.type === "vm") {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Dedicated VM (", (0, utils_2.dedicatedVmDisplay)(info.dedicated_vm), ")", " ", describePeriod(info)] }));
    }
    if (info.type !== "quota") {
        throw Error("at this point, we only deal with type=quota");
    }
    if (info.custom_uptime == null || info.quantity == null)
        throw new Error("should not happen");
    var _a = (0, site_license_1.untangleUptime)(info.custom_uptime), always_running = _a.always_running, idle_timeout = _a.idle_timeout;
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, describe_quota_1.describe_quota)({
                ram: info.custom_ram,
                cpu: info.custom_cpu,
                disk: info.custom_disk,
                always_running: always_running,
                idle_timeout: idle_timeout,
                member: info.custom_member,
                user: info.user,
            }), " ", describeQuantity(info), " (", describePeriod(info), ")"] }));
}
exports.describeItem = describeItem;
function describeQuantity(_a) {
    var _b = _a.quantity, quantity = _b === void 0 ? 1 : _b;
    return "for ".concat(quantity, " running ").concat((0, misc_1.plural)(quantity, "project"));
}
function describePeriod(_a) {
    var subscription = _a.subscription, start = _a.start, end = _a.end;
    if (subscription == "no") {
        if (start == null || end == null)
            throw new Error("start date not set!");
        var days = (0, timecalcs_1.getDays)({ start: start, end: end });
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(timestamp_1.default, { dateOnly: true, datetime: start, absolute: true }), " to", " ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { dateOnly: true, datetime: end, absolute: true }), ", ", days, " days"] }));
    }
    else {
        return "".concat(subscription, " subscription");
    }
}
exports.describePeriod = describePeriod;
//# sourceMappingURL=site-license-cost.js.map