"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 __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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");
/*
 *  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.
*/
var antd_1 = require("antd");
var badge_1 = __importDefault(require("components/misc/badge"));
var A_1 = __importDefault(require("components/misc/A"));
var sanitized_markdown_1 = __importDefault(require("components/misc/sanitized-markdown"));
var icon_1 = require("@cocalc/frontend/components/icon");
var misc_1 = require("@cocalc/util/misc");
var ui_1 = require("@cocalc/util/consts/ui");
function Description(_a) {
    var description = _a.description, maxWidth = _a.maxWidth;
    if (!(description === null || description === void 0 ? void 0 : description.trim()))
        return null;
    return ((0, jsx_runtime_1.jsx)("div", __assign({ style: {
            maxWidth: 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(_a) {
    var last_edited = _a.last_edited;
    return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: "".concat(new Date(parseFloat(last_edited)).toLocaleString()) });
}
function Title(_a) {
    var id = _a.id, title = _a.title, url = _a.url;
    return ((0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: url ? "/".concat(url) : "/share/public_paths/".concat(id) }, { children: (0, misc_1.trunc_middle)(title, 48) })));
}
function Visibility(_a) {
    var disabled = _a.disabled, unlisted = _a.unlisted, vhost = _a.vhost, authenticated = _a.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(_a) {
    var stars = _a.stars, views = _a.views;
    return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { display: "flex" } }, { children: [views > 0 && ((0, jsx_runtime_1.jsxs)("div", __assign({ 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.
var COLUMNS0 = [
    {
        title: "Path",
        dataIndex: "path",
        key: "path",
        render: function (title, record) { return ((0, jsx_runtime_1.jsx)(Title, { id: record.id, title: title, url: record.url })); },
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("path"),
    },
    {
        title: "Description",
        dataIndex: "description",
        key: "description",
        render: function (description) { return ((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: function (last_edited) { return (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: function (stars) { return (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: function (counter) { return (0, jsx_runtime_1.jsx)(badge_1.default, { count: counter }); },
        responsive: ["sm"],
        sorter: (0, misc_1.field_cmp)("counter"),
    },
];
var COLUMNS = COLUMNS0.concat([
    {
        title: "Documents",
        responsive: ["xs"],
        key: "path",
        render: function (_, record) {
            var path = record.path, url = record.url, last_edited = record.last_edited, id = record.id, description = record.description, stars = record.stars, counter = record.counter;
            return ((0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ 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 })] })));
        },
    },
]);
var COLUMNS_WITH_VISIBILITY = COLUMNS0.concat([
    {
        title: "Visibility",
        dataIndex: "disabled",
        key: "disabled",
        render: function (_, record) { return ((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: function (_, record) {
            var path = record.path, last_edited = record.last_edited, id = record.id, description = record.description, stars = record.stars, counter = record.counter, url = record.url;
            return ((0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ 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(_a) {
    var e_1, _b;
    var publicPaths = _a.publicPaths;
    var showVisibility = false;
    if (publicPaths) {
        try {
            for (var publicPaths_1 = __values(publicPaths), publicPaths_1_1 = publicPaths_1.next(); !publicPaths_1_1.done; publicPaths_1_1 = publicPaths_1.next()) {
                var path = publicPaths_1_1.value;
                var disabled = path.disabled, unlisted = path.unlisted, authenticated = path.authenticated;
                if (disabled || unlisted || authenticated) {
                    showVisibility = true;
                    break;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (publicPaths_1_1 && !publicPaths_1_1.done && (_b = publicPaths_1.return)) _b.call(publicPaths_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    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