"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 = __importStar(require("components/misc/timestamp"));
var util_1 = require("./util");
var antd_1 = require("antd");
var Text = antd_1.Typography.Text;
function DisplayCost(props) {
    var cost = props.cost, _a = props.simple, simple = _a === void 0 ? false : _a, _b = props.oneLine, oneLine = _b === void 0 ? false : _b, _c = props.simpleShowPeriod, simpleShowPeriod = _c === void 0 ? true : _c, _d = props.discountTooltip, discountTooltip = _d === void 0 ? false : _d;
    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) {
        var discount = discount_pct > 0 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["includes ", discount_pct, "% self-service discount"] }));
        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", {}), simpleShowPeriod && cost.period] })) : (""), oneLine ? null : (0, jsx_runtime_1.jsx)("br", {}), " ", discount && !discountTooltip && 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({ info: cost.input }), (0, jsx_runtime_1.jsx)("hr", {}), (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "money-check" }), " Cost: ", desc] }));
}
exports.DisplayCost = DisplayCost;
// TODO: this should be a component. Rename it to DescribeItem and use it
// properly, e.g., <DescribeItem info={cost.input}/> above.
function describeItem(props) {
    var _a;
    var info = props.info, _b = props.variant, variant = _b === void 0 ? "long" : _b;
    if (info.type === "disk") {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Dedicated Disk (", (0, utils_2.dedicatedDiskDisplay)(info.dedicated_disk, variant), ")", " ", describePeriod({ quota: info, variant: variant })] }));
    }
    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({ quota: info, variant: variant })] }));
    }
    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 _c = (0, site_license_1.untangleUptime)((_a = info.custom_uptime) !== null && _a !== void 0 ? _a : "short"), always_running = _c.always_running, idle_timeout = _c.idle_timeout;
    var 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,
    };
    if (variant === "short") {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Text, __assign({ strong: true }, { children: describeQuantity({ quota: info, variant: variant }) })), " ", (0, describe_quota_1.describeQuotaOnLine)(quota), ", ", describePeriod({ quota: info, variant: variant })] }));
    }
    else {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, describe_quota_1.describe_quota)(quota, false), " ", describeQuantity({ quota: info, variant: variant }), " (", describePeriod({ quota: info, variant: variant }), ")"] }));
    }
}
exports.describeItem = describeItem;
function describeQuantity(props) {
    var info = props.quota, _a = props.variant, variant = _a === void 0 ? "long" : _a;
    var _b = info.quantity, quantity = _b === void 0 ? 1 : _b;
    if (variant === "short") {
        return "".concat(quantity, "x");
    }
    else {
        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 quota = props.quota, _a = props.variant, variant = _a === void 0 ? "long" : _a;
    var subscription = quota.subscription, startRaw = quota.start, endRaw = quota.end;
    var _b = (0, util_1.useTimeFixer)(), fromServerTime = _b.fromServerTime, serverTimeDate = _b.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_1.roundToMidnight)(fromServerTime(startRaw), "start");
        var end = (0, timecalcs_1.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,
        });
        if (variant === "short") {
            var tsStart = (0, timestamp_1.processTimestamp)({ datetime: start, absolute: true });
            var tsEnd = (0, timestamp_1.processTimestamp)({ datetime: endDisplay, absolute: true });
            if (tsStart === "-" || tsEnd === "-") {
                return "-";
            }
            var timespanStr = "".concat(tsStart.absoluteTimeFull, " - ").concat(tsEnd.absoluteTimeFull);
            return ((0, jsx_runtime_1.jsx)(antd_1.Tooltip, __assign({ trigger: ["hover", "click"], title: timespanStr, placement: "bottom" }, { children: "".concat(days, " ").concat((0, misc_1.plural)(days, "day")) })));
        }
        else {
            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 {
        if (variant === "short") {
            return "".concat(subscription);
        }
        else {
            return "".concat(subscription, " subscription");
        }
    }
}
exports.describePeriod = describePeriod;
//# sourceMappingURL=site-license-cost.js.map