'use strict';

var React = require('react');
var classnames = require('classnames');
var Glimmer = require('./Glimmer-cjs.js');
var Icon = require('./Icon-cjs.js');
var Typography = require('./Typography-cjs.js');

var styles$1 = {"content":"b9lhF-BkUnI-","hasName":"BJLJeUxloeQ-","large":"t5IzWH6-Sy0-","fileName":"tj--cPRjQfI-","base":"l2Vxo1qaR8U-","spinning":"LDAOyzklETo-"};

var styles = {"image":"qOmZU-rbNf4-","fadeIn":"zB1aNhsmmqY-","hidden":"_2bihNsPs7w4-","spinning":"ZLMkLuZeH-o-"};

function InternalThumbnailImage({ file, UNSAFE_className, UNSAFE_style, }) {
    const { name, src } = file;
    const [imageSource, setImageSource] = React.useState();
    const [imageLoaded, setImageLoaded] = React.useState(false);
    React.useEffect(() => {
        src().then(url => setImageSource(url));
    }, []);
    return (React.createElement(React.Fragment, null,
        !imageLoaded && React.createElement(Glimmer.Glimmer, { size: "auto" }),
        React.createElement("img", { src: imageSource, onError: file.onImageLoadError, onLoad: handleImageLoad, alt: name, "data-testid": "internalThumbnailImage", className: classnames(styles.image, { [styles.hidden]: !imageLoaded }, UNSAFE_className === null || UNSAFE_className === void 0 ? void 0 : UNSAFE_className.container), style: UNSAFE_style === null || UNSAFE_style === void 0 ? void 0 : UNSAFE_style.container })));
    function handleImageLoad() {
        setImageLoaded(true);
    }
}

// This function comes from MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator
function getClientBrowser(userAgent) {
    // The order matters here, and this may report false positives for unlisted browsers.
    if (userAgent.includes("Firefox")) {
        // "Mozilla/5.0 (X11; Linux i686; rv:104.0) Gecko/20100101 Firefox/104.0"
        return "Mozilla Firefox";
    }
    else if (userAgent.includes("SamsungBrowser")) {
        // "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G955F Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/9.4 Chrome/67.0.3396.87 Mobile Safari/537.36"
        return "Samsung Internet";
    }
    else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
        // "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 OPR/90.0.4480.54"
        return "Opera";
    }
    else if (userAgent.includes("Edge")) {
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"
        return "Microsoft Edge (Legacy)";
    }
    else if (userAgent.includes("Edg")) {
        // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 Edg/104.0.1293.70"
        return "Microsoft Edge (Chromium)";
    }
    else if (userAgent.includes("Chrome")) {
        // "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
        return "Google Chrome or Chromium";
    }
    else if (userAgent.includes("Safari")) {
        // "Mozilla/5.0 (iPhone; CPU iPhone OS 15_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Mobile/15E148 Safari/604.1"
        return "Apple Safari";
    }
    else {
        return "unknown";
    }
}
function isSafari(userAgent) {
    return getClientBrowser(userAgent) === "Apple Safari";
}

// Allowlist rather than denylist so unrecognized types fail safe (fall back to
// a non-image preview) instead of handing a broken file to an <img>.
const PREVIEWABLE_TYPES = new Set([
    "image/jpeg",
    "image/jpg",
    "image/png",
    "image/gif",
    "image/webp",
    "image/bmp",
    "image/avif",
]);
const SAFARI_ONLY_TYPES = new Set(["image/heic", "image/heif"]);
function isPreviewableImage(type) {
    if (!type)
        return false;
    const normalizedType = type.toLowerCase();
    if (PREVIEWABLE_TYPES.has(normalizedType))
        return true;
    if (SAFARI_ONLY_TYPES.has(normalizedType)) {
        const userAgent = typeof document === "undefined" ? "" : window.navigator.userAgent;
        return isSafari(userAgent);
    }
    return false;
}

function Thumbnail({ compact = false, size, file, UNSAFE_className, UNSAFE_style, }) {
    const { name, type } = file;
    const iconName = getIconNameFromType(type);
    const hasName = Boolean(name) && compact;
    if (isPreviewableImage(type)) {
        return (React.createElement(InternalThumbnailImage, { file: file, UNSAFE_className: UNSAFE_className, UNSAFE_style: UNSAFE_style }));
    }
    return (React.createElement("div", { className: classnames(styles$1.content, styles$1[size], {
            [styles$1.hasName]: hasName,
        }, UNSAFE_className === null || UNSAFE_className === void 0 ? void 0 : UNSAFE_className.container), style: UNSAFE_style === null || UNSAFE_style === void 0 ? void 0 : UNSAFE_style.container },
        React.createElement(Icon.Icon, { name: iconName, color: "greyBlue", size: size }),
        hasName && (React.createElement("div", { className: styles$1.fileName },
            React.createElement(Typography.Typography, { element: "span", textColor: "text", numberOfLines: 1 }, name)))));
}
function getIconNameFromType(mimeType) {
    if (mimeType.startsWith("image/"))
        return "image";
    if (mimeType.startsWith("video/"))
        return "video";
    switch (mimeType) {
        case "application/pdf":
            return "pdf";
        case "application/vnd.ms-excel":
            return "excel";
        default:
            return "file";
    }
}

exports.Thumbnail = Thumbnail;
exports.isPreviewableImage = isPreviewableImage;
