"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 A_1 = __importDefault(require("components/misc/A"));
const antd_1 = require("antd");
const file_associations_1 = require("@cocalc/frontend/file-associations");
const icon_1 = require("@cocalc/frontend/components/icon");
const cmp_1 = require("@cocalc/util/cmp");
const openedFilesColumns = [
    {
        title: "Type of File",
        dataIndex: "ext",
        key: "ext",
        render: (ext) => {
            const icon = ((0, jsx_runtime_1.jsx)(icon_1.Icon, { style: { marginRight: "10px", fontSize: "14pt" }, name: file_associations_1.file_associations[ext]?.icon ?? file_associations_1.file_associations[""]?.icon }));
            const info = extensionToInfo[ext];
            if (info == null)
                return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [icon, " ", ext] }));
            const x = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [icon, " ", info.desc, " (.", ext, ")"] }));
            if (info.link == null)
                return x;
            return (0, jsx_runtime_1.jsx)(A_1.default, { href: info.link, children: x });
        },
        sorter: (a, b) => (0, cmp_1.cmp)(extensionToInfo[a.ext]?.desc ?? a.ext, extensionToInfo[b.ext]?.desc ?? b.ext),
    },
    {
        title: "Hour",
        dataIndex: "1h",
        key: "1h",
        sorter: (0, cmp_1.field_cmp)("1h"),
        defaultSortOrder: "descend",
    },
    { title: "Day", dataIndex: "1d", key: "1d", sorter: (0, cmp_1.field_cmp)("1d") },
    { title: "Week", dataIndex: "7d", key: "7d", sorter: (0, cmp_1.field_cmp)("7d") },
    {
        title: "Month",
        dataIndex: "30d",
        key: "30d",
        sorter: (0, cmp_1.field_cmp)("30d"),
    },
];
const extensionToInfo = {
    md: { desc: "Markdown" },
    py: { desc: "Python", link: "/features/python" },
    jpg: { desc: "Image" },
    pdf: { desc: "PDF" },
    png: { desc: "Image" },
    rmd: { desc: "RMarkdown", link: "/features/r" },
    rnw: { desc: "Knitr" },
    rst: { desc: "ReST" },
    svg: { desc: "Image" },
    tex: { desc: "LaTeX", link: "/features/latex-editor" },
    txt: { desc: "Plain Text" },
    x11: { desc: "X11 Linux Desktop", link: "/features/x11" },
    jpeg: { desc: "Image" },
    lean: { desc: "LEAN theorem prover" },
    rtex: { desc: "Knitr" },
    sage: {
        desc: "SageMath",
        link: "https://www.sagemath.org/",
    },
    term: { desc: "Linux Terminal", link: "/features/terminal" },
    ipynb: { desc: "Jupyter Notebook", link: "/features/jupyter-notebook" },
    tasks: { desc: "Task List", link: "https://doc.cocalc.com/tasks.html" },
    course: {
        desc: "Course Management",
        link: "https://doc.cocalc.com/teaching-instructors.html",
    },
    sagews: {
        desc: "Sage Worksheet",
        link: "https://doc.cocalc.com/sagews.html",
    },
    "sage-chat": { desc: "Chatroom" },
    board: { desc: "Whiteboard", link: "/features/whiteboard" },
};
function processFilesOpened(filesOpened, distinct) {
    let lastHour = 0;
    const counts = distinct ? filesOpened.distinct : filesOpened.total;
    const byExtension = {};
    for (const time in counts) {
        const extToCount = counts[time];
        for (const ext in extToCount) {
            const cnt = parseInt(extToCount[ext]);
            if (byExtension[ext] == null) {
                byExtension[ext] = { "1h": 0, "1d": 0, "7d": 0, "30d": 0 };
            }
            byExtension[ext][time] += cnt;
            if (time == "1h") {
                lastHour += cnt;
            }
        }
    }
    const rows = [];
    for (const ext in byExtension) {
        const counts = byExtension[ext];
        rows.push({ ext, ...counts });
    }
    return { rows, lastHour };
}
function OpenedFiles({ filesOpened, }) {
    const [distinct, setDistinct] = (0, react_1.useState)(true);
    const { rows, lastHour } = (0, react_1.useMemo)(() => processFilesOpened(filesOpened, distinct), [distinct, filesOpened]);
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", { style: { float: "right" }, children: [(0, jsx_runtime_1.jsx)(antd_1.Switch, { checked: distinct, onChange: setDistinct }), " Distinct"] }), (0, jsx_runtime_1.jsxs)("h2", { children: [distinct ? "Distinct " : "", "Files Used in the Last Hour: ", lastHour, " "] }), (0, jsx_runtime_1.jsxs)("p", { children: ["Track the number of ", distinct ? "distinct" : "", " files of each type that people opened during the last hour, day, week and month."] }), (0, jsx_runtime_1.jsx)(antd_1.Table, { dataSource: rows, columns: openedFilesColumns, bordered: true, pagination: false, rowKey: "ext" })] }));
}
exports.default = OpenedFiles;
//# sourceMappingURL=opened-files.js.map