"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
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 react_1 = require("react");
var antd_1 = require("antd");
var link_1 = __importDefault(require("next/link"));
var path_contents_1 = __importDefault(require("components/share/path-contents"));
var path_actions_1 = __importDefault(require("components/share/path-actions"));
var linked_path_1 = __importDefault(require("components/share/linked-path"));
var loading_1 = __importDefault(require("components/share/loading"));
var license_1 = __importDefault(require("components/share/license"));
var project_link_1 = __importDefault(require("components/share/project-link"));
var counter_1 = __importDefault(require("lib/share/counter"));
var layout_1 = require("components/share/layout");
var customize_1 = require("lib/share/customize");
var util_1 = require("lib/share/util");
var sanitized_markdown_1 = __importDefault(require("components/misc/sanitized-markdown"));
var badge_1 = __importDefault(require("components/misc/badge"));
var icon_1 = require("@cocalc/frontend/components/icon");
var ui_1 = require("@cocalc/util/consts/ui");
var post_1 = __importDefault(require("lib/api/post"));
var in_place_sign_in_or_up_1 = __importDefault(require("components/auth/in-place-sign-in-or-up"));
var router_1 = require("next/router");
var avatar_1 = __importDefault(require("components/share/proxy/avatar"));
var A_1 = __importDefault(require("components/misc/A"));
var path_1 = require("path");
function PublicPath(_a) {
    var id = _a.id, path = _a.path, url = _a.url, project_id = _a.project_id, projectTitle = _a.projectTitle, _b = _a.relativePath, relativePath = _b === void 0 ? "" : _b, description = _a.description, counter = _a.counter, compute_image = _a.compute_image, license = _a.license, contents = _a.contents, error = _a.error, customize = _a.customize, disabled = _a.disabled, unlisted = _a.unlisted, authenticated = _a.authenticated, _c = _a.stars, stars = _c === void 0 ? 0 : _c, isStarred0 = _a.isStarred, githubOrg = _a.githubOrg, githubRepo = _a.githubRepo, projectAvatarImage = _a.projectAvatarImage, redirect = _a.redirect;
    (0, counter_1.default)(id);
    var _d = __read((0, react_1.useState)(stars), 2), numStars = _d[0], setNumStars = _d[1];
    var _e = __read((0, react_1.useState)(isStarred0 !== null && isStarred0 !== void 0 ? isStarred0 : null), 2), isStarred = _e[0], setIsStarred = _e[1];
    (0, react_1.useEffect)(function () {
        setIsStarred(isStarred0);
    }, [isStarred0]);
    var _f = __read((0, react_1.useState)(false), 2), signingUp = _f[0], setSigningUp = _f[1];
    var router = (0, router_1.useRouter)();
    (0, react_1.useEffect)(function () {
        if (redirect) {
            router.push(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()] }));
        }
    }
    function star() {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        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.
                        return [4 /*yield*/, (0, post_1.default)("/public-paths/star", { id: id })];
                    case 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.
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    }
    function unstar() {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        setIsStarred(false);
                        setNumStars(numStars - 1);
                        return [4 /*yield*/, (0, post_1.default)("/public-paths/unstar", { id: id })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    }
    function renderStar() {
        var 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, __assign({ onClick: function () {
                    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.
        var btn;
        if (isStarred == true) {
            btn = ((0, jsx_runtime_1.jsxs)(antd_1.Button, __assign({ 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, __assign({ onClick: star }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "star" }), " Star ", badge] })));
        }
        return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { textAlign: "center" } }, { children: [btn, (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/stars" }, { children: "Your stars..." }))] })));
    }
    function renderProjectLink() {
        if (githubOrg && githubRepo) {
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tooltip, __assign({ 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, __assign({ href: "/github/".concat(githubOrg, "/").concat(githubRepo) }, { children: [githubOrg, "/", githubRepo] })), (0, jsx_runtime_1.jsx)("br", {})] })));
        }
        if (url) {
            var name_1, target = void 0;
            var i = url.indexOf("/");
            if (url.startsWith("gist")) {
                target = "https://gist.github.com/".concat(url.slice(i + 1));
                name_1 = "GitHub Gist";
            }
            else {
                target = "https://" + url.slice(i + 1);
                name_1 = "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, __assign({ placement: "right", title: "This file is hosted at ".concat(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_1, ":", " "] }), (0, jsx_runtime_1.jsx)(A_1.default, __assign({ 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) {
            var segments = url.split("/");
            return ((0, jsx_runtime_1.jsxs)(antd_1.Tooltip, __assign({ 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, __assign({ href: "https://github.com/".concat(path_1.join.apply(void 0, __spreadArray([], __read(segments.slice(1)), false))) }, { children: segments.length > 3
                            ? path_1.join.apply(void 0, __spreadArray([], __read(segments.slice(3)), false)) : path_1.join.apply(void 0, __spreadArray([], __read(segments.slice(1)), false)) })), (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 === null || contents === void 0 ? void 0 : contents.isdir }), (0, jsx_runtime_1.jsx)("br", {})] }));
    }
    return ((0, jsx_runtime_1.jsx)(customize_1.Customize, __assign({ value: customize }, { children: (0, jsx_runtime_1.jsxs)(layout_1.Layout, __assign({ title: (0, util_1.getTitle)({ path: path, relativePath: 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", __assign({ 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: function () {
                                    star();
                                    setSigningUp(false);
                                    router.reload();
                                } }) })), (description === null || description === void 0 ? void 0 : 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 === null || contents === void 0 ? void 0 : 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, __assign({ href: "/share/public_paths/".concat(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, __assign({ id: id, relativePath: relativePath, path: path }, contents)))] })) })));
}
exports.default = PublicPath;
//# sourceMappingURL=path.js.map