import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import classNames from '../../_virtual/index.js';
import Icon from '../Icon/Icon.js';
import Typography from '../Typography/Typography.js';
import { checkEmpty } from '../../utils/checkEmpty/checkEmpty.js';

const OverviewModal = ({ isOpen, onClose, isMaximized, width = '800px', height = '432px', header, children, zIndex = 999, icons, downloadFileIcon = false, showHeader = true, top = '0', overlay = true, downloadHandler, multiData = [], setSelectedVideo, customStyle, }) => {
    const [videos, setVideos] = useState(multiData);
    useEffect(() => {
        if (!checkEmpty(multiData)) {
            setVideos(multiData);
        }
    }, [multiData]);
    useEffect(() => {
        const handleKeyDown = (event) => {
            if (event.key === 'Escape') {
                onClose?.();
            }
        };
        if (isOpen) {
            document.addEventListener('keydown', handleKeyDown);
        }
        return () => {
            document.removeEventListener('keydown', handleKeyDown);
        };
    }, [isOpen, onClose]);
    if (!isOpen)
        return null;
    const isEmptyData = checkEmpty(videos);
    const singleClass = videos.length === 1 ? ' image-grid--single' : '';
    const handleVideoEnd = (index) => {
        setVideos((prev) => {
            const updated = prev.filter((_, i) => i !== index);
            if (updated.length === 0) {
                onClose?.();
            }
            return updated;
        });
    };
    const modalContent = (jsx("div", { className: "ff-overview-modal-overlay", style: {
            background: overlay ? 'var(--default-icon-color)' : undefined,
            zIndex,
        }, children: jsxs("div", { className: classNames('ff-overview-modal-container', {
                'ff-overview-maximized-container': isMaximized,
            }), style: {
                ...customStyle,
                '--modal-width': width,
                '--modal-height': height,
                '--modal-top': top,
            }, children: [showHeader && (jsxs("div", { className: "ff-overview-modal-header", children: [header, jsxs("div", { className: "ff-overview-modal-icons", children: [downloadFileIcon && (jsx(Icon, { width: 16, height: 16, name: "download_file", onClick: downloadHandler })), icons] })] })), jsx("div", { className: classNames('ff-overview-modal-body', {
                        'ff-overview-modal-body--centered': isEmptyData,
                    }), children: !isEmptyData ? (jsx("div", { className: `image-grid${singleClass}`, children: videos.map((video, idx) => (jsxs("div", { className: "ff-overview-modal-image-item", children: [jsx("video", { src: video.src, autoPlay: true, muted: true, playsInline: true, preload: "auto", controls: false, onEnded: () => handleVideoEnd(idx), className: "ff-overview-modal-video", children: "Your browser does not support the video tag." }), jsxs("div", { className: "ff-overview-modal-image-name", children: [jsx(Icon, { name: video.icon }), jsxs(Typography, { children: [video.machineName, " - ", video.scriptName] })] }), jsx(Icon, { width: 16, height: 16, className: "ff-overview-modal-image-action", name: "maximize_new", onClick: () => {
                                        const clickedVideo = {
                                            currentVideoData: { ...video, clickedAt: Date.now() },
                                            allVideoData: videos,
                                        };
                                        setSelectedVideo?.(clickedVideo);
                                    } }), jsxs(Typography, { color: "var(--ff-chip-bg)", className: "ff-overview-modal-scripts", children: [video.currentScripts, "/", video.totalScripts] })] }, idx))) })) : (children) })] }) }));
    return ReactDOM.createPortal(modalContent, document.body);
};

export { OverviewModal as default };
//# sourceMappingURL=overviewModal.js.map
