"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 = 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 site_license_1 = require("@cocalc/util/consts/site-license");
var describe_quota_1 = require("@cocalc/util/licenses/describe-quota");
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 utils_2 = require("@cocalc/util/upgrades/utils");
var timestamp_1 = __importDefault(require("components/misc/timestamp"));
var util_1 = require("./util");
var timecalcs_2 = require("@cocalc/util/stripe/timecalcs");
function DisplayCost(props) {
    var cost = props.cost, simple = props.simple, oneLine = props.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) {
    var _a;
    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.quantity == null) {
        throw new Error("should not happen");
    }
    var _b = (0, site_license_1.untangleUptime)((_a = info.custom_uptime) !== null && _a !== void 0 ? _a : "short"), always_running = _b.always_running, idle_timeout = _b.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"));
}
/**
 * ATTN: this is not a general purpose period description generator. It's very specific to the purchases in the store!
 */
function describePeriod(props) {
    var subscription = props.subscription, startRaw = props.start, endRaw = props.end;
    var _a = (0, util_1.useTimeFixer)(), fromServerTime = _a.fromServerTime, serverTimeDate = _a.serverTimeDate;
    if (subscription == "no") {
        if (startRaw == null || endRaw == null)
            throw new Error("start date not set!");
        // we do not use startOfDay and endOfDay, because this was already
        // done in "usage-and-duration::fixRangeSelector"
        // rather, we calculate back to the user's offset
        var start = (0, timecalcs_2.roundToMidnight)(fromServerTime(startRaw), "start");
        var end = (0, timecalcs_2.roundToMidnight)(fromServerTime(endRaw), "end");
        if (start == null || end == null) {
            throw new Error("this should never happen");
        }
        // days are calculated based on the actual selection
        var days = (0, timecalcs_1.getDays)({ start: start, end: end });
        // but the displayed end mimics what will happen later on the backend
        // i.e. if the day alreaday started, we append the already elapsed period to the end
        var endDisplay = (0, timecalcs_1.appendAfterNowToDate)({
            now: serverTimeDate,
            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: endDisplay, absolute: true }), ", ", days, " ", (0, misc_1.plural)(days, "day")] }));
    }
    else {
        return "".concat(subscription, " subscription");
    }
}
exports.describePeriod = describePeriod;
//# sourceMappingURL=site-license-cost.js.map