"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
/*
Select a site license, either by pasting one in, or
from a list of all of the licenses the signed in user
is a manager of.
*/
var react_1 = require("react");
var antd_1 = require("antd");
var select_license_1 = __importDefault(require("@cocalc/frontend/site-licenses/select-license"));
var database_1 = __importDefault(require("lib/hooks/database"));
var loading_1 = __importDefault(require("components/share/loading"));
function SelectSiteLicense(_a) {
    var onChange = _a.onChange, defaultLicenseId = _a.defaultLicenseId;
    var _b = (0, database_1.default)({
        manager_site_licenses: [
            { id: null, expires: null, title: null, quota: null },
        ],
    }), error = _b.error, value = _b.value, loading = _b.loading;
    var managedLicenses = (0, react_1.useMemo)(function () {
        var e_1, _a;
        var x = {};
        if (!value)
            return x;
        var manager_site_licenses = value.manager_site_licenses;
        try {
            for (var manager_site_licenses_1 = __values(manager_site_licenses), manager_site_licenses_1_1 = manager_site_licenses_1.next(); !manager_site_licenses_1_1.done; manager_site_licenses_1_1 = manager_site_licenses_1.next()) {
                var license = manager_site_licenses_1_1.value;
                if (license.expires) {
                    // comes back from database as ISO string.
                    license.expires = new Date(license.expires);
                }
                x[license.id] = license;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (manager_site_licenses_1_1 && !manager_site_licenses_1_1.done && (_a = manager_site_licenses_1.return)) _a.call(manager_site_licenses_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return x;
    }, [value]);
    if (loading || value == null) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, {});
    }
    if (error) {
        return (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error });
    }
    return ((0, jsx_runtime_1.jsx)(select_license_1.default, { onChange: onChange, defaultLicenseId: defaultLicenseId, managedLicenses: managedLicenses }));
}
exports.default = SelectSiteLicense;
//# sourceMappingURL=select-site-license.js.map