"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 __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
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 });
exports.LastEdited = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var api_1 = __importDefault(require("lib/hooks/api"));
var loading_1 = __importDefault(require("components/share/loading"));
var antd_1 = require("antd");
var edit_url_1 = __importDefault(require("lib/share/edit-url"));
var A_1 = __importDefault(require("components/misc/A"));
var misc_1 = require("@cocalc/util/misc");
var license_1 = __importDefault(require("./license"));
var r_join_1 = require("@cocalc/frontend/components/r_join");
var icon_1 = require("@cocalc/frontend/components/icon");
var misc_2 = require("@cocalc/util/misc");
var timestamp_1 = __importDefault(require("components/misc/timestamp"));
function Title(_a) {
    var title = _a.title, project_id = _a.project_id;
    return ((0, jsx_runtime_1.jsx)("span", __assign({ style: { wordWrap: "break-word", wordBreak: "break-word" } }, { children: (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: (0, edit_url_1.default)({ project_id: project_id, type: "collaborator" }), external: true }, { children: title })) })));
}
function Licenses(_a) {
    var site_license = _a.site_license, project_id = _a.project_id;
    return ((0, jsx_runtime_1.jsx)("div", __assign({ style: { wordWrap: "break-word", wordBreak: "break-word" } }, { children: (0, r_join_1.r_join)((0, misc_1.keys)(site_license).map(function (license_id) {
            var _a;
            return ((0, jsx_runtime_1.jsx)(license_1.default, { license_id: license_id, contrib: (_a = {}, _a[project_id] = site_license[license_id], _a) }, license_id));
        }), (0, jsx_runtime_1.jsx)("br", {})) })));
}
function LastEdited(_a) {
    var last_edited = _a.last_edited;
    return (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: last_edited });
}
exports.LastEdited = LastEdited;
function IsHidden(_a) {
    var hidden = _a.hidden;
    if (hidden) {
        return ((0, jsx_runtime_1.jsx)("div", __assign({ style: { textAlign: "center" } }, { children: (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "check" }) })));
    }
    else {
        return null;
    }
}
var columns = [
    {
        responsive: ["xs"],
        title: "Invoices and Receipts",
        render: function (_, project) { return ((0, jsx_runtime_1.jsxs)("div", { children: ["Project: ", (0, jsx_runtime_1.jsx)(Title, __assign({}, project)), (0, jsx_runtime_1.jsxs)("div", { children: ["Last Edited: ", (0, jsx_runtime_1.jsx)(LastEdited, __assign({}, project))] }), "Licenses:", (0, jsx_runtime_1.jsx)("div", __assign({ style: {
                        margin: "5px 0 0 30px",
                        border: "1px solid #eee",
                        padding: "5px",
                        borderRadius: "5px",
                    } }, { children: (0, jsx_runtime_1.jsx)(Licenses, __assign({}, project)) }))] })); },
    },
    {
        responsive: ["sm"],
        title: "Project",
        width: "30%",
        render: function (_, project) { return (0, jsx_runtime_1.jsx)(Title, __assign({}, project)); },
        sorter: { compare: function (a, b) { return (0, misc_1.cmp)(a.title, b.title); } },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, __assign({ placement: "bottom", title: "Licenses", content: (0, jsx_runtime_1.jsx)("div", __assign({ style: { maxWidth: "75ex" } }, { children: "These licenses are all applied to the project. They may or may not contribute any upgrades, depending on how the license is being used across all projects." })) }, { children: "Licenses" }))),
        render: function (_, project) { return (0, jsx_runtime_1.jsx)(Licenses, __assign({}, project)); },
        sorter: {
            compare: function (a, b) {
                return (0, misc_1.cmp)("".concat((0, misc_1.keys)(a.site_licenses)), "".concat((0, misc_1.keys)(b.site_licenses)));
            },
        },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, __assign({ placement: "bottom", title: "Last Edited", content: (0, jsx_runtime_1.jsx)("div", __assign({ style: { maxWidth: "75ex" } }, { children: "When the project was last edited." })) }, { children: "Project Last Edited" }))),
        render: function (_, project) { return (0, jsx_runtime_1.jsx)(LastEdited, __assign({}, project)); },
        sorter: { compare: function (a, b) { return (0, misc_1.cmp)(a.last_edited, b.last_edited); } },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, __assign({ placement: "bottom", title: "Project Hidden", content: (0, jsx_runtime_1.jsx)("div", __assign({ style: { maxWidth: "75ex" } }, { children: "Whether or not the project is \"hidden\" from you, so it doesn't appear in your default list of projects. Typically all student projects in a course you teach are hidden. There is a checkmark below for hidden projects." })) }, { children: "Project Hidden" }))),
        width: "10%",
        render: function (_, project) { return (0, jsx_runtime_1.jsx)(IsHidden, __assign({}, project)); },
        sorter: { compare: function (a, b) { return (0, misc_1.cmp)(a.hidden, b.hidden); } },
    },
];
function LicensedProjects() {
    var _a = __read((0, react_1.useState)(""), 2), search = _a[0], setSearch = _a[1];
    var _b = (0, api_1.default)("licenses/get-projects"), result = _b.result, error = _b.error;
    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, {});
    }
    if (search) {
        result = doSearch(result, search);
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("h3", { children: ["Your Licensed Projects (", result.length, ")"] }), "These are the licensed projects that you are a collaborator on. You might not be a manager of some of the licenses listed below. If you're teaching a course, the student projects are likely hidden from your normal project list, but are included below.", (0, jsx_runtime_1.jsx)("div", __assign({ style: { margin: "15px 0" } }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { placeholder: "Search...", allowClear: true, onChange: function (e) { return setSearch(e.target.value); }, style: { width: "40ex" } }) })), (0, jsx_runtime_1.jsx)(antd_1.Table, { columns: columns, dataSource: result, rowKey: "project_id", pagination: { hideOnSinglePage: true, pageSize: 100 } })] }));
}
exports.default = LicensedProjects;
function doSearch(data, search) {
    var e_1, _a;
    var v = (0, misc_2.search_split)(search.toLowerCase().trim());
    var w = [];
    try {
        for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
            var x = data_1_1.value;
            if (x["search"] == null) {
                x["search"] = "".concat(x["title"]).concat(JSON.stringify((0, misc_1.keys)(x["site_license"]))).toLowerCase();
            }
            if ((0, misc_2.search_match)(x["search"], v)) {
                w.push(x);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return w;
}
//# sourceMappingURL=licensed-projects.js.map