// Initialize Firebase using the compat scripts loaded on the page (lib/*-compat.js)
// The compat libs expose a global `firebase` object, so we must not use ESM imports here.

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "AIzaSyDfELz4NLAg9WQm5CbHEPdbDnxdc_uQxro",
  authDomain: "fortipas-67.firebaseapp.com",
  databaseURL: "https://fortipas-67-default-rtdb.asia-southeast1.firebasedatabase.app",
  projectId: "fortipas-67",
  storageBucket: "fortipas-67.firebasestorage.app",
  messagingSenderId: "958794867745",
  appId: "1:958794867745:web:d741beda0ee17294022f60",
  measurementId: "G-64ERRGFY29"
};

// Initialize Firebase
// Use the compat global API
const app = firebase.initializeApp(firebaseConfig);

// Initialize analytics only if the SDK was included and the method exists
if (typeof firebase.analytics === 'function') {
  try {
    firebase.analytics();
  } catch (e) {
    console.warn('Firebase analytics initialization failed:', e);
  }
}

// Expose compat-style auth and firestore references expected by popup.js
try {
  const fb_auth = firebase.auth();
  const fb_db = firebase.firestore();
  // Make them globally accessible (popup.js expects these names)
  window.fb_auth = fb_auth;
  window.fb_db = fb_db;
  // Ensure Firebase Auth persistence (so Firebase remembers the signed-in user)
  try {
    if (fb_auth && fb_auth.setPersistence && firebase && firebase.auth && firebase.auth.Auth && firebase.auth.Auth.Persistence) {
      fb_auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL).then(() => {
        console.debug('Firebase auth persistence set to LOCAL');
      }).catch((err) => {
        console.warn('Could not set Firebase auth persistence:', err);
      });
    }
  } catch (err) {
    console.warn('Error setting Firebase auth persistence:', err);
  }

} catch (e) {
  console.warn('Firebase auth/firestore not available:', e);
}