"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestToJoinType = exports.RequestToJoinStageState = exports.PresentingState = exports.JoinStageSettings = exports.switchToGroupCall = exports.updatePreset = exports.updateWebinarPresentingSetting = exports.updateCanPresent = exports.setVisibilitySettings = exports.disableVideo = exports.disableAudio = exports.muteAllVideo = exports.muteAll = exports.setCameraFacing = exports.resetParticipant = exports.requestToJoinPeerStageAccepted = exports.initiatePresenting = exports.stoppedPresenting = exports.startedPresenting = exports.requestToJoinType = exports.requestedJoinToStageState = exports.removedFromStage = exports.requestToJoinStageRejected = exports.requestToJoinStageAccepted = exports.setParticipantPermissions = exports.setAudioTrackSelection = exports.setVideoTrackSelection = exports.setVideoOff = exports.setScreenSharing = exports.setAudioMuted = exports.setScreenSharingProduction = exports.setAudioProduction = exports.setVideoProduction = exports.setUserDisplayImageURL = exports.setUserDisplayName = exports.setLoggedIn = exports.setUserToken = exports.setUser = exports.permissionEnum = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
const roomSlice_1 = require("./roomSlice");
const peersSlice_1 = require("./peersSlice");
const browser_utils_1 = require("../utils/browser-utils");
const newTypes_1 = require("../types/newTypes");
const react_native_1 = require("react-native");
const react_native_uuid_1 = __importDefault(require("react-native-uuid"));
var permissionEnum;
(function (permissionEnum) {
    permissionEnum[permissionEnum["ACCEPTED"] = 0] = "ACCEPTED";
    permissionEnum[permissionEnum["DENIED"] = 1] = "DENIED";
    permissionEnum[permissionEnum["FAILED"] = 2] = "FAILED";
    permissionEnum[permissionEnum["INIT"] = 3] = "INIT";
})(permissionEnum = exports.permissionEnum || (exports.permissionEnum = {}));
exports.setUser = toolkit_1.createAction('participant/set-user');
exports.setUserToken = toolkit_1.createAction('participant/set-token');
exports.setLoggedIn = toolkit_1.createAction('participant/set-logged-in');
exports.setUserDisplayName = toolkit_1.createAction('participant/set-display-name');
exports.setUserDisplayImageURL = toolkit_1.createAction('participant/set-display-image');
exports.setVideoProduction = toolkit_1.createAction('participant/set-video-production');
exports.setAudioProduction = toolkit_1.createAction('participant/set-audio-production');
exports.setScreenSharingProduction = toolkit_1.createAction('participant/set-screen-sharing-production');
exports.setAudioMuted = toolkit_1.createAction('participant/set-audio-muted');
exports.setScreenSharing = toolkit_1.createAction('participant/set-screen-sharing');
exports.setVideoOff = toolkit_1.createAction('participant/set-video-off');
exports.setVideoTrackSelection = toolkit_1.createAction('participant/set-video-track-selection');
exports.setAudioTrackSelection = toolkit_1.createAction('participant/set-audio-track-selection');
exports.setParticipantPermissions = toolkit_1.createAction('participant/set-permissions');
// Webinar Actions
exports.requestToJoinStageAccepted = toolkit_1.createAction('websocket/request-to-join-stage-accepted');
exports.requestToJoinStageRejected = toolkit_1.createAction('websocket/request-to-join-stage-rejected');
exports.removedFromStage = toolkit_1.createAction('websocket/removed-from-stage');
exports.requestedJoinToStageState = toolkit_1.createAction('room/request-to-join-stage-state');
exports.requestToJoinType = toolkit_1.createAction('participant/request-to-join-type');
exports.startedPresenting = toolkit_1.createAction('websocket/started-presenting');
exports.stoppedPresenting = toolkit_1.createAction('websocket/stopped-presenting');
exports.initiatePresenting = toolkit_1.createAction('participant/initiate-presenting');
exports.requestToJoinPeerStageAccepted = toolkit_1.createAction('websocket/peer-added-to-stage');
exports.resetParticipant = toolkit_1.createAction('session/reset-pariticipant');
exports.setCameraFacing = toolkit_1.createAction('session/camera-facing');
exports.muteAll = toolkit_1.createAction('websocket/mute-all');
exports.muteAllVideo = toolkit_1.createAction('websocket/mute-all-video');
exports.disableAudio = toolkit_1.createAction('websocket/disable-audio');
exports.disableVideo = toolkit_1.createAction('websocket/disable-video');
exports.setVisibilitySettings = toolkit_1.createAction('websocket/visibility-settings');
exports.updateCanPresent = toolkit_1.createAction('websocket/update_can_update');
exports.updateWebinarPresentingSetting = toolkit_1.createAction('websocket/update-webinar-update-setting');
// Webinar End
exports.updatePreset = toolkit_1.createAction('websocket/update-userpreset');
exports.switchToGroupCall = toolkit_1.createAction('session/switch-group-call');
var JoinStageSettings;
(function (JoinStageSettings) {
    JoinStageSettings["ALLOWED"] = "allowed";
    JoinStageSettings["ON_REQUEST"] = "on_request";
    JoinStageSettings["NOT_ALLOWED"] = "not_allowed";
})(JoinStageSettings = exports.JoinStageSettings || (exports.JoinStageSettings = {}));
var PresentingState;
(function (PresentingState) {
    PresentingState[PresentingState["PRESENTING"] = 0] = "PRESENTING";
    PresentingState[PresentingState["NOT_PRESENTING"] = 1] = "NOT_PRESENTING";
    PresentingState[PresentingState["INITIATED"] = 2] = "INITIATED";
})(PresentingState = exports.PresentingState || (exports.PresentingState = {}));
var RequestToJoinStageState;
(function (RequestToJoinStageState) {
    RequestToJoinStageState[RequestToJoinStageState["NONE"] = 0] = "NONE";
    RequestToJoinStageState[RequestToJoinStageState["WAITING"] = 1] = "WAITING";
    RequestToJoinStageState[RequestToJoinStageState["ACCEPTED"] = 2] = "ACCEPTED";
    RequestToJoinStageState[RequestToJoinStageState["REJECTED"] = 3] = "REJECTED";
    RequestToJoinStageState[RequestToJoinStageState["INITIATED"] = 4] = "INITIATED";
})(RequestToJoinStageState = exports.RequestToJoinStageState || (exports.RequestToJoinStageState = {}));
var RequestToJoinType;
(function (RequestToJoinType) {
    RequestToJoinType["present"] = "REQUEST_TO_PRESENT";
    RequestToJoinType["ask"] = "REQUEST_TO_ASK";
})(RequestToJoinType = exports.RequestToJoinType || (exports.RequestToJoinType = {}));
const initialState = {
    id: '',
    peerId: react_native_uuid_1.default.v4().toString(),
    device: '',
    email: '',
    displayName: '',
    authToken: '',
    loggedIn: false,
    displayImageURL: '',
    audioMuted: false,
    videoOff: false,
    sharingScreen: false,
    videoProduction: roomSlice_1.MediaStates.NOT_STARTED,
    audioProduction: roomSlice_1.MediaStates.NOT_STARTED,
    screenSharingProduction: roomSlice_1.MediaStates.NOT_STARTED,
    shouldProduce: true,
    isHost: false,
    canUnMute: true,
    clientSpecificId: null,
    participantPermissions: {
        camera: permissionEnum.INIT,
        microphone: permissionEnum.INIT,
        externalStorage: permissionEnum.INIT,
        bluetooth: permissionEnum.INIT,
        photos: permissionEnum.INIT,
        readExternalStorage: permissionEnum.INIT,
    },
    cameraFacing: 'front',
    hidden: false,
    webinarHiddenParticipant: false,
    joinStageSettings: JoinStageSettings.ALLOWED,
    requestToJoinStageState: RequestToJoinStageState.NONE,
    presentingState: PresentingState.NOT_PRESENTING,
    canPresent: false,
    metadata: {
        preset_name: 'default',
        view_type: 'GROUP_CALL',
    },
    organizationId: '',
    preset: null,
    switched: null,
};
function idFromAuthToken(token) {
    const decoded = browser_utils_1.parseJwt(token);
    return decoded.id;
}
function saveUserName(value) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            yield async_storage_1.default.setItem('dyte-display-name', value);
        }
        catch (_a) {
            // pass
        }
    });
}
const participant = toolkit_1.createReducer(initialState, {
    [exports.resetParticipant.toString()]: (state) => {
        state.sharingScreen = false;
        state.videoProduction = roomSlice_1.MediaStates.NOT_STARTED;
        state.audioProduction = roomSlice_1.MediaStates.NOT_STARTED;
    },
    [exports.setUser.toString()]: (state, action) => {
        state.authToken = action.payload.authToken;
        state.loggedIn = action.payload.loggedIn || false;
        state.email = action.payload.email || '';
        state.displayImageURL = action.payload.picture || '';
        state.id = action.payload.id || '';
        state.clientSpecificId = action.payload.clientSpecificId;
        state.organizationId = action.payload.organizationId;
        if (action.payload.name && action.payload.name.length > 0) {
            state.displayName = action.payload.name || '';
            saveUserName(action.payload.name || '');
        }
    },
    [exports.setUserToken.toString()]: (state, action) => {
        state.authToken = action.payload;
        state.id = idFromAuthToken(action.payload);
    },
    [exports.setUserDisplayName.toString()]: (state, action) => {
        state.displayName = action.payload;
        saveUserName(action.payload);
    },
    [exports.setUserDisplayImageURL.toString()]: (state, action) => {
        state.displayImageURL = action.payload;
    },
    [exports.setLoggedIn.toString()]: (state, action) => {
        state.loggedIn = action.payload;
    },
    [exports.setParticipantPermissions.toString()]: (state, action) => {
        state.participantPermissions = action.payload;
    },
    [exports.setVideoProduction.toString()]: (state, action) => {
        state.videoProduction = action.payload;
    },
    [exports.setAudioProduction.toString()]: (state, action) => {
        state.audioProduction = action.payload;
    },
    [exports.setScreenSharingProduction.toString()]: (state, action) => {
        state.screenSharingProduction = action.payload;
    },
    [exports.setAudioMuted.toString()]: (state, action) => {
        if (state.canUnMute === false) {
            return;
        }
        state.audioMuted = action.payload;
    },
    [exports.setVideoOff.toString()]: (state, action) => {
        state.videoOff = action.payload;
    },
    [exports.setVideoTrackSelection.toString()]: (state, action) => {
        state.videoTrackSelection = action.payload;
    },
    [exports.setAudioTrackSelection.toString()]: (state, action) => {
        state.audioTrackSelection = action.payload;
    },
    [exports.setScreenSharing.toString()]: (state, action) => {
        state.sharingScreen = action.payload;
    },
    [peersSlice_1.roomJoined.toString()]: (state, action) => {
        state.isHost = action.payload.isHost;
    },
    [exports.muteAll.toString()]: (state, action) => {
        if (!state.isHost) {
            state.audioMuted = true;
            state.canUnMute = action.payload.allowUnMute;
        }
    },
    [exports.muteAllVideo.toString()]: (state) => {
        if (!state.isHost) {
            state.videoOff = true;
        }
    },
    [exports.disableAudio.toString()]: (state) => {
        state.audioMuted = true;
    },
    [exports.disableVideo.toString()]: (state) => {
        state.videoOff = true;
    },
    [exports.setCameraFacing.toString()]: (state, action) => {
        state.cameraFacing = action.payload;
    },
    [exports.requestToJoinStageAccepted.toString()]: (state, action) => {
        state.joinStageSettings = JoinStageSettings.ALLOWED;
        state.requestToJoinStageState = RequestToJoinStageState.ACCEPTED;
        state.presentingState = PresentingState.INITIATED;
        state.requestToJoinType = action.payload.requestToJoinType;
        state.canPresent = true;
    },
    [exports.updateCanPresent.toString()]: (state, action) => {
        state.canPresent = action.payload;
    },
    [exports.updateWebinarPresentingSetting.toString()]: (state, action) => {
        state.webinarHiddenParticipant = action.payload;
    },
    [exports.requestToJoinStageRejected.toString()]: (state) => {
        state.webinarHiddenParticipant = true;
        state.joinStageSettings = JoinStageSettings.NOT_ALLOWED;
        state.requestToJoinStageState = RequestToJoinStageState.REJECTED;
        state.presentingState = PresentingState.NOT_PRESENTING;
        state.sharingScreen = false;
        state.screenSharingProduction = roomSlice_1.MediaStates.NOT_STARTED;
        react_native_1.Alert.alert('Request Declined', 'Your Request have been declined by host or moderator', [
            { text: 'OK', onPress: () => { } },
        ]);
    },
    [exports.removedFromStage.toString()]: (state, action) => {
        state.webinarHiddenParticipant = true;
        state.joinStageSettings = JoinStageSettings.NOT_ALLOWED;
        state.audioMuted = true;
        state.videoOff = true;
        state.sharingScreen = false;
        state.requestToJoinStageState = RequestToJoinStageState.NONE;
        state.presentingState = PresentingState.NOT_PRESENTING;
        state.screenSharingProduction = roomSlice_1.MediaStates.NOT_STARTED;
        state.requestToJoinType = undefined;
        const msgTitle = action.payload.type === newTypes_1.WebinarAcceptRequestType.REQUESTED_BY_PARTICIPANT ? 'Dropped successfully.' : 'Dropped from Stage';
        const msgTxt = action.payload.type === newTypes_1.WebinarAcceptRequestType.REQUESTED_BY_PARTICIPANT
            ? `You are no more ${state.requestToJoinType === RequestToJoinType.present
                ? 'Pesenting' : 'Asking'}.` : `Host/Moderator has force stopped you from ${state.requestToJoinType === RequestToJoinType.present ? 'Pesenting' : 'Asking'}.`;
        react_native_1.Alert.alert(msgTitle, msgTxt, [
            { text: 'OK', onPress: () => { } },
        ]);
        state.canPresent = false;
    },
    [exports.requestedJoinToStageState.toString()]: (state, action) => {
        state.requestToJoinStageState = action.payload;
    },
    [exports.requestToJoinType.toString()]: (state, action) => {
        state.requestToJoinType = action.payload;
    },
    [exports.startedPresenting.toString()]: (state) => {
        state.webinarHiddenParticipant = false;
        state.presentingState = PresentingState.PRESENTING;
    },
    [exports.stoppedPresenting.toString()]: (state) => {
        state.webinarHiddenParticipant = true;
        state.presentingState = PresentingState.NOT_PRESENTING;
        state.screenSharingProduction = roomSlice_1.MediaStates.NOT_STARTED;
        state.sharingScreen = false;
        state.canPresent = false;
    },
    [exports.initiatePresenting.toString()]: (state) => {
        state.presentingState = PresentingState.INITIATED;
    },
    [exports.requestToJoinPeerStageAccepted.toString()]: (state, action) => {
        if (action.payload.id === state.peerId) {
            state.requestToJoinType = action.payload.requestToJoinType;
        }
    },
    [exports.setVisibilitySettings.toString()]: (state, action) => {
        const { hidden, joinStageSettings } = action.payload;
        state.joinStageSettings = joinStageSettings;
    },
    [exports.updatePreset.toString()]: (state, action) => {
        state.preset = action.payload.preset;
    },
    [peersSlice_1.peerMetadataUpdated.toString()]: (state, action) => {
        if (state.peerId === action.payload.id) {
            state.metadata.preset_name = action.payload.preset_name;
            state.metadata.view_type = action.payload.view_type;
        }
    },
    [exports.switchToGroupCall.toString()]: (state, action) => {
        state.videoProduction = roomSlice_1.MediaStates.NOT_STARTED;
        state.audioProduction = roomSlice_1.MediaStates.NOT_STARTED;
        state.switched = 'meeting';
    },
    [peersSlice_1.switchToWebinar.toString()]: (state, action) => {
        state.switched = 'webinar';
    },
});
exports.default = participant;
//# sourceMappingURL=participantSlice.js.map