"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 react/destructuring-assignment */
/* eslint-disable max-classes-per-file */
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const graphql_hooks_1 = require("graphql-hooks");
const store_1 = require("../store/store");
const graphqlClient_1 = require("../utils/graphqlClient");
const ConfigProvider_1 = require("../utils/ConfigProvider");
const UserTrackProvider_1 = __importDefault(require("../services/UserTrackProvider"));
const SDKSetupStage_1 = __importDefault(require("../pages/SDKSetupStage"));
const DyteMeeting_1 = require("./DyteMeeting");
const config_1 = require("../utils/config");
const toolkit_1 = require("@reduxjs/toolkit");
const LoadingScreen_1 = __importDefault(require("../components/LoadingScreen"));
const react_native_1 = require("react-native");
const DrawerService_1 = __importDefault(require("../services/DrawerService"));
const OrientationContext_1 = __importDefault(require("../services/OrientationContext"));
class ErrorBoundary extends react_1.default.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 (_a) {
            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 (react_1.default.createElement(LoadingScreen_1.default, { text: `Something went wrong.. #${this.state.eventId}` }));
        }
        return this.props.children;
    }
}
let _dytemeeting;
const Dyte = ({ clientId = '', meetingConfig, uiConfig, onInit, onError, }) => {
    const [meeting, setMeeting] = react_1.useState(null);
    const [renderKey, setRenderKey] = react_1.useState(null);
    const [ended, setEnded] = react_1.useState(false);
    react_1.useEffect(() => {
        const setupConfig = {
            clientId,
            authToken: meetingConfig.authToken,
            roomName: meetingConfig.roomName,
            backgroundBehaviour: meetingConfig.backgroundBehaviour,
            iosSafeAreaInsetConfig: meetingConfig.iosSafeAreaInsetConfig
        };
        if (meetingConfig === null || meetingConfig === void 0 ? void 0 : meetingConfig.apiBase) {
            config_1.setUrl(meetingConfig.apiBase);
        }
        const m = new DyteMeeting_1.Meeting(setupConfig, uiConfig);
        _dytemeeting = m;
        m.showSetupScreen = meetingConfig.showSetupScreen || false;
        setRenderKey(toolkit_1.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 (react_1.default.createElement(ErrorBoundary, { onError: sendSentryError },
        react_1.default.createElement(react_native_1.StatusBar, { hidden: true }),
        react_1.default.createElement(react_redux_1.Provider, { store: store_1.store },
            react_1.default.createElement(OrientationContext_1.default, null,
                react_1.default.createElement(graphql_hooks_1.ClientContext.Provider, { value: graphqlClient_1.getClient(meeting.setupConfig.apiBase) },
                    react_1.default.createElement(ConfigProvider_1.ConfigServiceProvider, { meeting: meeting },
                        react_1.default.createElement(UserTrackProvider_1.default, null,
                            react_1.default.createElement(DrawerService_1.default, null,
                                react_1.default.createElement(SDKSetupStage_1.default, { roomName: meetingConfig.roomName })))))))));
};
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;
}
exports.default = react_1.default.memo(Dyte, areEqual);
//# sourceMappingURL=Dyte.js.map