"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable react/jsx-props-no-spreading */
/* eslint-disable no-param-reassign */
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const browser_utils_1 = require("../../utils/browser-utils");
const components_1 = require("../../themes/components");
const PeerView_1 = __importDefault(require("./PeerView"));
const PluginWrapper_1 = __importDefault(require("../../services/PluginWrapper"));
const ConfigProvider_1 = require("../../utils/ConfigProvider");
const PeersService_1 = require("../../services/PeersService");
const OrientationContext_1 = require("../../services/OrientationContext");
const react_native_draggable_1 = __importDefault(require("react-native-draggable"));
const getSizesMobile = (width, height, totalPeers) => {
    let rows = 1;
    let hideBadge = false;
    if (totalPeers < 3)
        totalPeers = 3;
    if (totalPeers > 6 || (totalPeers > 4 && width > 480)) {
        rows = Math.ceil(totalPeers / 4);
        totalPeers = 4;
    }
    else if (totalPeers > 3 && width <= 480) {
        rows = 2;
        totalPeers = 3;
    }
    let peerSize = Math.floor(width / totalPeers);
    const ratio = width + 30 > height ? 0.8 : 0.65;
    const minHeightMainContent = height * ratio;
    const calcMainContentHeight = height - rows * peerSize;
    if (calcMainContentHeight < minHeightMainContent) {
        peerSize = (height * (1 - ratio)) / rows;
        rows = Math.ceil((peerSize * totalPeers) / width);
    }
    if (peerSize < 100) {
        hideBadge = true;
    }
    return {
        mainViewHeightOffset: rows * peerSize,
        videoSizeMobile: peerSize - 12,
        hideBadge,
    };
};
const SingleSpeakerGridLayout = (props) => {
    const { widthOffset, sharingScreen, peers: inputPeers } = props;
    const { sessionConfig } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const { screenSharePeer } = react_1.useContext(PeersService_1.PeersServiceContext);
    const windowDimensions = browser_utils_1.useWindowDimensions();
    let { width, height } = sessionConfig.theme.dimensions || windowDimensions;
    // const activeSpeakerPeerId = useSelector((state: RootState) => state.room.activeSpeakerPeerId);
    const isMobile = width < 1024;
    const { orientation, orientationType } = react_1.default.useContext(OrientationContext_1.OrientationContext);
    const mobileLandscape = orientation === 'LANDSCAPE';
    let heightOffset = 0;
    if (mobileLandscape) {
        // [height,width] = [width,height];
        width += heightOffset + 72;
    }
    // Fixed height header and control bars 40px Header + 64px Control bar
    if (sessionConfig.theme.header.is_enabled) {
        heightOffset -= 72;
    }
    if (sessionConfig.theme.control_bar.is_enabled) {
        heightOffset -= 54;
    }
    if (mobileLandscape) {
        width += heightOffset;
    }
    else {
        height += heightOffset;
        width -= widthOffset;
    }
    const activeVideoHeightDesktop = height - 32;
    // Constant width of peer videos
    const peerVideoWidthDesktop = 250;
    const remainingWidthDesktop = width - peerVideoWidthDesktop - 16;
    const { mainViewHeightOffset, videoSizeMobile, hideBadge } = getSizesMobile(width, height, inputPeers.length);
    const plugins = react_redux_1.useSelector((state) => state.plugin);
    /**
     * Input peers is reordered to include the pinned/screensharing peer at the start
     */
    const peers = [];
    let screenSharingPeer;
    let pinnedPeer;
    let activeSpeakerPeer;
    inputPeers.forEach((peer) => {
        /*
        if (peer.id === activeSpeakerPeerId) {
            activeSpeakerPeer = peer;
        } */
        if (peer.isScreensharing) {
            screenSharingPeer = peer;
            peers.unshift(peer);
        }
        else if (peer.isPinned) {
            pinnedPeer = peer;
            peers.unshift(peer);
        }
        else {
            peers.push(peer);
        }
    });
    const activePeer = screenSharePeer ||
        pinnedPeer ||
        activeSpeakerPeer ||
        peers[peers.length - 1];
    let mainViewComponent;
    if (plugins.main.length > 0) {
        const frame = plugins.main[0];
        mainViewComponent = (react_1.default.createElement(PluginWrapper_1.default, { height: height + 56, width: width - 20, frame: frame }));
    }
    else if (mobileLandscape) {
        mainViewComponent = (react_1.default.createElement(PeerView_1.default, { height: height - 16, width: width - 32, contain: activePeer.screenshare ||
                sessionConfig.theme.grid.single.videoFit === 'contain', peer: activePeer, totalPeers: peers.length, isMobile: true }));
    }
    else if (isMobile) {
        mainViewComponent = (react_1.default.createElement(PeerView_1.default, { contain: activePeer.screenshare ||
                sessionConfig.theme.grid.single.videoFit === 'contain', height: height - mainViewHeightOffset - 16, width: width - 16, totalPeers: peers.length, peer: activePeer, isMobile: true }));
    }
    else {
        mainViewComponent = (react_1.default.createElement(PeerView_1.default, { totalPeers: peers.length, contain: activePeer.screenshare ||
                sessionConfig.theme.grid.single.videoFit === 'contain', height: activeVideoHeightDesktop, width: remainingWidthDesktop - 64, peer: activePeer }));
    }
    const transform = `rotate(${mobileLandscape ? 90 : 0}deg)`;
    const transformOrigin = `${Math.floor(height / 2)}px ${Math.floor(height / 2)}px`;
    const renderHiddenPeers = () => peers.map((peer) => {
        const newPeer = Object.assign({}, peer);
        newPeer.videoVisible = false;
        newPeer.videoTrack = null;
        return (react_1.default.createElement(components_1.Box, { key: `${peer.id}_video`, position: "absolute", zIndex: -5, top: 100, width: 2, height: 2, left: 100, visibility: "hidden" },
            react_1.default.createElement(PeerView_1.default, { width: 1, height: 1, peer: newPeer, totalPeers: peers.length })));
    });
    const renderPluginPeer = () => {
        const frame = plugins.main[0];
        const activePlugin = plugins.plugins.find((p) => p.id === frame.plugin);
        const pluginPeer = peers.find((p) => p.userId === (activePlugin === null || activePlugin === void 0 ? void 0 : activePlugin.enabledBy));
        if (!pluginPeer)
            return null;
        return (react_1.default.createElement(react_native_draggable_1.default, { x: 0, y: 0, minX: 0, minY: 0, maxX: width - 80, maxY: height - 6, z: 20 },
            react_1.default.createElement(PeerView_1.default, { width: 100, height: 80, hideBadge: true, peer: pluginPeer, totalPeers: peers.length })));
    };
    const renderScreensharingPeer = () => {
        if (!screenSharingPeer || !mobileLandscape)
            return null;
        return (react_1.default.createElement(react_native_draggable_1.default, { x: 0, y: 0, minX: 0, minY: 0, maxX: width - 80, maxY: height - 6, z: 20 },
            react_1.default.createElement(PeerView_1.default, { width: 120, height: 100, hideBadge: true, peer: screenSharingPeer, totalPeers: peers.length })));
    };
    if (isMobile) {
        if (plugins.main.length > 0 && !mobileLandscape) {
            return (react_1.default.createElement(components_1.Flex, { height: height + 56, width: width - 2, marginTop: 10, flexDirection: "column", backgroundColor: "secondary.1000" },
                react_1.default.createElement(components_1.Box, { alignItems: "center", justifyContent: "center", mt: 0 },
                    mainViewComponent,
                    renderPluginPeer())));
        }
        return (react_1.default.createElement(components_1.Flex, { height: height, width: width, flexDirection: mobileLandscape ? 'row' : 'column', marginLeft: mobileLandscape && orientationType !== 'landscapeLeft'
                ? 56
                : 0, paddingLeft: mobileLandscape ? 20 : 0, backgroundColor: "secondary.1000" },
            react_1.default.createElement(components_1.Box, { alignItems: "center", justifyContent: "center", mt: mobileLandscape ? 0 : 8, ml: mobileLandscape ? -8 : 0 },
                mainViewComponent,
                renderScreensharingPeer(),
                renderPluginPeer()),
            !mobileLandscape && (react_1.default.createElement(components_1.Flex, { mt: 5, className: "dyte-client-noScrollbar", justify: "center", flexWrap: "wrap", direction: "row" }, peers.map((peer) => {
                if (!activePeer.screenshare &&
                    plugins.main.length === 0 &&
                    peer.id === activePeer.id)
                    return null;
                return (react_1.default.createElement(PeerView_1.default, { width: videoSizeMobile, height: videoSizeMobile, peer: peer, isMobile: true, hideBadge: hideBadge, totalPeers: peers.length, key: `${peer.id}_video` }));
            }))),
            mobileLandscape && renderHiddenPeers()));
    }
    return null;
};
exports.default = SingleSpeakerGridLayout;
//# sourceMappingURL=SingleSpeakerGridLayout.js.map