"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 __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 util_1 = require("lib/share/util");
var file_extensions_1 = require("@cocalc/frontend/file-extensions");
var raw_url_1 = __importDefault(require("lib/share/raw-url"));
var feature_1 = require("lib/share/feature");
var codemirror_1 = __importDefault(require("./codemirror"));
var sage_worksheet_1 = __importDefault(require("./sage-worksheet"));
var nbviewer_1 = __importDefault(require("@cocalc/frontend/jupyter/nbviewer/nbviewer"));
var static_markdown_1 = __importDefault(require("@cocalc/frontend/editors/slate/static-markdown"));
var index_1 = __importDefault(require("@cocalc/frontend/frame-editors/whiteboard-editor/share/index"));
var html_ssr_1 = __importDefault(require("@cocalc/frontend/components/html-ssr"));
var A_1 = __importDefault(require("components/misc/A"));
var util_2 = require("lib/share/util");
var url_transform_1 = __importDefault(require("lib/share/url-transform"));
var anchor_tag_component_1 = __importDefault(require("./anchor-tag-component"));
var file_context_1 = require("@cocalc/frontend/lib/file-context");
function FileContents(_a) {
    var id = _a.id, content = _a.content, path = _a.path, relativePath = _a.relativePath;
    var filename = relativePath ? relativePath : path;
    var ext = (0, util_1.getExtension)(filename);
    var raw = (0, raw_url_1.default)({ id: id, path: path, relativePath: relativePath });
    var withFileContext = function (x) {
        var relPath = (0, util_2.containingPath)(relativePath);
        var value = {
            urlTransform: (0, url_transform_1.default)({ id: id, path: path, relativePath: relPath }),
            AnchorTagComponent: (0, anchor_tag_component_1.default)({ id: 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, __assign({ 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", __assign({ style: { textAlign: "center", margin: "30px" } }, { children: (0, jsx_runtime_1.jsx)(A_1.default, __assign({ 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", __assign({ style: { textAlign: "center", margin: "30px" } }, { children: [(0, jsx_runtime_1.jsx)(A_1.default, __assign({ 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)) {
        return withFileContext((0, jsx_runtime_1.jsx)(html_ssr_1.default, { 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