import "firebase/analytics";
import "firebase/auth";
import "firebase/database";
import "firebase/messaging";
import firebase from "firebase/app";

const config = JSON.parse(process.env.REACT_APP_FIREBASE_CONFIG);
firebase.initializeApp(config);

export const auth = firebase.auth();
export const analytics = firebase.analytics();
export default firebase;

export const getCurrentUser = () => {
  return new Promise((resolve, reject) => {
    const unsubscribe = auth.onAuthStateChanged((userAuth) => {
      unsubscribe();
      resolve(userAuth);
    }, reject);
  });
};

export const updateCurrentUser = (userDetails) => {
  return new Promise((resolve, reject) => {
    const unsubscribe = auth.onAuthStateChanged((userAuth) => {
      userAuth.updateProfile(userDetails).then((r) => {
        unsubscribe();
        resolve(userAuth);
      });
    }, reject);
  });
};
