'use client';

import { useQuery } from '@tanstack/react-query';

import { apiFetch } from '@/lib/api/client';

/**
 * Returns true when the given feature flag is enabled for the current port.
 * Result is cached for 5 minutes.
 */
export function useFeatureFlag(key: string): boolean {
  const { data } = useQuery<{ enabled: boolean }>({
    queryKey: ['feature-flag', key],
    queryFn: () => apiFetch(`/api/v1/settings/feature-flag?key=${encodeURIComponent(key)}`),
    staleTime: 300_000, // 5 min
  });
  return data?.enabled ?? false;
}
