"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Media = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const microdataContext_1 = require("../../gravity-blocks/context/microdataContext/index.js");
const utils_1 = require("../../utils/index.js");
const microdata_1 = require("../../utils/microdata.js");
const VideoBlock_1 = tslib_1.__importDefault(require("../VideoBlock/VideoBlock.js"));
const DataLens_1 = tslib_1.__importDefault(require("./DataLens/DataLens.js"));
const FullscreenVideo_1 = tslib_1.__importDefault(require("./FullscreenVideo/FullscreenVideo.js"));
const Iframe_1 = tslib_1.__importDefault(require("./Iframe/Iframe.js"));
const Image_1 = tslib_1.__importDefault(require("./Image/Image.js"));
const Video_1 = tslib_1.__importDefault(require("./Video/Video.js"));
const b = (0, utils_1.block)('Media');
const Media = (props) => {
    const { image, video, youtube, videoIframe, dataLens, color, height, previewImg, parallax = false, fullscreen, isFullscreenImageCover, analyticsEvents, className, imageClassName, videoClassName, youtubeClassName, disableImageSliderForArrayInput, playVideo = true, isBackground, playButton, customBarControlsClassName, qa, ratio, autoplay, onImageLoad, onIntrinsicSizeChange, disablePlayerAutoSizing, iframe, margins, videoMicrodata, } = props;
    const [hasVideoFallback, setHasVideoFallback] = React.useState(false);
    const microdata = (0, microdataContext_1.useMicrodata)();
    const qaAttributes = (0, utils_1.getQaAttrubutes)(qa, 'video');
    const content = React.useMemo(() => {
        let result = [];
        if (image) {
            result.push((0, jsx_runtime_1.jsx)(Image_1.default, { parallax: parallax, image: image, disableImageSliderForArrayInput: disableImageSliderForArrayInput, height: height, imageClassName: imageClassName, fullscreenClassName: isFullscreenImageCover ? b('fullscreen-image-cover') : undefined, isBackground: isBackground, video: video, hasVideoFallback: hasVideoFallback, fullscreen: fullscreen, qa: qaAttributes.image, onLoad: onImageLoad, onIntrinsicSizeChange: onIntrinsicSizeChange }, "image"));
        }
        if (video) {
            const videoProps = {
                key: 'video',
                video,
                videoClassName,
                height,
                analyticsEvents,
                playVideo,
                previewImg,
                playButton,
                customBarControlsClassName,
                hasVideoFallback,
                setHasVideoFallback,
                disablePlayerAutoSizing,
                onIntrinsicSizeChange,
                ratio,
            };
            if (fullscreen) {
                result.push((0, jsx_runtime_1.jsx)(FullscreenVideo_1.default, { ...videoProps, qa: qaAttributes.video }));
            }
            else {
                result.push((0, jsx_runtime_1.jsx)(Video_1.default, { ...videoProps, qa: qaAttributes.video }));
            }
        }
        if (youtube || videoIframe) {
            result = ((0, jsx_runtime_1.jsx)(VideoBlock_1.default, { className: b('youtube', youtubeClassName), record: youtube, videoIframe: videoIframe, attributes: { color: 'white', rel: '0' }, previewImg: previewImg, height: height, fullscreen: fullscreen, analyticsEvents: analyticsEvents, autoplay: autoplay, onImageLoad: onImageLoad }));
        }
        if (dataLens) {
            result = (0, jsx_runtime_1.jsx)(DataLens_1.default, { dataLens: dataLens });
        }
        if (iframe) {
            result = (0, jsx_runtime_1.jsx)(Iframe_1.default, { iframe: iframe, margins: margins });
        }
        return result;
    }, [
        image,
        video,
        youtube,
        videoIframe,
        dataLens,
        iframe,
        parallax,
        disableImageSliderForArrayInput,
        height,
        imageClassName,
        isFullscreenImageCover,
        isBackground,
        hasVideoFallback,
        fullscreen,
        qaAttributes.image,
        qaAttributes.video,
        onImageLoad,
        onIntrinsicSizeChange,
        videoClassName,
        analyticsEvents,
        playVideo,
        previewImg,
        playButton,
        customBarControlsClassName,
        disablePlayerAutoSizing,
        ratio,
        youtubeClassName,
        autoplay,
        margins,
    ]);
    const videoMicrodataScript = React.useMemo(() => {
        const { name, description } = videoMicrodata || {};
        const json = JSON.stringify({
            '@context': 'http://schema.org/',
            '@type': 'VideoObject',
            uploadDate: microdata?.contentUpdatedDate,
            contentUrl: video?.src?.[0] || videoIframe || youtube,
            thumbnailUrl: previewImg,
            ...(videoMicrodata || {}),
            name: name ? (0, microdata_1.sanitizeMicrodata)(name) : name,
            description: description ? (0, microdata_1.sanitizeMicrodata)(description) : description,
        });
        return video || youtube || videoIframe ? ((0, jsx_runtime_1.jsx)("script", { type: "application/ld+json", dangerouslySetInnerHTML: { __html: json } })) : null;
    }, [microdata?.contentUpdatedDate, previewImg, video, videoIframe, videoMicrodata, youtube]);
    return ((0, jsx_runtime_1.jsxs)("div", { className: b(null, className), style: { backgroundColor: color }, "data-qa": qa, children: [videoMicrodataScript, content] }));
};
exports.Media = Media;
exports.default = exports.Media;
//# sourceMappingURL=Media.js.map
