text: import { initializeApp } from "firebase/app";
  import {
    createUserWithEmailAndPassword,
    getAuth,
    GoogleAuthProvider,
    onAuthStateChanged,
    sendPasswordResetEmail,
    signInWithPopup,
    signInWithEmailAndPassword,
    signOut,
  } from "firebase/auth";
  import { getFirestore } from "firebase/firestore";
  
  import storage from "@/utils/storage";
  
  const firebaseConfig = {
    apiKey: process.env.FIREBASE_API_KEY,
    appId: process.env.FIREBASE_APP_ID,
    authDomain: process.env.FIREBASE_AUTH_DOMAIN,
    messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
    projectId: process.env.FIREBASE_PROECT_ID,
    storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
  };
  
  // TODO: move these into the redux store as actions in an auth slice
  
  const app = initializeApp(firebaseConfig);
  const db = getFirestore(app);
  const auth = getAuth(app);
  const googleProvider = new GoogleAuthProvider();
  
  onAuthStateChanged(auth, (user) => {
    if (user) {
      user.getIdToken(true).then((token) => {
        storage.setToken(token);
      });
    } else {
      storage.clearToken();
    }
  });
  
  const signInWithGoogle = async () => {
    try {
      const result = await signInWithPopup(auth, googleProvider);
      const user = result.user;
  
      // add the user if they aren't found in the database
      const query = await db.collection("users").where("uid", "==", user.uid).get();
      if (query.docs.length === 0) {
        await db.collection("users").add({
          uid: user.uid,
          name: user.displayName,
          authprovider: "google",
          email: user.email,
        });
      }
    } catch (error) {
      console.error(error);
    }
  };
  
  const signInWithEmail = async (email, password) => {
    try {
      await signInWithEmailAndPassword(auth, email, password);
    } catch (error) {
      console.error(error);
    }
  };
  
  const registerWithEmail = async (name, email, password) => {
    try {
      const result = await createUserWithEmailAndPassword(auth, email, password);
      const user = result.user;
      await db.collection("users").add({
        uid: user.uid,
        name,
        authProvider: "local",
        email,
      });
    } catch (error) {
      console.error(error);
    }
  };
  
  const sendPasswordReset = async (email) => {
    try {
      await sendPasswordResetEmail(auth, email);
    } catch (error) {
      console.error(error);
    }
  };
  
  const logout = () => {
    signOut(auth);
  };
  
  export {
    auth,
    db,
    signInWithGoogle,
    signInWithEmail,
    registerWithEmail,
    sendPasswordReset,
    logout,
  };
  