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: "AIzaSyCuHJHdwVN_F-VmUG4Hd7bGiMRqj6rPlLo",
  authDomain: "musicland-d33f9.firebaseapp.com",
  projectId: "musicland-d33f9",
  storageBucket: "musicland-d33f9.firebasestorage.app",
  messagingSenderId: "305598753437",
  appId: "1:305598753437:web:9930f10af59e5cf7f28274",
  measurementId: "G-RV4MS1SVTT",
};

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

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

  if (__DEV__) {
    firebase.functions().useEmulator("192.168.68.98", 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;
