'use client';

import { useEffect } from 'react';
import { useCookieConsentStore } from '@/store/cookieConsentStore';

declare function gtag(...args: unknown[]): void;

export default function GoogleAnalyticsConsent() {
  const { analytics, hasHydrated } = useCookieConsentStore();

  useEffect(() => {
    if (!hasHydrated) return;
    if (typeof gtag === 'undefined') return;

    gtag('consent', 'update', {
      analytics_storage: analytics ? 'granted' : 'denied',
      ad_storage: analytics ? 'granted' : 'denied',
    });
  }, [analytics, hasHydrated]);

  return null;
}
