"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LastEdited = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const api_1 = __importDefault(require("lib/hooks/api"));
const loading_1 = __importDefault(require("components/share/loading"));
const antd_1 = require("antd");
const edit_url_1 = __importDefault(require("lib/share/edit-url"));
const A_1 = __importDefault(require("components/misc/A"));
const misc_1 = require("@cocalc/util/misc");
const license_1 = __importDefault(require("./license"));
const r_join_1 = require("@cocalc/frontend/components/r_join");
const icon_1 = require("@cocalc/frontend/components/icon");
const misc_2 = require("@cocalc/util/misc");
const timestamp_1 = __importDefault(require("components/misc/timestamp"));
function Title({ title, project_id }) {
    return ((0, jsx_runtime_1.jsx)("span", { style: { wordWrap: "break-word", wordBreak: "break-word" }, children: (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, edit_url_1.default)({ project_id, type: "collaborator" }), external: true, children: title }) }));
}
function Licenses({ site_license, project_id }) {
    return ((0, jsx_runtime_1.jsx)("div", { style: { wordWrap: "break-word", wordBreak: "break-word" }, children: (0, r_join_1.r_join)((0, misc_1.keys)(site_license).map((license_id) => ((0, jsx_runtime_1.jsx)(license_1.default, { license_id: license_id, contrib: { [project_id]: site_license[license_id] } }, license_id))), (0, jsx_runtime_1.jsx)("br", {})) }));
}
function LastEdited({ last_edited }) {
    return (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: last_edited });
}
exports.LastEdited = LastEdited;
function IsHidden({ hidden }) {
    if (hidden) {
        return ((0, jsx_runtime_1.jsx)("div", { style: { textAlign: "center" }, children: (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "check" }) }));
    }
    else {
        return null;
    }
}
const columns = [
    {
        responsive: ["xs"],
        title: "Invoices and Receipts",
        render: (_, project) => ((0, jsx_runtime_1.jsxs)("div", { children: ["Project: ", (0, jsx_runtime_1.jsx)(Title, { ...project }), (0, jsx_runtime_1.jsxs)("div", { children: ["Last Edited: ", (0, jsx_runtime_1.jsx)(LastEdited, { ...project })] }), "Licenses:", (0, jsx_runtime_1.jsx)("div", { style: {
                        margin: "5px 0 0 30px",
                        border: "1px solid #eee",
                        padding: "5px",
                        borderRadius: "5px",
                    }, children: (0, jsx_runtime_1.jsx)(Licenses, { ...project }) })] })),
    },
    {
        responsive: ["sm"],
        title: "Project",
        width: "30%",
        render: (_, project) => (0, jsx_runtime_1.jsx)(Title, { ...project }),
        sorter: { compare: (a, b) => (0, misc_1.cmp)(a.title, b.title) },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, { placement: "bottom", title: "Licenses", content: (0, jsx_runtime_1.jsx)("div", { 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: (_, project) => (0, jsx_runtime_1.jsx)(Licenses, { ...project }),
        sorter: {
            compare: (a, b) => (0, misc_1.cmp)(`${(0, misc_1.keys)(a.site_licenses)}`, `${(0, misc_1.keys)(b.site_licenses)}`),
        },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, { placement: "bottom", title: "Last Edited", content: (0, jsx_runtime_1.jsx)("div", { style: { maxWidth: "75ex" }, children: "When the project was last edited." }), children: "Project Last Edited" })),
        render: (_, project) => (0, jsx_runtime_1.jsx)(LastEdited, { ...project }),
        sorter: { compare: (a, b) => (0, misc_1.cmp)(a.last_edited, b.last_edited) },
    },
    {
        responsive: ["sm"],
        title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, { placement: "bottom", title: "Project Hidden", content: (0, jsx_runtime_1.jsx)("div", { 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: (_, project) => (0, jsx_runtime_1.jsx)(IsHidden, { ...project }),
        sorter: { compare: (a, b) => (0, misc_1.cmp)(a.hidden, b.hidden) },
    },
];
function LicensedProjects() {
    const [search, setSearch] = (0, react_1.useState)("");
    let { result, error } = (0, api_1.default)("licenses/get-projects");
    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", { style: { margin: "15px 0" }, children: (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { placeholder: "Search...", allowClear: true, onChange: (e) => 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) {
    const v = (0, misc_2.search_split)(search.toLowerCase().trim());
    const w = [];
    for (const x of data) {
        if (x["search"] == null) {
            x["search"] = `${x["title"]}${JSON.stringify((0, misc_1.keys)(x["site_license"]))}`.toLowerCase();
        }
        if ((0, misc_2.search_match)(x["search"], v)) {
            w.push(x);
        }
    }
    return w;
}
//# sourceMappingURL=licensed-projects.js.map