// Import the functions you need from the SDKs you need
import { initializeApp, getApps, getApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getDataConnect } from 'firebase/data-connect';
import { connectorConfig } from '@dataconnect/generated';

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
  authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
  projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
  storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
  appId: import.meta.env.VITE_FIREBASE_APP_ID
};

// Initialize Firebase app only once
let app;
if (getApps().length === 0) { // Check if no app is already initialized
  app = initializeApp(firebaseConfig);
} else {
  app = getApp(); // If already initialized, use the existing app
}

export const dataConnect = getDataConnect(app, connectorConfig);
export const auth = getAuth(app);

// For debugging purposes, if you previously added this, it can stay or be removed
// window.firebaseAuth = auth;