text: import firebase from "firebase/app";
  import "firebase/auth";
  import "firebase/firestore";
  require('firebase/app');
  require('firebase/auth');
  require("firebase/firestore");
  
  const firebaseConfig = {
    apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
    authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
    databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
    projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
    storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
    appId: process.env.REACT_APP_FIREBASE_APP_ID,
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  
  export const auth = firebase.auth();
  export const firestore = firebase.firestore();
  export const FieldValue = firebase.firestore.FieldValue;
  
  const provider = new firebase.auth.GoogleAuthProvider();
  export const signInWithGoogle = () => {
    auth.signInWithRedirect(provider).then((result) => {
      if (!result.user) {
        return false;
      } else {
        console.log(result.user.providerData);
        let nameArr = result.user.displayName.split(' ');
        generateUserDocument(result.user, {firstName: nameArr[0], lastName: nameArr[1], likes: 0});
        return true;
      }
    });
  };
  
  export const generateUserDocument = async (user, additionalData) => {
    if (!user) return;
  
    const userRef = firestore.doc(`users/${user.uid}`);
    const snapshot = await userRef.get();
  
    if (!snapshot.exists) {
      const { email, displayName, photoURL } = user;
      let nameArr = displayName.split(' ');
      //     firebase.firestore().collection('users').doc(firebaseUser.uid).set(
      //       {
      //         firstName: nameArr[0],
      //         lastName: nameArr[nameArr.length - 1],
      //         email: firebaseUser.email,
      //       }
      try {
        await userRef.set({
          displayName,
          email,
          photoURL,
          ...additionalData
        });
      } catch (error) {
        console.error("Error creating user document", error);
      }
    }
    return getUserDocument(user.uid);
  };
  
  export const getTimeStamp = () => {
    return firebase.firestore.FieldValue.serverTimestamp();
  }
  
  const getUserDocument = async uid => {
    if (!uid) return null;
    try {
      const userDocument = await firestore.doc(`users/${uid}`).get();
  
      return {
        uid,
        ...userDocument.data()
      };
    } catch (error) {
      console.error("Error fetching user", error);
    }
  };
  