// Import the functions you need from the SDKs you need
import { initializeApp, getApps, getApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getMessaging, isSupported } from "firebase/messaging";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "AIzaSyAJpHPX2cjwxVmUCYoAWzhWtcuuV1dnfP4",
  authDomain: "goldapp-bdaf1.firebaseapp.com",
  projectId: "goldapp-bdaf1",
  storageBucket: "goldapp-bdaf1.firebasestorage.app",
  messagingSenderId: "581623454822",
  appId: "1:581623454822:web:1a56f3c2755d94bab961a7",
  measurementId: "G-ZT5NM5QFTK"
};

// Initialize Firebase (prevent re-initializing during hot reloads)
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);

// Initialize Analytics conditionally (only in browser environment)
let analytics = null;
if (typeof window !== "undefined") {
  analytics = getAnalytics(app);
}

export { app, analytics };

// Initialize Firebase Cloud Messaging conditionally
export const setupMessaging = async () => {
  if (typeof window !== "undefined") {
    const supported = await isSupported();
    if (supported) {
      return getMessaging(app);
    }
  }
  return null;
};
