"use strict";
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;
const 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
 */
const icon_1 = require("@cocalc/frontend/components/icon");
const site_license_1 = require("@cocalc/util/consts/site-license");
const describe_quota_1 = require("@cocalc/util/licenses/describe-quota");
const utils_1 = require("@cocalc/util/licenses/purchase/utils");
const misc_1 = require("@cocalc/util/misc");
const timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
const utils_2 = require("@cocalc/util/upgrades/utils");
const timestamp_1 = __importStar(require("components/misc/timestamp"));
const util_1 = require("./util");
const antd_1 = require("antd");
const { Text } = antd_1.Typography;
function DisplayCost(props) {
    const { cost, simple = false, oneLine = false, simpleShowPeriod = true, discountTooltip = false, } = props;
    if (isNaN(cost.cost) || isNaN(cost.discounted_cost)) {
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "\u2013" });
    }
    const discount_pct = (0, utils_1.percent_discount)(cost);
    if (simple) {
        const 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] }));
    }
    let desc;
    if (cost.discounted_cost < cost.cost) {
        desc = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", { 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 = `${(0, utils_1.money)(cost.cost)} ${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) {
    const { info, variant = "long" } = props;
    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 })] }));
    }
    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 })] }));
    }
    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");
    }
    const { always_running, idle_timeout } = (0, site_license_1.untangleUptime)(info.custom_uptime ?? "short");
    const quota = {
        ram: info.custom_ram,
        cpu: info.custom_cpu,
        disk: info.custom_disk,
        always_running,
        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, { strong: true, children: describeQuantity({ quota: info, variant }) }), " ", (0, describe_quota_1.describeQuotaOnLine)(quota), ", ", describePeriod({ quota: info, 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 }), " (", describePeriod({ quota: info, variant }), ")"] }));
    }
}
exports.describeItem = describeItem;
function describeQuantity(props) {
    const { quota: info, variant = "long" } = props;
    const { quantity = 1 } = info;
    if (variant === "short") {
        return `${quantity}x`;
    }
    else {
        return `for ${quantity} running ${(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) {
    const { quota, variant = "long" } = props;
    const { subscription, start: startRaw, end: endRaw } = quota;
    const { fromServerTime, serverTimeDate } = (0, util_1.useTimeFixer)();
    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
        const start = (0, timecalcs_1.roundToMidnight)(fromServerTime(startRaw), "start");
        const 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
        const days = (0, timecalcs_1.getDays)({ start, 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
        const endDisplay = (0, timecalcs_1.appendAfterNowToDate)({
            now: serverTimeDate,
            start,
            end,
        });
        if (variant === "short") {
            const tsStart = (0, timestamp_1.processTimestamp)({ datetime: start, absolute: true });
            const tsEnd = (0, timestamp_1.processTimestamp)({ datetime: endDisplay, absolute: true });
            if (tsStart === "-" || tsEnd === "-") {
                return "-";
            }
            const timespanStr = `${tsStart.absoluteTimeFull} - ${tsEnd.absoluteTimeFull}`;
            return ((0, jsx_runtime_1.jsx)(antd_1.Tooltip, { trigger: ["hover", "click"], title: timespanStr, placement: "bottom", children: `${days} ${(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 `${subscription}`;
        }
        else {
            return `${subscription} subscription`;
        }
    }
}
exports.describePeriod = describePeriod;
//# sourceMappingURL=site-license-cost.js.map