"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
 */
const react_1 = require("react");
const antd_1 = require("antd");
const link_1 = __importDefault(require("next/link"));
const path_contents_1 = __importDefault(require("components/share/path-contents"));
const path_actions_1 = __importDefault(require("components/share/path-actions"));
const linked_path_1 = __importDefault(require("components/share/linked-path"));
const loading_1 = __importDefault(require("components/share/loading"));
const license_1 = __importDefault(require("components/share/license"));
const project_link_1 = __importDefault(require("components/share/project-link"));
const counter_1 = __importDefault(require("lib/share/counter"));
const layout_1 = require("components/share/layout");
const customize_1 = require("lib/share/customize");
const util_1 = require("lib/share/util");
const sanitized_markdown_1 = __importDefault(require("components/misc/sanitized-markdown"));
const badge_1 = __importDefault(require("components/misc/badge"));
const icon_1 = require("@cocalc/frontend/components/icon");
const ui_1 = require("@cocalc/util/consts/ui");
const post_1 = __importDefault(require("lib/api/post"));
const in_place_sign_in_or_up_1 = __importDefault(require("components/auth/in-place-sign-in-or-up"));
const router_1 = require("next/router");
const avatar_1 = __importDefault(require("components/share/proxy/avatar"));
const A_1 = __importDefault(require("components/misc/A"));
const path_1 = require("path");
function PublicPath({ id, path, url, project_id, projectTitle, relativePath = "", description, counter, compute_image, license, contents, error, customize, disabled, unlisted, authenticated, stars = 0, isStarred: isStarred0, githubOrg, githubRepo, projectAvatarImage, redirect, }) {
    (0, counter_1.default)(id);
    const [numStars, setNumStars] = (0, react_1.useState)(stars);
    const [isStarred, setIsStarred] = (0, react_1.useState)(isStarred0 ?? null);
    (0, react_1.useEffect)(() => {
        setIsStarred(isStarred0);
    }, [isStarred0]);
    const [signingUp, setSigningUp] = (0, react_1.useState)(false);
    const router = (0, router_1.useRouter)();
    (0, react_1.useEffect)(() => {
        if (redirect) {
            router.replace(redirect);
        }
    }, [redirect]);
    if (id == null)
        return (0, jsx_runtime_1.jsx)(loading_1.default, { style: { fontSize: "30px" } });
    function visibility_explanation() {
        if (disabled) {
            return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "lock" }), " Private (only visible to collaborators on the project)"] }));
        }
        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 (only visible to those who know the link)"] }));
        }
        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 (", ui_1.SHARE_AUTHENTICATED_EXPLANATION, ")"] }));
        }
    }
    function visibility() {
        if (unlisted || disabled || authenticated) {
            return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("b", { children: "Visibility:" }), " ", visibility_explanation()] }));
        }
    }
    async function star() {
        setIsStarred(true);
        setNumStars(numStars + 1);
        // Actually do the api call after changing state, so it is
        // maximally snappy.  Also, being absolutely certain that star/unstar
        // actually worked is not important.
        await (0, post_1.default)("/public-paths/star", { id });
    }
    async function unstar() {
        setIsStarred(false);
        setNumStars(numStars - 1);
        await (0, post_1.default)("/public-paths/unstar", { id });
    }
    function renderStar() {
        const badge = ((0, jsx_runtime_1.jsx)(badge_1.default, { count: numStars, style: {
                marginLeft: "10px",
                marginTop: "-2.5px",
            } }));
        if (isStarred == null) {
            // not signed in ==> isStarred is null or undefined.
            return ((0, jsx_runtime_1.jsxs)(antd_1.Button, { onClick: () => {
                    setSigningUp(!signingUp);
                }, title: "Sign in to star", children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "star" }), " Star ", badge] }));
        }
        // Signed in so isStarred is true or false.
        let btn;
        if (isStarred == true) {
            btn = ((0, jsx_runtime_1.jsxs)(antd_1.Button, { onClick: unstar, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "star-filled", style: { color: "#eac54f" } }), " Starred", " ", badge] }));
        }
        else {
            btn = ((0, jsx_runtime_1.jsxs)(antd_1.Button, { onClick: star, children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "star" }), " Star ", badge] }));
        }
        return ((0, jsx_runtime_1.jsxs)("div", { style: { textAlign: "center" }, children: [btn, (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(A_1.default, { href: "/stars", children: "Your stars..." })] }));
    }
    function renderProjectLink() {
        if (githubOrg && githubRepo) {
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tooltip, { title: "Go to the top level of the repository.", placement: "right", children: [(0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "home" }), " GitHub Repository:", " "] }), (0, jsx_runtime_1.jsxs)(A_1.default, { href: `/github/${githubOrg}/${githubRepo}`, children: [githubOrg, "/", githubRepo] }), (0, jsx_runtime_1.jsx)("br", {})] }));
        }
        if (url) {
            let name, target;
            const i = url.indexOf("/");
            if (url.startsWith("gist")) {
                target = `https://gist.github.com/${url.slice(i + 1)}`;
                name = "GitHub Gist";
            }
            else {
                target = "https://" + url.slice(i + 1);
                name = "URL";
            }
            // NOTE: it could conceivable only be http:// display will work, but this
            // link will be wrong. I'm not going to worry about that.
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tooltip, { placement: "right", title: `This file is hosted at ${target}. Click to open in a new tab.`, children: [(0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "external-link" }), " ", name, ":", " "] }), (0, jsx_runtime_1.jsx)(A_1.default, { href: target, children: target }), (0, jsx_runtime_1.jsx)("br", {})] }));
        }
        return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("b", { children: "Project:" }), " ", (0, jsx_runtime_1.jsx)(project_link_1.default, { project_id: project_id, title: projectTitle }), (0, jsx_runtime_1.jsx)("br", {})] }));
    }
    function renderPathLink() {
        if (githubRepo) {
            const segments = url.split("/");
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tooltip, { placement: "right", title: "This is hosted on GitHub. Click to open GitHub in a new tab.", children: [(0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "external-link" }), " Path:", " "] }), (0, jsx_runtime_1.jsx)(A_1.default, { href: `https://github.com/${(0, path_1.join)(...segments.slice(1))}`, children: segments.length > 3
                            ? (0, path_1.join)(...segments.slice(3))
                            : (0, path_1.join)(...segments.slice(1)) }), (0, jsx_runtime_1.jsx)("br", {})] }));
        }
        if (url)
            return;
        return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("b", { children: "Path: " }), (0, jsx_runtime_1.jsx)(linked_path_1.default, { path: path, relativePath: relativePath, id: id, isDir: contents?.isdir }), (0, jsx_runtime_1.jsx)("br", {})] }));
    }
    return ((0, jsx_runtime_1.jsx)(customize_1.Customize, { value: customize, children: (0, jsx_runtime_1.jsxs)(layout_1.Layout, { title: (0, util_1.getTitle)({ path, relativePath }), top: projectAvatarImage ? ((0, jsx_runtime_1.jsx)(antd_1.Avatar, { shape: "square", size: 160, icon: (0, jsx_runtime_1.jsx)("img", { src: projectAvatarImage }), style: { float: "left", margin: "10px" } })) : undefined, children: [githubOrg && ((0, jsx_runtime_1.jsx)(avatar_1.default, { size: 96, name: githubOrg, style: { float: "right", marginLeft: "15px" } })), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { style: { float: "right" }, children: renderStar() }), signingUp && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { margin: "0 auto", maxWidth: "400px" }, type: "warning", message: (0, jsx_runtime_1.jsx)(in_place_sign_in_or_up_1.default, { title: "Star Shared Files", why: "to star this", onSuccess: () => {
                                    star();
                                    setSigningUp(false);
                                    router.reload();
                                } }) })), description?.trim() && ((0, jsx_runtime_1.jsx)(sanitized_markdown_1.default, { style: { marginBottom: "-1em" } /* -1em to undo it being a paragraph */, value: description })), renderProjectLink(), renderPathLink(), counter && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "eye" }), " Views:"] }), " ", (0, jsx_runtime_1.jsx)(badge_1.default, { count: counter }), (0, jsx_runtime_1.jsx)("br", {})] })), license && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("b", { children: "License:" }), " ", (0, jsx_runtime_1.jsx)(license_1.default, { license: license }), (0, jsx_runtime_1.jsx)("br", {})] })), visibility(), compute_image && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("b", { children: "Image:" }), " ", compute_image, (0, jsx_runtime_1.jsx)("br", {})] })), (0, jsx_runtime_1.jsx)(path_actions_1.default, { id: id, path: path, url: url, relativePath: relativePath, isDir: contents?.isdir, exclude: new Set(["hosted"]), project_id: project_id, image: compute_image, description: description })] }), (0, jsx_runtime_1.jsx)(antd_1.Divider, {}), error != null && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { showIcon: true, type: "error", style: { maxWidth: "700px", margin: "30px auto" }, message: "Error loading file", description: (0, jsx_runtime_1.jsxs)("div", { children: ["There was a problem loading", " ", relativePath ? relativePath : "this file", " in", " ", (0, jsx_runtime_1.jsx)(link_1.default, { href: `/share/public_paths/${id}`, children: (0, jsx_runtime_1.jsxs)("a", { children: [path, "."] }) }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), error] }) })), contents != null && ((0, jsx_runtime_1.jsx)(path_contents_1.default, { id: id, relativePath: relativePath, path: path, ...contents }))] }) }));
}
exports.default = PublicPath;
//# sourceMappingURL=path.js.map