import { useState, useEffect } from "react";
import { supabase } from "@/integrations/supabase/client";

let cachedKey: string | null = null;

export function useGoogleMapsKey() {
  const [apiKey, setApiKey] = useState(cachedKey || "");
  const [loading, setLoading] = useState(!cachedKey);

  useEffect(() => {
    if (cachedKey) return;
    (async () => {
      try {
        const { data, error } = await supabase.functions.invoke("get-google-maps-key");
        if (!error && data?.key) {
          cachedKey = data.key;
          setApiKey(data.key);
        }
      } catch {
        // silent
      } finally {
        setLoading(false);
      }
    })();
  }, []);

  return { apiKey, loading };
}
