"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.DateRange = exports.Quota = exports.Details = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var describe_quota_1 = require("@cocalc/util/licenses/describe-quota");
var misc_1 = require("@cocalc/util/misc");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var copyable_1 = __importDefault(require("components/misc/copyable"));
var timestamp_1 = __importDefault(require("components/misc/timestamp"));
var loading_1 = __importDefault(require("components/share/loading"));
var api_1 = __importDefault(require("lib/hooks/api"));
var editable_license_1 = require("./editable-license");
function License(_a) {
    var license_id = _a.license_id, style = _a.style;
    // TODO: do something with contrib
    return ((0, jsx_runtime_1.jsx)(antd_1.Popover, __assign({ content: function () { return (0, jsx_runtime_1.jsx)(Details, { license_id: license_id }); }, title: license_id, mouseEnterDelay: 0.5 }, { children: (0, jsx_runtime_1.jsx)(A_1.default, __assign({ style: __assign({ cursor: "pointer", fontFamily: "monospace" }, style), href: "/licenses/how-used?license_id=".concat(license_id) }, { children: license_id })) })));
}
exports.default = License;
/*
{
  "id": "cad2fe88-29d7-4f4e-987c-a3ae6143a25b",
  "title": "different business license for specific time",
  "description": "",
  "expires": 1642799517638,
  "activates": 1640121117638,
  "last_used": null,
  "managers": [
    "93620c6e-324a-4217-a60e-2ac436953174"
  ],
  "upgrades": null,
  "quota": {
    "cpu": 1,
    "ram": 1,
    "disk": 1,
    "user": "business",
    "member": true,
    "dedicated_cpu": 0,
    "dedicated_ram": 0,
    "always_running": true
  },
  "run_limit": 3,
  is_manager:true,
  number_running:2
}
*/
function Details(_a) {
    var license_id = _a.license_id;
    var _b = (0, api_1.default)("licenses/get-license", { license_id: license_id }, 3), result = _b.result, error = _b.error; // 3s cache
    if (error) {
        return (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error });
    }
    if (!result) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, {});
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(result.title || result.is_manager) && ((0, jsx_runtime_1.jsx)("div", __assign({ style: { fontWeight: "bold", fontSize: "13pt" } }, { children: result.is_manager ? ((0, jsx_runtime_1.jsx)(editable_license_1.EditableTitle, { license_id: license_id, title: result.title })) : ("Title: " + result.title) }))), (result.description || result.is_manager) && ((0, jsx_runtime_1.jsx)("div", { children: result.is_manager ? ((0, jsx_runtime_1.jsx)(editable_license_1.EditableDescription, { license_id: license_id, description: result.description })) : ("Description: " + result.description) })), result.managers != null && (0, jsx_runtime_1.jsx)("div", { children: "You are a manager of this license." }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(DateRange, __assign({}, result)) }), result.last_used != null && ((0, jsx_runtime_1.jsxs)("div", { children: ["Last used: ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: result.last_used })] })), (0, jsx_runtime_1.jsxs)("div", { children: ["Quota: ", (0, jsx_runtime_1.jsx)(Quota, { quota: result.quota, upgrades: result.upgrades })] }), result.run_limit != null && ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { width: "100%", display: "flex" } }, { children: ["Run limit: ", result.run_limit, result.number_running != null
                        ? "; Currently running: ".concat(result.number_running)
                        : "", result.run_limit && result.number_running != null && ((0, jsx_runtime_1.jsx)(antd_1.Progress, { style: {
                            marginLeft: "15px",
                            flex: 1,
                        }, percent: Math.round((result.number_running / result.run_limit) * 100) }))] }))), result.is_manager && ((0, jsx_runtime_1.jsx)(copyable_1.default, { label: "ID:", value: license_id, style: { marginTop: "5px" } }))] }));
}
exports.Details = Details;
function Quota(_a) {
    var quota = _a.quota, upgrades = _a.upgrades;
    if (quota == null) {
        if (upgrades == null) {
            return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
        }
        else {
            // These are very old, and we just do a little bit to display
            // something valid.
            quota = {
                cpu: upgrades.cores,
                ram: upgrades.memory / 1000,
            };
        }
    }
    var info = (0, describe_quota_1.describe_quota)(quota, true);
    return (0, jsx_runtime_1.jsx)("span", { children: info });
}
exports.Quota = Quota;
function DateRange(_a) {
    var _b;
    var activates = _a.activates, expires = _a.expires, info = _a.info;
    var isExpired = expires && expires < new Date().valueOf();
    var sub = (_b = info === null || info === void 0 ? void 0 : info.purchased) === null || _b === void 0 ? void 0 : _b.subscription;
    if (sub && sub != "no") {
        return ((0, jsx_runtime_1.jsxs)("span", { children: [(0, misc_1.capitalize)(sub), " subscription", isExpired && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("b", { children: " Expired " }), " ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: expires, absolute: true, dateOnly: true })] }))] }));
    }
    var dates = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: activates, absolute: true, dateOnly: true }), " \u2013", " ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: expires, absolute: true, dateOnly: true })] }));
    return ((0, jsx_runtime_1.jsx)("span", { children: isExpired ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("b", { children: "Expired " }), "(was valid ", dates, ")"] })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Valid: ", dates] })) }));
}
exports.DateRange = DateRange;
//# sourceMappingURL=license.js.map