"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 util_1 = require("lib/share/util");
const file_extensions_1 = require("@cocalc/frontend/file-extensions");
const raw_url_1 = __importDefault(require("lib/share/raw-url"));
const feature_1 = require("lib/share/feature");
const codemirror_1 = __importDefault(require("./codemirror"));
const sage_worksheet_1 = __importDefault(require("./sage-worksheet"));
const nbviewer_1 = __importDefault(require("@cocalc/frontend/jupyter/nbviewer/nbviewer"));
const static_markdown_1 = __importDefault(require("@cocalc/frontend/editors/slate/static-markdown"));
const index_1 = __importDefault(require("@cocalc/frontend/frame-editors/whiteboard-editor/share/index"));
//import HTML from "@cocalc/frontend/components/html-ssr";
const A_1 = __importDefault(require("components/misc/A"));
const util_2 = require("lib/share/util");
const url_transform_1 = __importDefault(require("lib/share/url-transform"));
const anchor_tag_component_1 = __importDefault(require("./anchor-tag-component"));
const file_context_1 = require("@cocalc/frontend/lib/file-context");
function FileContents({ id, content, path, relativePath, }) {
    const filename = relativePath ? relativePath : path;
    const ext = (0, util_1.getExtension)(filename);
    const raw = (0, raw_url_1.default)({ id, path, relativePath });
    const withFileContext = (x) => {
        const relPath = (0, util_2.containingPath)(relativePath);
        const value = {
            urlTransform: (0, url_transform_1.default)({ id, path, relativePath: relPath }),
            AnchorTagComponent: (0, anchor_tag_component_1.default)({ id, relativePath: relPath }),
            noSanitize: false, // We **MUST** sanitize, since we users could launch XSS attacks, mess up style, etc.,
            // This will, of course, break things, in which case users will have to open them in their own projects.
        };
        return (0, jsx_runtime_1.jsx)(file_context_1.FileContext.Provider, { value: value, children: x });
    };
    if ((0, file_extensions_1.isImage)(ext)) {
        return (0, jsx_runtime_1.jsx)("img", { src: raw, style: { maxWidth: "100%" } });
    }
    else if ((0, file_extensions_1.isVideo)(ext)) {
        return ((0, jsx_runtime_1.jsx)("video", { controls: true, autoPlay: true, loop: true, style: { width: "100%", height: "auto" }, src: raw }));
    }
    else if ((0, file_extensions_1.isAudio)(ext)) {
        return (0, jsx_runtime_1.jsx)("audio", { src: raw, autoPlay: true, controls: true, loop: false });
    }
    else if (ext == "pdf") {
        // iOS and iPADOS does not have any way to embed PDF's in pages.
        // I think pretty much every other web browser does, though
        // strangely even desktop Safari seems often broken, so we also block that.
        // Amazingly, nextjs handles this sort of thing fine!
        return (0, feature_1.isIOS)() || (0, feature_1.isSafari)() ? ((0, jsx_runtime_1.jsx)("h1", { style: { textAlign: "center", margin: "30px" }, children: (0, jsx_runtime_1.jsx)(A_1.default, { href: raw, external: true, children: "View this PDF..." }) })) : ((0, jsx_runtime_1.jsx)("embed", { style: { width: "100%", height: "100vh" }, src: raw, type: "application/pdf" }));
    }
    else if (content == null) {
        return ((0, jsx_runtime_1.jsxs)("h1", { style: { textAlign: "center", margin: "30px" }, children: [(0, jsx_runtime_1.jsx)(A_1.default, { href: raw, external: true, children: "Open or Download..." }), " "] }));
    }
    else if ((0, file_extensions_1.isCodemirror)(ext)) {
        return (0, jsx_runtime_1.jsx)(codemirror_1.default, { content: content, filename: filename });
    }
    else if ((0, file_extensions_1.isMarkdown)(ext)) {
        return withFileContext((0, jsx_runtime_1.jsx)(static_markdown_1.default, { value: content }));
    }
    else if ((0, file_extensions_1.isHTML)(ext)) {
        // We use a sandboxed iframe since it is much more likely to be
        // useful to users than our HTML component.  Most use of our
        // HTML component with math rendering, etc., is much better done
        // via a Markdown file.  This makes it easy to show, e.g.,
        // static k3d plots, which CUP is doing.  HTML files tend to
        // be independent of cocalc anyways.
        return ((0, jsx_runtime_1.jsx)("iframe", { srcDoc: content, style: { width: "100%", height: "100vh" }, sandbox: "allow-scripts" }));
        //     return withFileContext(
        //       <HTML value={content} style={{ width: "100%", height: "100vh" }} />
        //     );
    }
    else if (ext == "sagews") {
        return withFileContext((0, jsx_runtime_1.jsx)(sage_worksheet_1.default, { content: content }));
    }
    else if (ext == "ipynb") {
        return withFileContext((0, jsx_runtime_1.jsx)(nbviewer_1.default, { content: content }));
    }
    else if (ext == "board") {
        return withFileContext((0, jsx_runtime_1.jsx)(index_1.default, { content: content }));
    }
    return (0, jsx_runtime_1.jsx)("pre", { children: content });
}
exports.default = FileContents;
//# sourceMappingURL=file-contents.js.map