import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { useMicrodata } from "../../gravity-blocks/context/microdataContext/index.js";
import { block, getQaAttrubutes } from "../../utils/index.js";
import { sanitizeMicrodata } from "../../utils/microdata.js";
import IframeVideoBlock from "../VideoBlock/VideoBlock.js";
import DataLens from "./DataLens/DataLens.js";
import FullscreenVideo from "./FullscreenVideo/FullscreenVideo.js";
import Iframe from "./Iframe/Iframe.js";
import Image from "./Image/Image.js";
import Video from "./Video/Video.js";
import './Media.css';
const b = block('Media');
export 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 = useMicrodata();
    const qaAttributes = getQaAttrubutes(qa, 'video');
    const content = React.useMemo(() => {
        let result = [];
        if (image) {
            result.push(_jsx(Image, { 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(_jsx(FullscreenVideo, { ...videoProps, qa: qaAttributes.video }));
            }
            else {
                result.push(_jsx(Video, { ...videoProps, qa: qaAttributes.video }));
            }
        }
        if (youtube || videoIframe) {
            result = (_jsx(IframeVideoBlock, { 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 = _jsx(DataLens, { dataLens: dataLens });
        }
        if (iframe) {
            result = _jsx(Iframe, { 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 ? sanitizeMicrodata(name) : name,
            description: description ? sanitizeMicrodata(description) : description,
        });
        return video || youtube || videoIframe ? (_jsx("script", { type: "application/ld+json", dangerouslySetInnerHTML: { __html: json } })) : null;
    }, [microdata?.contentUpdatedDate, previewImg, video, videoIframe, videoMicrodata, youtube]);
    return (_jsxs("div", { className: b(null, className), style: { backgroundColor: color }, "data-qa": qa, children: [videoMicrodataScript, content] }));
};
export default Media;
//# sourceMappingURL=Media.js.map
