"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.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 });
const react_1 = __importStar(require("react"));
const PeerView_1 = __importDefault(require("./PeerView"));
/* import AloneHere from '@src/components/AloneHere';
import WaitingRoomMessage from '@src/components/WaitingRoomMessage'; */
const ConfigProvider_1 = require("../../utils/ConfigProvider");
const components_1 = require("../../themes/components");
const react_native_1 = require("react-native");
const Pagination_1 = __importDefault(require("../Pagination"));
const react_redux_1 = require("react-redux");
const OrientationContext_1 = require("../../services/OrientationContext");
const peersPerColumn = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(2),
    5: Array(5).fill(2),
    4: Array(4).fill(2),
    3: [1, 2, 2],
    2: [1, 1],
    1: [1],
};
const peersPerRowMobile = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(2),
    5: [2, ...Array(4).fill(2)],
    4: Array(4).fill(2),
    3: [1, 2, 2],
    2: [1, 1],
    1: [1],
};
const peersPerRow = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(3),
    5: [...Array(2).fill(2), ...Array(3).fill(3)],
    4: Array(4).fill(2),
    3: Array(3).fill(2),
    2: Array(2).fill(2),
    1: [1],
};
/*  Config for mobile, heights for different grid configurations
    */
const peersPerColumnMobile = {
    9: Array(9).fill(3),
    8: Array(9).fill(3),
    7: Array(9).fill(3),
    6: Array(6).fill(3),
    5: [...Array(2).fill(2), ...Array(3).fill(3)],
    4: Array(4).fill(2),
    3: Array(3).fill(2),
    2: Array(2).fill(2),
    1: [1],
};
const MultiSpeakerGridLayout = (props) => {
    const { sessionConfig } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const windowDimensions = react_native_1.useWindowDimensions();
    let { width, height } = sessionConfig.theme.dimensions || windowDimensions;
    const isMobile = width < 1024;
    const { orientation, orientationType } = react_1.default.useContext(OrientationContext_1.OrientationContext);
    const mobileLandscape = orientation === 'LANDSCAPE';
    const gridView = react_redux_1.useSelector((state) => state.room.gridView);
    const { peers, waitingRoom, widthOffset, } = props;
    let heightOffset = 0;
    // Fixed height header and control bars 56px Header + 56px Control bar
    if (sessionConfig.theme.header.is_enabled) {
        heightOffset -= 56;
    }
    if (sessionConfig.theme.control_bar.is_enabled
        && (!waitingRoom || sessionConfig.theme.waiting_room.enable_preview)) {
        heightOffset -= 54;
    }
    // for mobile landscape, the parent grid height width are reverse, and rotated 90 degree,
    // but we want the video to not be rotated, so mobile width is subtracted with header, controlbar
    if (mobileLandscape) {
        // [height,width] = [width,height];
        height += widthOffset;
        width += heightOffset + 72;
    }
    else {
        height += heightOffset;
        width -= widthOffset;
    }
    const [paginationHeightToSubtract, setPaginationHeightToSubtract] = react_1.default.useState(0);
    const isAlone = peers.length === 1;
    // show alone component when isAlone, not on mobile
    // and not in waiting room, and not disabled by config
    const showAloneComponent = isAlone && !isMobile && !waitingRoom;
    // when in waitingRoom, extra grid items would be 0 if
    // on mobile if showSelfVideo is not set false, othewise 1
    const waitingOffset = waitingRoom
        && (!isMobile || !sessionConfig.theme.waiting_room.enable_preview);
    // if either alone component or waiting component is there, no. of grid +1
    const peerOffset = (showAloneComponent || waitingOffset) && 1;
    // total number of grid items
    let videoGridNumber = peers.length + peerOffset;
    if (videoGridNumber > 6) {
        videoGridNumber = 9;
    }
    if (gridView === 'multi_alternate' && peers.length !== 1) {
        videoGridNumber -= 1;
    }
    const joinedPeers = react_redux_1.useSelector((state) => state.peers.joinedPeers);
    const noOfPages = Math.ceil(((joinedPeers.length + 1) / 6));
    react_1.default.useEffect(() => {
        if (noOfPages > 1) {
            if (peers.length === 1) {
                setPaginationHeightToSubtract(16); // 2 + 16 + 18
            }
            else if (peers.length <= 4) {
                setPaginationHeightToSubtract(7); // 4 + 14 = 18
            }
            else if (peers.length === 5) {
                setPaginationHeightToSubtract(12);
            }
            else {
                setPaginationHeightToSubtract(4); // 6 + 12 = 18
            }
        }
    }, [peers, noOfPages]);
    const calcHeightDivisions = (divisions, padding) => Math.floor((height - 6) / divisions);
    const calcWidthDivisions = (divisions, padding) => Math.floor((width - 6) / divisions);
    const getHeight = (videoGridNum, index) => {
        if (isMobile && !mobileLandscape) {
            return calcHeightDivisions(peersPerColumnMobile[videoGridNum][index], 3);
        }
        return calcHeightDivisions(peersPerColumn[videoGridNum][index], mobileLandscape ? 3 : 5);
    };
    const getWidth = (videoGridNum, index) => {
        if (isMobile && !mobileLandscape) {
            return calcWidthDivisions(peersPerRowMobile[videoGridNum][index], 3);
        }
        return calcWidthDivisions(peersPerRow[videoGridNum][index], mobileLandscape ? 3 : 5);
    };
    const peersMap = () => peers.map((peer, index) => {
        if (gridView === 'multi_alternate' && peer.isMe && peers.length !== 1)
            return null;
        if (peer) {
            const height = getHeight(videoGridNumber, index + peerOffset) - paginationHeightToSubtract;
            return (react_1.default.createElement(PeerView_1.default, { key: `peer_${peer.id}`, height: height - (mobileLandscape ? 2 : 0), width: getWidth(videoGridNumber, index + peerOffset), peer: peer, totalPeers: peers.length, isMobile: isMobile }));
        }
        return null;
    });
    // eslint-disable-next-line no-nested-ternary
    const direction = mobileLandscape ? (videoGridNumber === 5 ? 'row' : 'column') : (videoGridNumber === 5 ? 'column' : 'row');
    return (react_1.default.createElement(components_1.Flex, { position: "relative", height: height, width: width, bg: "secondary.1000", marginLeft: mobileLandscape
            && orientationType !== 'landscapeLeft'
            && sessionConfig.theme.header.is_enabled ? 56 : 0, padding: 3, pt: 0 },
        react_1.default.createElement(components_1.Flex, { direction: direction, flexWrap: "wrap", justify: "center" }, (!waitingRoom || (sessionConfig.theme.waiting_room.enable_preview && !isMobile))
            ? (react_1.default.createElement(react_1.default.Fragment, null, peersMap())) : null),
        react_1.default.createElement(components_1.Flex, { width: width, justify: "center", position: "absolute", bottom: "2" },
            react_1.default.createElement(Pagination_1.default, null))));
};
MultiSpeakerGridLayout.defaultProps = {
    waitingRoom: false,
    widthOffset: 0,
};
exports.default = MultiSpeakerGridLayout;
//# sourceMappingURL=MultiSpeakerGridLayout.js.map