import firebase from "firebase/compat/app";
import {
  initializeAuth,
  getReactNativePersistence,
} from "firebase/auth/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";

import "firebase/compat/auth";
import "firebase/compat/storage";
import "firebase/compat/functions";
import "firebase/compat/firestore";

export const firebaseConfig = {
  apiKey: "AIzaSyDrD0yGi4U_5zxLjJmypnA7QmKXVeEkhyc",
  authDomain: "minuitstarter.firebaseapp.com",
  projectId: "minuitstarter",
  storageBucket: "minuitstarter.appspot.com",
  messagingSenderId: "918923582918",
  appId: "1:918923582918:web:c2ff55f8e19881f249a6c7",
};

if (!firebase?.apps?.filter(({ name_ }) => name_ === "[DEFAULT]").length) {
  const defaultApp = firebase.initializeApp(firebaseConfig);

  initializeAuth(defaultApp, {
    persistence: getReactNativePersistence(AsyncStorage),
  });

  // if (__DEV__) {
  //   firebase.functions().useEmulator("localhost", 5001);
  // }
  console.log("Firebase init");
}

const firestore = firebase.firestore();

export const usersRef = firestore.collection("users");
export const notificationsRef = firestore.collection("notifications");
export const documentsRef = firestore.collection("documents");
export const chatsRef = firestore.collection("chats");

export const { arrayUnion, arrayRemove, increment } =
  firebase.firestore.FieldValue;

export default firebase;
