/* eslint-disable react/destructuring-assignment */
/* eslint-disable max-classes-per-file */
import React, { useEffect, useState } from 'react';
import { Provider } from 'react-redux';
import { ClientContext } from 'graphql-hooks';
import { store } from '../store/store';
import { getClient } from '../utils/graphqlClient';
import { ConfigServiceProvider } from '../utils/ConfigProvider';
import UserTrackProvider from '../services/UserTrackProvider';
import SDKSetupStage from '../pages/SDKSetupStage';
import { Meeting } from '../lib/DyteMeeting';
import { setUrl } from '../utils/config';
import { nanoid } from '@reduxjs/toolkit';
import LoadingScreen from '../components/LoadingScreen';
import { StatusBar } from 'react-native';
class ErrorBoundary extends React.Component {
    constructor(props) {
        super(props);
        this.onError = (e) => { };
        this.onError = props.onError;
        this.state = { hasError: false, eventId: null };
    }
    static getDerivedStateFromError(error) {
        try {
            const eventId = 1;
            return { hasError: true, eventId };
        }
        catch {
            return { hasError: true, eventId: -1 };
        }
    }
    componentDidCatch(error, errorInfo) {
        console.log(error);
    }
    render() {
        if (this.state.eventId) {
            this.onError(this.state.eventId);
        }
        if (this.state.hasError) {
            return (<LoadingScreen text={`Something went wrong.. #${this.state.eventId}`}/>);
        }
        return this.props.children;
    }
}
let _dytemeeting;
const Dyte = (props) => {
    const { clientId, meetingConfig, uiConfig, onInit, onError, } = props;
    const [meeting, setMeeting] = useState(null);
    const [renderKey, setRenderKey] = useState(null);
    const [ended, setEnded] = useState(false);
    useEffect(() => {
        // 11
        const setupConfig = {
            clientId,
            authToken: meetingConfig.authToken,
            roomName: meetingConfig.roomName,
            backgroundBehaviour: meetingConfig.backgroundBehaviour,
        };
        if (meetingConfig?.apiBase) {
            setUrl(meetingConfig.apiBase);
        }
        const m = new Meeting(setupConfig, uiConfig);
        _dytemeeting = m;
        m.showSetupScreen = meetingConfig.showSetupScreen || false;
        setRenderKey(nanoid());
        setMeeting(m);
        m.on(m.Events.meetingEnded, () => setEnded(true));
        m.onError((error) => onError(error));
        onInit(m);
    }, [meetingConfig.roomName, meetingConfig.authToken, meetingConfig.apiBase]);
    const sendSentryError = (errorId) => {
        // onError(errorId);
    };
    if (!meeting || ended)
        return null;
    return (<ErrorBoundary onError={sendSentryError}>
			<StatusBar hidden/>
			<Provider store={store}>
				<ClientContext.Provider value={getClient(meeting.setupConfig.apiBase)}>
					<ConfigServiceProvider meeting={meeting}>
						<UserTrackProvider>
							<SDKSetupStage roomName={meetingConfig.roomName}/>
						</UserTrackProvider>
					</ConfigServiceProvider>
				</ClientContext.Provider>
			</Provider>
		</ErrorBoundary>);
};
Dyte.defaultProps = {
    uiConfig: null,
    onError: (e) => { },
};
function deepEqual(object1, object2) {
    function isObject(object) {
        return object != null && typeof object === 'object';
    }
    const keys1 = Object.keys(object1);
    const keys2 = Object.keys(object2);
    if (keys1.length !== keys2.length) {
        return false;
    }
    // eslint-disable-next-line no-restricted-syntax
    for (const key of keys1) {
        const val1 = object1[key];
        const val2 = object2[key];
        const areObjects = isObject(val1) && isObject(val2);
        if ((areObjects && !deepEqual(val1, val2)) || (!areObjects && val1 !== val2)) {
            return false;
        }
    }
    // eslint-disable-next-line no-restricted-syntax
    for (const key of keys2) {
        const val1 = object1[key];
        const val2 = object2[key];
        const areObjects = isObject(val1) && isObject(val2);
        if ((areObjects && !deepEqual(val1, val2)) || (!areObjects && val1 !== val2)) {
            return false;
        }
    }
    return true;
}
function areEqual(prevProps, nextProps) {
    if (!deepEqual(nextProps.meetingConfig, prevProps.meetingConfig)) {
        return false;
    }
    if (!deepEqual(nextProps.uiConfig, prevProps.uiConfig)) {
        if (_dytemeeting) {
            _dytemeeting.updateUIConfig(nextProps.uiConfig);
        }
    }
    return true;
}
export default React.memo(Dyte, areEqual);
//# sourceMappingURL=Dyte.js.map