"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fullscreen = Fullscreen;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_native_1 = require("react-native");
const react_native_edge_to_edge_1 = require("react-native-edge-to-edge");
const react_native_safe_area_context_1 = require("react-native-safe-area-context");
const __1 = require("../..");
const audio_only_overlay_1 = require("./ui/audio-only-overlay");
const video_native_1 = tslib_1.__importDefault(require("./video.native"));
// Standard 16:9 video aspect ratio
const VIDEO_ASPECT_RATIO = 16 / 9;
function Fullscreen(props) {
    const ref = (0, react_1.useRef)(null);
    const insets = (0, react_native_safe_area_context_1.useSafeAreaInsets)();
    const [dimensions, setDimensions] = (0, react_1.useState)(react_native_1.Dimensions.get("window"));
    // Get state from player store
    const protocol = (0, __1.usePlayerStore)((x) => x.protocol);
    const fullscreen = (0, __1.usePlayerStore)((x) => x.fullscreen);
    const setFullscreen = (0, __1.usePlayerStore)((x) => x.setFullscreen);
    const handle = (0, __1.useLivestreamStore)((x) => x.profile?.handle);
    const danmuEnabled = (0, __1.useDanmuEnabled)();
    const danmuOpacity = (0, __1.useDanmuOpacity)();
    const danmuSpeed = (0, __1.useDanmuSpeed)();
    const danmuLaneCount = (0, __1.useDanmuLaneCount)();
    const danmuMaxMessages = (0, __1.useDanmuMaxMessages)();
    const setSrc = (0, __1.usePlayerStore)((x) => x.setSrc);
    (0, react_1.useEffect)(() => {
        setSrc(props.src);
    }, [props.src]);
    // Re-calculate dimensions on orientation change
    (0, react_1.useEffect)(() => {
        const updateDimensions = () => {
            setDimensions(react_native_1.Dimensions.get("window"));
        };
        const subscription = react_native_1.Dimensions.addEventListener("change", updateDimensions);
        return () => {
            subscription.remove();
        };
    }, []);
    // Hide status bar when in fullscreen mode
    (0, react_1.useEffect)(() => {
        if (fullscreen) {
            react_native_edge_to_edge_1.SystemBars.setHidden(true);
            console.log("setting sidebar hidden");
            // Handle hardware back button
            const backHandler = react_native_1.BackHandler.addEventListener("hardwareBackPress", () => {
                setFullscreen(false);
                return true;
            });
            return () => {
                backHandler.remove();
            };
        }
        else {
            react_native_edge_to_edge_1.SystemBars.setHidden(false);
        }
        return () => {
            react_native_edge_to_edge_1.SystemBars.setHidden(false);
        };
    }, [fullscreen, setFullscreen]);
    // Handle fullscreen state changes for native video players
    (0, react_1.useEffect)(() => {
        // For WebRTC, we handle fullscreen manually via the custom implementation
        if (protocol === __1.PlayerProtocol.WEBRTC) {
            return;
        }
        // For HLS and other protocols, sync with native fullscreen
        if (ref.current) {
            if (fullscreen) {
                ref.current.enterFullscreen();
            }
            else {
                ref.current.exitFullscreen();
            }
        }
    }, [fullscreen, protocol]);
    if (fullscreen && protocol === __1.PlayerProtocol.WEBRTC) {
        // Determine if we're in landscape mode
        const isLandscape = dimensions.width > dimensions.height;
        // Calculate video container dimensions based on screen size and orientation
        let videoWidth;
        let videoHeight;
        if (isLandscape) {
            // In landscape, account for safe areas and use available height
            const availableHeight = dimensions.height - (insets.top + insets.bottom);
            const availableWidth = dimensions.width - (insets.left + insets.right);
            videoHeight = availableHeight;
            videoWidth = videoHeight * VIDEO_ASPECT_RATIO;
            // If calculated width exceeds available width, constrain and maintain aspect ratio
            if (videoWidth > availableWidth) {
                videoWidth = availableWidth;
                videoHeight = videoWidth / VIDEO_ASPECT_RATIO;
            }
        }
        else {
            // In portrait, account for safe areas
            const availableWidth = dimensions.width - (insets.left + insets.right);
            videoWidth = availableWidth;
            videoHeight = videoWidth / VIDEO_ASPECT_RATIO;
        }
        // Calculate position to center the video, accounting for safe areas
        const leftPosition = (dimensions.width - videoWidth) / 2;
        const topPosition = (dimensions.height - videoHeight) / 2;
        // When in custom fullscreen mode
        return ((0, jsx_runtime_1.jsx)(react_native_1.View, { style: [
                styles.fullscreenContainer,
                {
                    width: isLandscape ? dimensions.width + 40 : dimensions.width,
                    height: dimensions.height,
                },
            ], children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: [
                    styles.videoContainer,
                    {
                        width: isLandscape ? videoWidth + 40 : videoWidth,
                        height: videoHeight,
                        left: leftPosition,
                        top: topPosition,
                    },
                ], children: [(0, jsx_runtime_1.jsx)(video_native_1.default, { objectFit: props.objectFit, pictureInPictureEnabled: props.pictureInPictureEnabled }), (0, jsx_runtime_1.jsx)(audio_only_overlay_1.AudioOnlyOverlay, {}), (0, jsx_runtime_1.jsx)(__1.DanmuOverlay, { enabled: danmuEnabled, opacity: danmuOpacity, speed: danmuSpeed, laneCount: danmuLaneCount, maxMessages: danmuMaxMessages }), props.children] }) }));
    }
    // Normal non-fullscreen mode
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(__1.VideoRetry, { children: (0, jsx_runtime_1.jsx)(video_native_1.default, { objectFit: props.objectFit, pictureInPictureEnabled: props.pictureInPictureEnabled }) }), (0, jsx_runtime_1.jsx)(audio_only_overlay_1.AudioOnlyOverlay, {}), (0, jsx_runtime_1.jsx)(__1.DanmuOverlay, { enabled: danmuEnabled, opacity: danmuOpacity, speed: danmuSpeed, laneCount: danmuLaneCount, maxMessages: danmuMaxMessages }), props.children] }));
}
const styles = react_native_1.StyleSheet.create({
    fullscreenContainer: {
        position: "absolute",
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        backgroundColor: "#000",
        zIndex: 9999,
        elevation: 9999,
        margin: 0,
        padding: 0,
        justifyContent: "center",
        alignItems: "center",
    },
    videoContainer: {
        position: "absolute",
        backgroundColor: "#111",
        overflow: "hidden",
    },
});
//# sourceMappingURL=fullscreen.native.js.map