"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRotation = exports.RotationProvider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const rotation_async_native_1 = require("./rotation-async.native");
const RotationContext = (0, react_1.createContext)(null);
const RotationProvider = ({ children, enabled = true, }) => {
    const [isLocked, setIsLocked] = (0, react_1.useState)(false);
    const [canRotate, setCanRotate] = (0, react_1.useState)(rotation_async_native_1.isRotationAvailable);
    const [currentOrientation, setCurrentOrientation] = (0, react_1.useState)(rotation_async_native_1.ScreenOrientation?.Orientation.PORTRAIT_UP ?? 1);
    // If module not available, provide disabled context
    if (!rotation_async_native_1.isRotationAvailable || !rotation_async_native_1.ScreenOrientation) {
        const disabledContextValue = {
            currentOrientation: 1, // Orientation.PORTRAIT_UP
            isLocked: false,
            isActive: false,
            rotateToLandscape: async () => { },
            rotateToPortrait: async () => { },
            toggleRotation: async () => { },
            canRotate: false,
        };
        return ((0, jsx_runtime_1.jsx)(RotationContext.Provider, { value: disabledContextValue, children: children }));
    }
    // Manual rotation functions
    const rotateToLandscape = async () => {
        if (!enabled || !canRotate || !rotation_async_native_1.ScreenOrientation)
            return;
        try {
            await rotation_async_native_1.ScreenOrientation.unlockAsync();
            await rotation_async_native_1.ScreenOrientation.lockAsync(rotation_async_native_1.ScreenOrientation.OrientationLock.LANDSCAPE_LEFT);
            setIsLocked(true);
            // set current orientation to landscape right
            setCurrentOrientation(rotation_async_native_1.ScreenOrientation.Orientation.LANDSCAPE_LEFT);
            if (__DEV__) {
                console.log("📲 Manual landscape");
            }
        }
        catch (error) {
            console.warn("Failed to rotate to landscape:", error);
        }
    };
    const rotateToPortrait = async () => {
        if (!enabled || !canRotate || !rotation_async_native_1.ScreenOrientation)
            return;
        try {
            await rotation_async_native_1.ScreenOrientation.unlockAsync();
            await rotation_async_native_1.ScreenOrientation.lockAsync(rotation_async_native_1.ScreenOrientation.OrientationLock.PORTRAIT_UP);
            setIsLocked(true);
            setCurrentOrientation(rotation_async_native_1.ScreenOrientation.Orientation.PORTRAIT_UP);
            if (__DEV__) {
                console.log("📲 Manual portrait");
            }
        }
        catch (error) {
            console.warn("Failed to rotate to portrait:", error);
        }
    };
    const toggleRotation = async () => {
        if (!rotation_async_native_1.ScreenOrientation)
            return;
        const isLandscape = currentOrientation === rotation_async_native_1.ScreenOrientation.Orientation.LANDSCAPE_LEFT ||
            currentOrientation === rotation_async_native_1.ScreenOrientation.Orientation.LANDSCAPE_RIGHT;
        if (__DEV__) {
            console.log(`🔄 Toggle: current=${currentOrientation}, isLandscape=${isLandscape}`);
        }
        if (isLandscape) {
            await rotateToPortrait();
        }
        else {
            await rotateToLandscape();
        }
    };
    // Track orientation changes
    (0, react_1.useEffect)(() => {
        if (!enabled)
            return;
        const getCurrentOrientation = async () => {
            if (!rotation_async_native_1.ScreenOrientation)
                return;
            try {
                const orient = await rotation_async_native_1.ScreenOrientation.getOrientationAsync();
                setCurrentOrientation(orient);
                if (__DEV__) {
                    console.log(`📲 Orientation on load: ${orient}`);
                }
            }
            catch (error) {
                console.warn("Failed to get orientation:", error);
            }
        };
        getCurrentOrientation();
        if (!rotation_async_native_1.ScreenOrientation)
            return;
        const subscription = rotation_async_native_1.ScreenOrientation.addOrientationChangeListener((event) => {
            const newOrientation = event.orientationInfo.orientation;
            setCurrentOrientation(newOrientation);
            if (__DEV__) {
                console.log(`🔄 Orientation: ${newOrientation} (locked: ${isLocked})`);
            }
        });
        return () => {
            subscription?.remove();
            if (rotation_async_native_1.ScreenOrientation) {
                rotation_async_native_1.ScreenOrientation.removeOrientationChangeListeners();
                rotation_async_native_1.ScreenOrientation.unlockAsync().catch(() => { });
            }
        };
    }, [enabled]);
    const contextValue = (0, react_1.useMemo)(() => ({
        currentOrientation,
        isLocked,
        isActive: enabled,
        rotateToLandscape,
        rotateToPortrait,
        toggleRotation,
        canRotate,
    }), [
        currentOrientation,
        isLocked,
        enabled,
        canRotate,
        rotateToLandscape,
        rotateToPortrait,
        toggleRotation,
    ]);
    return ((0, jsx_runtime_1.jsx)(RotationContext.Provider, { value: contextValue, children: children }));
};
exports.RotationProvider = RotationProvider;
const useRotation = () => {
    const context = (0, react_1.useContext)(RotationContext);
    if (!context) {
        throw new Error("useRotation must be used within a RotationProvider");
    }
    return context;
};
exports.useRotation = useRotation;
//# sourceMappingURL=rotation-lock.js.map