"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.OrientationContext = void 0;
const react_1 = __importDefault(require("react"));
const react_native_1 = require("react-native");
const { DyteHelper } = react_native_1.NativeModules;
const ne = new react_native_1.NativeEventEmitter(DyteHelper);
exports.OrientationContext = react_1.default.createContext(null);
/**
 * Returns true if the screen is in portrait mode
 */
const isPortrait = () => {
    const dim = react_native_1.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'
 */
function default_1(props) {
    const { children } = props;
    const [orientation, setOrientation] = react_1.default.useState(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
    const [orientationType, setOrientationType] = react_1.default.useState(react_native_1.Platform.OS === 'ios' ? DyteHelper.deviceOrientation() : 'unknown');
    const [forced, setForced] = react_1.default.useState(false);
    react_1.default.useEffect(() => {
        const callback = () => {
            setOrientation(isPortrait() ? 'PORTRAIT' : 'LANDSCAPE');
        };
        const listener = react_native_1.Dimensions.addEventListener('change', callback);
        const getOrientation = () => __awaiter(this, void 0, void 0, function* () {
            if (react_native_1.Platform.OS === 'android') {
                const isForced = yield DyteHelper.isForcedLandscape();
                setForced(isForced || false);
            }
        });
        getOrientation();
        let deviceOrientation = null;
        if (react_native_1.Platform.OS === 'ios') {
            deviceOrientation = ne.addListener('DEVICE_ORIENTATION', (event) => {
                setOrientationType(event.orientation);
            });
        }
        return () => {
            if (listener) {
                listener.remove();
            }
            else {
                react_native_1.Dimensions.removeEventListener('change', callback);
            }
            if (react_native_1.Platform.OS === 'ios') {
                deviceOrientation === null || deviceOrientation === void 0 ? void 0 : deviceOrientation.remove();
            }
        };
    }, []);
    return (react_1.default.createElement(exports.OrientationContext.Provider, { value: {
            orientation,
            forced,
            setForced,
            orientationType,
        } }, children));
}
exports.default = default_1;
//# sourceMappingURL=OrientationContext.js.map