import React from 'react';
import { Dimensions, Platform, NativeModules, NativeEventEmitter, } from 'react-native';
const { DyteHelper } = NativeModules;
const ne = new NativeEventEmitter(DyteHelper);
export const OrientationContext = React.createContext(null);
/**
 * Returns true if the screen is in portrait mode
 */
const isPortrait = () => {
    const dim = Dimensions.get('screen');
    return dim.height >= dim.width;
};
/**
 * A React Hook which updates when the orientation changes
 * @returns whether the user is in 'PORTRAIT' or 'LANDSCAPE'
 */
export default function (props) {
    const { children } = props;
    const [orientation, setOrientation] = React.useState(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
    const [orientationType, setOrientationType] = React.useState(Platform.OS === 'ios' ? DyteHelper.deviceOrientation() : 'unknown');
    const [forced, setForced] = React.useState(false);
    React.useEffect(() => {
        const callback = () => {
            setOrientation(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
        };
        const listener = Dimensions.addEventListener('change', callback);
        const getOrientation = async () => {
            if (Platform.OS === 'android') {
                const isForced = await DyteHelper.isForcedLandscape();
                setForced(isForced || false);
            }
        };
        getOrientation();
        let deviceOrientation = null;
        if (Platform.OS === 'ios') {
            deviceOrientation = ne.addListener('DEVICE_ORIENTATION', (event) => {
                setOrientationType(event.orientation);
            });
        }
        return () => {
            if (listener) {
                listener.remove();
            }
            else {
                Dimensions.removeEventListener('change', callback);
            }
            if (Platform.OS === 'ios') {
                deviceOrientation?.remove();
            }
        };
    }, []);
    return (<OrientationContext.Provider value={{
        orientation, forced, setForced, orientationType,
    }}>
			{children}
		</OrientationContext.Provider>);
}
//# sourceMappingURL=OrientationContext.js.map