"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable */
const react_1 = __importStar(require("react"));
const react_native_1 = require("react-native");
const lib_1 = require("./lib");
const { DyteHelper, DyteStaticSafeAreaInsets } = react_native_1.NativeModules;
function Main({ clientId, meetingConfig, width, height, uiConfig = {}, }) {
    var _a, _b;
    const meetingRef = react_1.useRef(null);
    // Changes from native side
    // Native -> JS code
    react_1.useEffect(() => {
        const ne = new react_native_1.NativeEventEmitter(DyteHelper);
        const micListener = ne.addListener('SELF_TOGGLE_AUDIO', (event) => {
            if (meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.audioEnabled)
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.disableAudio();
            else
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.enableAudio();
        });
        const camListener = ne.addListener('SELF_TOGGLE_VIDEO', (event) => {
            if (meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.videoEnabled)
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.disableVideo();
            else
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.enableVideo();
        });
        const pinListener = ne.addListener('SELF_PIN', (event) => {
            meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.pin();
        });
        const unpinListener = ne.addListener('SELF_UNPIN', (event) => {
            meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.unpin();
        });
        const screensharingListener = ne.addListener('SELF_TOGGLE_SCREENSHARING', (event) => {
            if (meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.isScreensharing)
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.disableScreenshare();
            else
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.self.enableScreenshare();
        });
        const updateUIConfigListener = ne.addListener('UPDATE_UICONFIG', (event) => {
            let config = null;
            try {
                // JSON.parse empty string throws error and can crash app.
                // So  we can't do SON.parse(event.data) || ''
                // We can also not parse object i.e
                // this will throw error as well JSON.parse(event.data || {})
                // Hence probably it's best to add it in try catch
                config =
                    react_native_1.Platform.OS === 'android'
                        ? JSON.parse(event.config)
                        : event || {};
                meetingRef === null || meetingRef === void 0 ? void 0 : meetingRef.current.updateUIConfig(config);
            }
            catch (err) {
                console.log(err);
            }
        });
        return () => {
            micListener.remove();
            camListener.remove();
            pinListener.remove();
            unpinListener.remove();
            screensharingListener.remove();
            updateUIConfigListener.remove();
        };
    });
    // JS -> Native code
    const onMeetingInit = (meeting) => {
        meetingRef.current = meeting;
        meeting.on(meeting.Events.connect, () => {
            DyteHelper.SDKMeetingConnect();
        });
        meeting.on(meeting.Events.disconnect, () => {
            DyteHelper.SDKMeetingDisconnect();
        });
        meeting.on(meeting.Events.meetingJoined, () => {
            DyteHelper.SDKMeetingJoined();
            DyteHelper.SDKSelfBasic(meeting.self.id, meeting.self.name, meeting.self.picture);
        });
        meeting.on(meeting.Events.meetingEnded, (p) => {
            DyteHelper.SDKMeetingEnded();
        });
        meeting.on(meeting.Events.activeSpeaker, (p) => {
            DyteHelper.SDKActiveSpeaker(p.id);
        });
        meeting.on(meeting.Events.participantLeave, (p) => {
            DyteHelper.SDKParticipantLeave(p.id);
        });
        meeting.on(meeting.Events.participantJoin, (p) => {
            DyteHelper.SDKParticipantJoin(p.id, p.name, p.picture, p.clientSpecificId, p.audioEnabled, p.videoEnabled, p.isPinned, false);
        });
        meeting.on(meeting.Events.participantUpdate, (p) => {
            DyteHelper.SDKParticipantUpdate(p.id, p.audioEnabled, p.videoEnabled, p.isPinned, false);
        });
        meeting.on(meeting.Events.roomMessage, (p) => {
            DyteHelper.SDKRoomMessage(p);
        });
        meeting.on(meeting.Events.message, (p) => {
            DyteHelper.SDKMessage(p);
        });
        meeting.on(meeting.Events.localMediaConnected, () => {
            DyteHelper.SDKSelfUpdate(meeting.self.audioEnabled, meeting.self.videoEnabled, meeting.self.isPinned, meeting.self.isScreensharing);
            DyteHelper.SDKlocalMediaConnected();
        });
    };
    // Android sends string which we need to parse to make it an object
    // Ios on other hand directly sends obkect
    if (react_native_1.Platform.OS === 'android') {
        try {
            uiConfig = JSON.parse(uiConfig);
        }
        catch (e) {
            uiConfig = {};
        }
    }
    let iosHeightDelta = 0;
    if ((_a = meetingConfig.iosSafeAreaInsetConfig) === null || _a === void 0 ? void 0 : _a.includes('bottom')) {
        iosHeightDelta += DyteStaticSafeAreaInsets.safeAreaInsetsBottom;
    }
    if ((_b = meetingConfig.iosSafeAreaInsetConfig) === null || _b === void 0 ? void 0 : _b.includes('top')) {
        iosHeightDelta += DyteStaticSafeAreaInsets.safeAreaInsetsTop;
    }
    uiConfig =
        width && height
            ? Object.assign(Object.assign({}, uiConfig), { dimensions: react_native_1.Platform.OS === 'android'
                    ? {
                        height: Math.floor(height / react_native_1.PixelRatio.get()),
                        width: Math.floor(width / react_native_1.PixelRatio.get()),
                    }
                    : {
                        height: Math.floor(height - iosHeightDelta),
                        width: Math.floor(width),
                    } }) : Object.assign({}, uiConfig);
    return (react_1.default.createElement(lib_1.DyteMeeting, { clientId: clientId, meetingConfig: meetingConfig, onInit: onMeetingInit, uiConfig: uiConfig }));
}
exports.default = Main;
//# sourceMappingURL=index.js.map