"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
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.computeCost = exports.computeDedicatedVMCost = exports.computeDedicatedDiskCost = void 0;
var billing_1 = require("@cocalc/util/consts/billing");
var compute_cost_1 = require("@cocalc/util/licenses/purchase/compute-cost");
var timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
var dedicated_1 = require("@cocalc/util/upgrades/dedicated");
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));
    }
}
exports.computeDedicatedDiskCost = computeDedicatedDiskCost;
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",
    };
}
exports.computeDedicatedVMCost = computeDedicatedVMCost;
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;
//# sourceMappingURL=compute-cost.js.map