import { initializeApp } from "firebase/app";
import {
  initializeAuth,
  // `getReactNativePersistence` is documented Firebase RN setup but Firebase
  // intentionally omits it from `firebase/auth`'s public type bundle (it
  // would pollute web autocomplete). The runtime export exists on every
  // platform; this is the workaround the Firebase docs themselves use.
  // @ts-expect-error — RN-only symbol missing from public Firebase types.
  getReactNativePersistence,
} from "firebase/auth";
import { initializeFirestore } from "firebase/firestore";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { appConfig } from "@/config/env";

export const firebaseApp = initializeApp(appConfig.firebase);

// AsyncStorage persists the Firebase auth token across cold starts. Without
// it, the user would have to re-sign-in to Firestore every launch even though
// the Orion bearer token is in SecureStore.
export const firebaseAuth = initializeAuth(firebaseApp, {
  persistence: getReactNativePersistence(AsyncStorage),
});

// Firestore's default WebChannel transport often fails on cellular networks
// and behind aggressive proxies on iOS. Long-polling is the documented
// remedy for React Native and is also what the Firebase team recommends
// for mobile apps using the JS SDK.
export const firestoreDb = initializeFirestore(firebaseApp, {
  experimentalAutoDetectLongPolling: true,
});
