"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const roomSlice_1 = require("../store/roomSlice");
const MediaRoomService_1 = require("./MediaRoomService");
const UserTrackProvider_1 = require("./UserTrackProvider");
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const ConfigProvider_1 = require("../utils/ConfigProvider");
const components_1 = require("../themes/components");
const StageService = (props) => {
    const dispatch = react_redux_1.useDispatch();
    const { processPreset, meeting } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const producersState = react_redux_1.useSelector((state) => state.room.producersState);
    const legacyMode = react_redux_1.useSelector((state) => state.room.legacyMode);
    const joined = react_redux_1.useSelector((state) => state.room.joined);
    const waitingState = react_redux_1.useSelector((state) => state.room.waitingState);
    const dynPreset = react_redux_1.useSelector((state) => state.participant.preset);
    const audioMuted = react_redux_1.useSelector((state) => state.participant.audioMuted);
    const { mediaRoom } = react_1.useContext(MediaRoomService_1.MediaRoomContext);
    const { mediaTracks: { audioTrack, webcamTrack, screenShareVideoTrack }, } = react_1.useContext(UserTrackProvider_1.UserTrackContext);
    react_1.useEffect(() => {
        const changeTrack = () => __awaiter(void 0, void 0, void 0, function* () {
            if (legacyMode !== mediaRoom.legacyMode) {
                mediaRoom.legacyMode = legacyMode;
                mediaRoom.resetVideoProducers(webcamTrack, screenShareVideoTrack);
            }
        });
        if (mediaRoom && producersState === roomSlice_1.MediaStates.LOADED) {
            changeTrack();
        }
    }, [legacyMode, producersState]);
    react_1.useEffect(() => {
        const startProducing = () => __awaiter(void 0, void 0, void 0, function* () {
            dispatch(roomSlice_1.setProducersState(roomSlice_1.MediaStates.CONNECTING));
            if (webcamTrack) {
                yield mediaRoom.shareWebcam(webcamTrack);
            }
            if (audioTrack) {
                yield mediaRoom.shareMic(audioTrack, audioMuted);
            }
            dispatch(roomSlice_1.setProducersState(roomSlice_1.MediaStates.LOADED));
            meeting === null || meeting === void 0 ? void 0 : meeting.emit(meeting.Events.localMediaConnected);
        });
        const fetchData = () => {
            mediaRoom.getMesssages();
            mediaRoom.getPolls();
        };
        if (producersState === roomSlice_1.MediaStates.NOT_STARTED && waitingState !== roomSlice_1.WaitingState.WAITING) {
            // Don't fetch data again for hidden state change
            if (!mediaRoom.hidden) {
                fetchData();
            }
            mediaRoom.hidden = false;
            startProducing();
        }
    }, [producersState, joined, waitingState]);
    react_1.useEffect(() => {
        if (dynPreset) {
            processPreset(dynPreset);
        }
    }, [dynPreset]);
    return react_1.default.createElement(components_1.Box, null);
};
exports.default = react_1.default.memo(StageService);
//# sourceMappingURL=StageService.js.map