"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const loading_1 = __importDefault(require("components/share/loading"));
const antd_1 = require("antd");
const select_license_1 = __importDefault(require("./select-license"));
const avatar_1 = __importDefault(require("components/account/avatar"));
const misc_1 = require("@cocalc/util/misc");
const A_1 = __importDefault(require("components/misc/A"));
const post_1 = __importDefault(require("lib/api/post"));
const misc_2 = require("@cocalc/util/misc");
const edit_url_1 = __importDefault(require("lib/share/edit-url"));
const license_1 = require("./license");
const managed_1 = require("./managed");
const router_1 = require("next/router");
const copyable_1 = __importDefault(require("components/misc/copyable"));
const licensed_projects_1 = require("./licensed-projects");
function TitleId({ title, project_id, collaborators, account_id, label }) {
    return ((0, jsx_runtime_1.jsxs)("div", { style: { wordWrap: "break-word", wordBreak: "break-word" }, children: [collaborators.includes(account_id) ? ((0, jsx_runtime_1.jsx)(A_1.default, { href: (0, edit_url_1.default)({ project_id, type: "collaborator" }), external: true, children: title })) : (title), label && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("br", {}), "Project Id:"] })), (0, jsx_runtime_1.jsx)(copyable_1.default, { value: project_id, size: "small" })] }));
}
function Collaborators({ collaborators }) {
    return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: collaborators.map((account_id) => ((0, jsx_runtime_1.jsx)(avatar_1.default, { account_id: account_id, size: 24, style: { marginRight: "2.5px" } }, account_id))) }));
}
function State({ state }) {
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, misc_2.capitalize)(state) });
}
function HowLicenseUsed({ account_id }) {
    const router = (0, router_1.useRouter)();
    const [license, setLicense] = (0, react_1.useState)(`${router.query.license_id ?? ""}`);
    const [search, setSearch] = (0, react_1.useState)("");
    const [error, setError] = (0, react_1.useState)("");
    let [projects, setProjects] = (0, react_1.useState)([]);
    const [loading, setLoading] = (0, react_1.useState)(false);
    const [excludeMe, setExcludeMe] = (0, react_1.useState)(false);
    const columns = (0, react_1.useMemo)(() => {
        return [
            {
                responsive: ["xs"],
                render: (_, project) => ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(TitleId, { ...project, account_id: account_id, label: true }), (0, jsx_runtime_1.jsxs)("div", { children: ["Last Edited: ", (0, jsx_runtime_1.jsx)(licensed_projects_1.LastEdited, { ...project })] }), (0, jsx_runtime_1.jsxs)("div", { children: ["State: ", (0, jsx_runtime_1.jsx)(State, { ...project })] }), (0, jsx_runtime_1.jsxs)("div", { children: ["Collaborators: ", (0, jsx_runtime_1.jsx)(Collaborators, { ...project })] }), (0, jsx_runtime_1.jsxs)("div", { children: ["Quota in use: ", (0, jsx_runtime_1.jsx)(managed_1.Quota, { ...project })] })] })),
            },
            {
                responsive: ["sm"],
                title: ((0, jsx_runtime_1.jsx)(antd_1.Popover, { placement: "bottom", title: "Project", content: (0, jsx_runtime_1.jsx)("div", { style: { maxWidth: "75ex" }, children: "This is the title and id of the project. If you are a collaborator on this project, then you can click the title to open the project." }), children: "Project" })),
                width: "30%",
                render: (_, project) => ((0, jsx_runtime_1.jsx)(TitleId, { ...project, account_id: account_id })),
                sorter: { compare: (a, b) => (0, misc_2.cmp)(a.title, b.title) },
            },
            {
                responsive: ["sm"],
                title: "Last Edited",
                render: (_, project) => (0, jsx_runtime_1.jsx)(licensed_projects_1.LastEdited, { ...project }),
                sorter: { compare: (a, b) => (0, misc_2.cmp)(a.last_edited, b.last_edited) },
            },
            {
                responsive: ["sm"],
                title: ((0, jsx_runtime_1.jsxs)(antd_1.Popover, { title: "Collaborators", content: (0, jsx_runtime_1.jsxs)("div", { style: { maxWidth: "75ex" }, children: ["These are the collaborators on this project. You are not necessarily included in this list, since this license can be applied to any project by somebody who knows the license code. Click the \"Exclude me\" checkbox to see only projects that you are ", (0, jsx_runtime_1.jsx)("b", { children: "not" }), " a collaborator on."] }), children: ["Collaborators", (0, jsx_runtime_1.jsx)("div", { style: { fontWeight: 300 }, children: (0, jsx_runtime_1.jsx)(antd_1.Checkbox, { onChange: (e) => setExcludeMe(e.target.checked), checked: excludeMe, children: "Exclude me" }) })] })),
                render: (_, project) => (0, jsx_runtime_1.jsx)(Collaborators, { ...project }),
            },
            {
                responsive: ["sm"],
                title: "State",
                dataIndex: "state",
                key: "state",
                sorter: { compare: (a, b) => (0, misc_2.cmp)(a.state, b.state) },
                render: (_, project) => (0, jsx_runtime_1.jsx)(State, { ...project }),
            },
            managed_1.quotaColumn,
        ];
    }, [account_id, excludeMe]);
    async function load(license_id) {
        setLicense(license_id);
        setError("");
        if (license_id) {
            setLoading(true);
            setProjects([]);
            try {
                setProjects(await (0, post_1.default)("/licenses/get-projects-with-license", {
                    license_id,
                }));
            }
            catch (err) {
                setError(err.message);
            }
            finally {
                setLoading(false);
            }
        }
    }
    (0, react_1.useEffect)(() => {
        // initial license load (e.g., from query param)
        if (license) {
            load(license);
        }
    }, []);
    return ((0, jsx_runtime_1.jsxs)("div", { style: { width: "100%", overflowX: "auto", minHeight: "50vh" }, children: [(0, jsx_runtime_1.jsx)("h3", { children: "How a License You Manage is Being Used" }), "Select a license you manage to see how it is being used. You can see", " ", (0, jsx_runtime_1.jsx)("i", { children: "all" }), " projects that have this license applied to them (even if you are not a collaborator on them!), remove licenses from projects, and view analytics about how the license has been used over time to better inform your decision making.", (0, jsx_runtime_1.jsx)("div", { style: { margin: "15px 0", width: "100%", textAlign: 'center' }, children: (0, jsx_runtime_1.jsx)(select_license_1.default, { disabled: loading, onSelect: (license_id) => {
                        router.push({
                            pathname: router.asPath.split("?")[0],
                            query: { license_id },
                        });
                        load(license_id);
                    }, license: license, style: { width: "100%", maxWidth: "90ex" } }) }), license && error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error }), license && loading && ((0, jsx_runtime_1.jsx)(loading_1.default, { style: { fontSize: "16pt", margin: "auto" } })), (0, jsx_runtime_1.jsx)("div", { style: {
                    border: "1px solid lightgrey",
                    borderRadius: "5px",
                    padding: "15px",
                    backgroundColor: "#fafafa",
                    width: "100%",
                    maxWidth: "90ex",
                    margin: "auto",
                }, children: license ? ((0, jsx_runtime_1.jsx)(license_1.Details, { license_id: license })) : ((0, jsx_runtime_1.jsx)("div", { style: { textAlign: "center", fontSize: "13pt" }, children: "Select a license above." })) }), license && !loading && projects.length > 1 && ((0, jsx_runtime_1.jsx)("div", { style: { margin: "15px 0", maxWidth: "50ex" }, children: (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { placeholder: "Search project titles...", allowClear: true, onChange: (e) => setSearch(e.target.value), style: { width: "100%" } }) })), license && !loading && ((0, jsx_runtime_1.jsx)(antd_1.Table, { columns: columns, dataSource: doSearch(projects, search, excludeMe, account_id), rowKey: "project_id", style: { marginTop: "15px" }, pagination: { hideOnSinglePage: true, pageSize: 100 } }))] }));
}
exports.default = HowLicenseUsed;
function doSearch(data, search, excludeMe, account_id) {
    const v = (0, misc_1.search_split)(search.toLowerCase().trim());
    const w = [];
    for (const x of data) {
        if (excludeMe && x["collaborators"]?.includes(account_id))
            continue;
        if (x["search"] == null) {
            x["search"] = `${x["title"] ?? ""} ${x["id"]}`.toLowerCase();
        }
        if ((0, misc_1.search_match)(x["search"], v)) {
            w.push(x);
        }
    }
    return w;
}
//# sourceMappingURL=how-used.js.map