"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 __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");
var react_1 = require("react");
var A_1 = __importDefault(require("components/misc/A"));
var antd_1 = require("antd");
var file_associations_1 = require("@cocalc/frontend/file-associations");
var icon_1 = require("@cocalc/frontend/components/icon");
var cmp_1 = require("@cocalc/util/cmp");
var openedFilesColumns = [
    {
        title: "Type of File",
        dataIndex: "ext",
        key: "ext",
        render: function (ext) {
            var _a, _b, _c;
            var icon = ((0, jsx_runtime_1.jsx)(icon_1.Icon, { style: { marginRight: "10px", fontSize: "14pt" }, name: (_b = (_a = file_associations_1.file_associations[ext]) === null || _a === void 0 ? void 0 : _a.icon) !== null && _b !== void 0 ? _b : (_c = file_associations_1.file_associations[""]) === null || _c === void 0 ? void 0 : _c.icon }));
            var info = extensionToInfo[ext];
            if (info == null)
                return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [icon, " ", ext] }));
            var 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, __assign({ href: info.link }, { children: x }));
        },
        sorter: function (a, b) {
            var _a, _b, _c, _d;
            return (0, cmp_1.cmp)((_b = (_a = extensionToInfo[a.ext]) === null || _a === void 0 ? void 0 : _a.desc) !== null && _b !== void 0 ? _b : a.ext, (_d = (_c = extensionToInfo[b.ext]) === null || _c === void 0 ? void 0 : _c.desc) !== null && _d !== void 0 ? _d : 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"),
    },
];
var 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) {
    var lastHour = 0;
    var counts = distinct ? filesOpened.distinct : filesOpened.total;
    var byExtension = {};
    for (var time in counts) {
        var extToCount = counts[time];
        for (var ext in extToCount) {
            var 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;
            }
        }
    }
    var rows = [];
    for (var ext in byExtension) {
        var counts_1 = byExtension[ext];
        rows.push(__assign({ ext: ext }, counts_1));
    }
    return { rows: rows, lastHour: lastHour };
}
function OpenedFiles(_a) {
    var filesOpened = _a.filesOpened;
    var _b = __read((0, react_1.useState)(true), 2), distinct = _b[0], setDistinct = _b[1];
    var _c = (0, react_1.useMemo)(function () { return processFilesOpened(filesOpened, distinct); }, [distinct, filesOpened]), rows = _c.rows, lastHour = _c.lastHour;
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", __assign({ 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