"use client";

import { useEffect } from "react";
import { useQuery } from "@tanstack/react-query";
import { setCurrency } from "@/lib/utils/currency";

export function CurrencyInit() {
  const { data } = useQuery({
    queryKey: ["user-profile"],
    queryFn: async () => {
      const res = await fetch("/api/user/profile");
      if (!res.ok) return { currency: "USD" };
      return res.json() as Promise<{ currency: string }>;
    },
    staleTime: 1000 * 60 * 10,
  });

  useEffect(() => {
    if (data?.currency) setCurrency(data.currency);
  }, [data?.currency]);

  return null;
}
