"use strict";

import { useEffect, useRef, useState } from 'react';
import { AppState } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { KeyboardProvider } from 'react-native-keyboard-controller';
import { OxyContextProvider } from '../context/OxyContext';
import { QueryClientProvider, focusManager, onlineManager } from '@tanstack/react-query';
import { setupFonts } from './FontLoader';
import BottomSheetRouter from './BottomSheetRouter';
import { Toaster } from '../../lib/sonner';
import { createQueryClient } from '../hooks/queryClient';
import { useStorage } from '../hooks/useStorage';

// Initialize fonts automatically
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
setupFonts();

/**
 * OxyProvider component
 *
 * Provides the authentication/session context used across the app.
 * UI composition (e.g. OxyRouter inside a bottom sheet) can be added externally.
 */
const OxyProvider = ({
  oxyServices,
  children,
  contextOnly = false,
  onAuthStateChange,
  storageKeyPrefix,
  baseURL,
  queryClient: providedQueryClient
}) => {
  // contextOnly is retained for backwards compatibility while the UI-only
  // bottom sheet experience is removed. At the moment both modes behave the same.
  void contextOnly;

  // Get storage for query persistence
  const {
    storage,
    isReady: isStorageReady
  } = useStorage();

  // Initialize React Query Client with persistence
  const queryClientRef = useRef(null);
  const [queryClient, setQueryClient] = useState(null);
  useEffect(() => {
    if (providedQueryClient) {
      queryClientRef.current = providedQueryClient;
      setQueryClient(providedQueryClient);
    } else if (isStorageReady) {
      // Create query client with persistence once storage is ready
      if (!queryClientRef.current) {
        const client = createQueryClient(storage);
        queryClientRef.current = client;
        setQueryClient(client);
      }
    }
  }, [providedQueryClient, isStorageReady, storage]);

  // Hook React Query focus manager into React Native AppState
  useEffect(() => {
    const subscription = AppState.addEventListener('change', state => {
      focusManager.setFocused(state === 'active');
    });
    return () => {
      subscription.remove();
    };
  }, []);

  // Setup network status monitoring for offline detection
  useEffect(() => {
    let cleanup;
    const setupNetworkMonitoring = async () => {
      try {
        // For React Native, try to use NetInfo
        if (typeof window === 'undefined' || typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
          try {
            const NetInfo = await import('@react-native-community/netinfo');
            const state = await NetInfo.default.fetch();
            onlineManager.setOnline(state.isConnected ?? true);
            const unsubscribe = NetInfo.default.addEventListener(state => {
              onlineManager.setOnline(state.isConnected ?? true);
            });
            cleanup = () => unsubscribe();
          } catch {
            // NetInfo not available, default to online
            onlineManager.setOnline(true);
          }
        } else {
          // For web, use navigator.onLine
          onlineManager.setOnline(navigator.onLine);
          const handleOnline = () => onlineManager.setOnline(true);
          const handleOffline = () => onlineManager.setOnline(false);
          window.addEventListener('online', handleOnline);
          window.addEventListener('offline', handleOffline);
          cleanup = () => {
            window.removeEventListener('online', handleOnline);
            window.removeEventListener('offline', handleOffline);
          };
        }
      } catch (error) {
        // Default to online if detection fails
        onlineManager.setOnline(true);
      }
    };
    setupNetworkMonitoring();
    return () => {
      cleanup?.();
    };
  }, []);

  // Ensure we have a valid QueryClient
  if (!queryClient) {
    // Return loading state or fallback
    return null;
  }
  return /*#__PURE__*/_jsx(SafeAreaProvider, {
    children: /*#__PURE__*/_jsx(GestureHandlerRootView, {
      style: {
        flex: 1
      },
      children: /*#__PURE__*/_jsx(KeyboardProvider, {
        children: queryClient && /*#__PURE__*/_jsx(QueryClientProvider, {
          client: queryClient,
          children: /*#__PURE__*/_jsxs(OxyContextProvider, {
            oxyServices: oxyServices,
            baseURL: baseURL,
            storageKeyPrefix: storageKeyPrefix,
            onAuthStateChange: onAuthStateChange,
            children: [children, /*#__PURE__*/_jsx(BottomSheetRouter, {}), /*#__PURE__*/_jsx(Toaster, {})]
          })
        })
      })
    })
  });
};
export default OxyProvider;
//# sourceMappingURL=OxyProvider.js.map