"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
 */
var link_1 = __importDefault(require("next/link"));
var antd_1 = require("antd");
var path_1 = require("path");
var misc_1 = require("@cocalc/util/misc");
function DirectoryListing(_a) {
    var id = _a.id, listing = _a.listing, relativePath = _a.relativePath, showHidden = _a.showHidden;
    return ((0, jsx_runtime_1.jsx)(antd_1.Table, { rowKey: "name", dataSource: filter(listing, showHidden), columns: columns(id, relativePath), pagination: {
            hideOnSinglePage: true,
            defaultPageSize: 50,
            showSizeChanger: true,
            pageSizeOptions: ["50", "100", "200", "500"],
        } }));
}
exports.default = DirectoryListing;
function filter(listing, showHidden) {
    var e_1, _a;
    var _b;
    if (showHidden) {
        return listing;
    }
    var v = [];
    try {
        for (var listing_1 = __values(listing), listing_1_1 = listing_1.next(); !listing_1_1.done; listing_1_1 = listing_1.next()) {
            var x = listing_1_1.value;
            if (!((_b = x.name) === null || _b === void 0 ? void 0 : _b.startsWith("."))) {
                v.push(x);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (listing_1_1 && !listing_1_1.done && (_a = listing_1.return)) _a.call(listing_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return v;
}
function columns(id, relativePath) {
    return [
        {
            title: "Name",
            dataIndex: "name",
            key: "name",
            // for style below, see comment in public-paths.tsx.
            render: function (name, record) {
                return ((0, jsx_runtime_1.jsx)(link_1.default, __assign({ href: "/share/public_paths/".concat(id, "/").concat(encodeURIComponent((0, path_1.join)(relativePath, name))) }, { children: (0, jsx_runtime_1.jsx)("a", __assign({ style: { width: "100%", display: "inline-block" } }, { children: record.isdir ? (0, jsx_runtime_1.jsx)("b", { children: name }) : name })) })));
            },
        },
        {
            title: "Date Modified",
            dataIndex: "mtime",
            key: "mtime",
            render: function (mtime) { return "".concat(new Date(mtime).toLocaleString()); },
        },
        {
            title: "Size",
            dataIndex: "size",
            key: "size",
            render: function (size, record) { return renderSize(size, record.isdir); },
            align: "right",
        },
    ];
}
function renderSize(size, isdir) {
    if (size == null)
        return "-";
    if (isdir)
        return "".concat(size, " ").concat((0, misc_1.plural)(size, "item"));
    return (0, misc_1.human_readable_size)(size);
}
//# sourceMappingURL=directory-listing.js.map