"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");
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
/*
A table of a list of public paths.
*/
const antd_1 = require("antd");
const badge_1 = __importDefault(require("components/misc/badge"));
const A_1 = __importDefault(require("components/misc/A"));
const sanitized_markdown_1 = __importDefault(require("components/misc/sanitized-markdown"));
const icon_1 = require("@cocalc/frontend/components/icon");
const misc_1 = require("@cocalc/util/misc");
const ui_1 = require("@cocalc/util/consts/ui");
function Description({ description, maxWidth, }) {
    if (!description?.trim())
        return null;
    return ((0, jsx_runtime_1.jsx)("div", { style: {
            maxWidth,
            maxHeight: "4em",
            overflow: "auto",
            border: "1px solid #eee",
            borderRadius: "3px",
            padding: "5px",
        }, children: (0, jsx_runtime_1.jsx)(sanitized_markdown_1.default, { value: description }) }));
}
function LastEdited({ last_edited }) {
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: `${new Date(parseFloat(last_edited)).toLocaleString()}` });
}
function Title({ id, title, url, avatar_image_tiny, }) {
    return ((0, jsx_runtime_1.jsxs)(A_1.default, { href: url ? `/${url}` : `/share/public_paths/${id}`, children: [avatar_image_tiny && ((0, jsx_runtime_1.jsx)(antd_1.Avatar, { size: 24, shape: "square", icon: (0, jsx_runtime_1.jsx)("img", { src: avatar_image_tiny }), style: { marginRight: "5px", marginTop: "-4px" } })), (0, misc_1.trunc_middle)(title, 48)] }));
}
function Visibility({ disabled, unlisted, vhost, authenticated }) {
    if (disabled) {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "lock" }), " Private"] }));
    }
    if (authenticated) {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: ui_1.SHARE_AUTHENTICATED_ICON }), " Authenticated"] }));
    }
    if (unlisted) {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "eye-slash" }), " Unlisted"] }));
    }
    if (vhost) {
        return (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Virtual Host: ", vhost] });
    }
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "eye" }), " Listed"] }));
}
function ViewsAndStars({ stars, views }) {
    return ((0, jsx_runtime_1.jsxs)("div", { style: { display: "flex" }, children: [views > 0 && ((0, jsx_runtime_1.jsxs)("div", { style: { marginRight: "30px" }, children: ["Views ", (0, jsx_runtime_1.jsx)(badge_1.default, { count: views })] })), stars > 0 && ((0, jsx_runtime_1.jsxs)("div", { children: ["Stars ", (0, jsx_runtime_1.jsx)(badge_1.default, { count: stars })] }))] }));
}
// I'm using any[]'s below since it's too much of a pain dealing with TS for this.
const COLUMNS0 = [
    {
        title: "Path",
        dataIndex: "path",
        key: "path",
        render: (title, record) => ((0, jsx_runtime_1.jsx)(Title, { id: record.id, title: title, url: record.url, avatar_image_tiny: record.avatar_image_tiny })),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("path"),
    },
    {
        title: "Description",
        dataIndex: "description",
        key: "description",
        render: (description) => ((0, jsx_runtime_1.jsx)(Description, { description: description, maxWidth: "250px" })),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("description"),
    },
    {
        title: "Last Modified",
        dataIndex: "last_edited",
        key: "last_edited",
        render: (last_edited) => (0, jsx_runtime_1.jsx)(LastEdited, { last_edited: last_edited }),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("last_edited"),
    },
    {
        title: "Stars",
        dataIndex: "stars",
        key: "stars",
        render: (stars) => (0, jsx_runtime_1.jsx)(badge_1.default, { count: stars }),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("stars"),
    },
    {
        title: "Views",
        dataIndex: "counter",
        key: "counter",
        render: (counter) => (0, jsx_runtime_1.jsx)(badge_1.default, { count: counter }),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("counter"),
    },
];
const COLUMNS = COLUMNS0.concat([
    {
        title: "Documents",
        responsive: ["xs"],
        key: "path",
        render: (_, record) => {
            const { path, url, last_edited, id, description, stars, counter } = record;
            return ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%" }, children: [(0, jsx_runtime_1.jsx)(Title, { title: path, id: id, url: url }), (0, jsx_runtime_1.jsx)(Description, { description: description }), (0, jsx_runtime_1.jsx)(LastEdited, { last_edited: last_edited }), (0, jsx_runtime_1.jsx)(ViewsAndStars, { stars: stars, views: counter })] }));
        },
    },
]);
const COLUMNS_WITH_VISIBILITY = COLUMNS0.concat([
    {
        title: "Visibility",
        dataIndex: "disabled",
        key: "disabled",
        render: (_, record) => ((0, jsx_runtime_1.jsx)(Visibility, { disabled: record.disabled, unlisted: record.unlisted, authenticated: record.authenticated, vhost: record.vhost })),
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)(["disabled", "unlisted", "vhost", "authenticated"]),
    },
    {
        title: "Documents",
        responsive: ["xs"],
        key: "path",
        render: (_, record) => {
            const { path, last_edited, id, description, stars, counter, url } = record;
            return ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%" }, children: [(0, jsx_runtime_1.jsx)(Title, { title: path, id: id, url: url }), (0, jsx_runtime_1.jsx)(Description, { description: description }), (0, jsx_runtime_1.jsx)(LastEdited, { last_edited: last_edited }), (0, jsx_runtime_1.jsx)(Visibility, { disabled: record.disabled, unlisted: record.unlisted, authenticated: record.authenticated, vhost: record.vhost }), (0, jsx_runtime_1.jsx)(ViewsAndStars, { stars: stars, views: counter })] }));
        },
    },
]);
function PublicPaths({ publicPaths }) {
    let showVisibility = false;
    if (publicPaths) {
        for (const path of publicPaths) {
            const { disabled, unlisted, authenticated } = path;
            if (disabled || unlisted || authenticated) {
                showVisibility = true;
                break;
            }
        }
    }
    return ((0, jsx_runtime_1.jsx)(antd_1.Table, { pagination: false, rowKey: "id", loading: publicPaths == null, dataSource: publicPaths, columns: showVisibility ? COLUMNS_WITH_VISIBILITY : COLUMNS, style: { overflowX: "auto" } }));
}
exports.default = PublicPaths;
//# sourceMappingURL=public-paths.js.map