"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 no-underscore-dangle */
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const store_1 = require("../store/store");
const GridManager_1 = require("../components/video/GridManager");
const react_native_1 = require("react-native");
const ConfirmExitModal_1 = __importDefault(require("../components/ConfirmExitModal"));
const HostWaitlistManagerModal_1 = __importDefault(require("../components/HostWaitlistManagerModal"));
const ConfigProvider_1 = require("../utils/ConfigProvider");
const browser_utils_1 = __importStar(require("../utils/browser-utils"));
const components_1 = require("../themes/components");
const StageHeader_1 = __importDefault(require("../components/StageHeader"));
const StageControlBar_1 = __importDefault(require("../components/StageControlBar"));
const RequestToJoinStageModal_1 = __importDefault(require("../components/RequestToJoinStageModal"));
const StartPresentingModal_1 = __importDefault(require("../components/StartPresentingModal"));
const PIPModeContainer_1 = __importDefault(require("../components/video/PIPModeContainer"));
const StageService_1 = __importDefault(require("../services/StageService"));
const MediaRoomService_1 = require("../services/MediaRoomService");
const roomSlice_1 = require("../store/roomSlice");
const participantSlice_1 = require("../store/participantSlice");
const newTypes_1 = require("../types/newTypes");
const { DyteHelper } = react_native_1.NativeModules;
function Stage(props) {
    const dispatch = react_redux_1.useDispatch();
    const { isOpen, onClose, onOpen } = browser_utils_1.useDisclosure();
    const isMobile = true;
    const { roomName } = props;
    const { sessionConfig, meeting } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const roomDisplayTitle = react_redux_1.useSelector((state) => state.room.roomDisplayTitle);
    const waitlistedPeers = react_redux_1.useSelector((state) => state.room.waitlistedPeers);
    const joinedPeers = react_redux_1.useSelector((state) => state.peers.joinedPeers);
    const plugins = react_redux_1.useSelector((state) => state.plugin);
    const windowDimensions = react_native_1.useWindowDimensions();
    const sharingScreen = react_redux_1.useSelector((state) => state.participant.sharingScreen);
    const { height, width } = sessionConfig.theme.dimensions || windowDimensions;
    const [hasCopied, onCopy] = browser_utils_1.default(`/stage/${roomName}`);
    const { mediaRoom } = react_1.useContext(MediaRoomService_1.MediaRoomContext);
    const joinToStageState = react_redux_1.useSelector((state) => state.participant.requestToJoinStageState);
    const kicked = react_redux_1.useSelector((state) => state.room.kicked);
    react_1.useEffect(() => {
        // remove when ios native module for the same is implemented
        if (react_native_1.Platform.OS === 'android') {
            DyteHelper.activateScreenWake();
            return () => DyteHelper.deactivateScreenWake();
        }
        return () => { };
    });
    react_1.useEffect(() => {
        const listener = react_native_1.BackHandler.addEventListener('hardwareBackPress', () => {
            // If ConfirmExitModal is already open, exit
            if (isOpen) {
                return false;
            }
            onOpen();
            return true;
        });
        return () => {
            if (listener) {
                listener.remove();
            }
            else {
                react_native_1.BackHandler.removeEventListener('hardwareBackPress', () => null);
            }
        };
    }, []);
    const onLeaveMeetingCalled = () => {
        setTimeout(() => {
            DyteHelper.stopService();
            if (sharingScreen) {
                dispatch(roomSlice_1.setScreenShareRequested(false));
                mediaRoom.disableScreenShare();
            }
        });
        if (joinToStageState === participantSlice_1.RequestToJoinStageState.ACCEPTED) {
            mediaRoom.removePeerFromStage(mediaRoom.peerid, newTypes_1.WebinarAcceptRequestType.REQUESTED_BY_PARTICIPANT);
        }
        else {
            mediaRoom.stopPresenting();
            dispatch(participantSlice_1.stoppedPresenting());
        }
        meeting.emit(meeting.Events.disconnect);
        meeting.emit(meeting.Events.meetingEnded, { kicked });
        mediaRoom === null || mediaRoom === void 0 ? void 0 : mediaRoom.stopAllProducers();
        dispatch(store_1.resetEverything());
        if (isOpen) {
            onClose();
        }
    };
    react_1.useEffect(() => {
        if (meeting) {
            meeting.leaveRoom = onLeaveMeetingCalled;
        }
    }, [meeting]);
    react_1.useEffect(() => {
        if (kicked) {
            onLeaveMeetingCalled();
        }
    }, [kicked]);
    react_1.useEffect(() => {
        const ne = new react_native_1.NativeEventEmitter(DyteHelper);
        const lesveRoomListener = ne.addListener('LEAVE_ROOM', () => {
            onLeaveMeetingCalled();
        });
        return () => {
            lesveRoomListener.remove();
        };
    }, []);
    return (react_1.default.createElement(components_1.Flex, { direction: "column", bg: "secondary.1000", height: height, width: width },
        react_1.default.createElement(components_1.Flex, { direction: "column", width: width, height: height },
            react_1.default.createElement(StageService_1.default, null),
            react_1.default.createElement(ConfirmExitModal_1.default, { isOpen: isOpen, onClose: onClose, onLeave: onLeaveMeetingCalled }),
            react_1.default.createElement(HostWaitlistManagerModal_1.default, { isOpen: waitlistedPeers.length > 0, peers: waitlistedPeers }),
            sessionConfig.theme.header.is_enabled && (react_1.default.createElement(StageHeader_1.default, { title: roomDisplayTitle !== ''
                    ? roomDisplayTitle
                    : roomName, linkCopied: hasCopied, copyLink: onCopy, width: width, height: height, noOfParticipants: joinedPeers.filter((v, i, a) => a.findIndex((t) => t.id === v.id) === i).length })),
            react_1.default.createElement(PIPModeContainer_1.default, { isMobile: isMobile }),
            react_1.default.createElement(GridManager_1.GridManager, { widthOffset: 0, singleSpeakerView: plugins.main.length > 0 && !isMobile }),
            react_1.default.createElement(RequestToJoinStageModal_1.default, { isMobile: isMobile }),
            react_1.default.createElement(StartPresentingModal_1.default, null),
            sessionConfig.theme.control_bar.is_enabled && (react_1.default.createElement(StageControlBar_1.default, { linkCopied: hasCopied, copyLink: onCopy, isMobile: isMobile, leaveCall: onOpen })))));
}
exports.default = Stage;
//# sourceMappingURL=Stage.js.map