import { useSelector } from 'react-redux';
import type { RootState } from '@/store/store';

/**
 * Returns whether the feature identified by `slug` is enabled.
 * Defaults to `true` while flags are still loading (avoids a flash
 * of "blocked" UI before the first fetch resolves).
 */
export function useFeatureFlag(slug: string): boolean {
  const { featureControls, status } = useSelector((state: RootState) => state.featureControls);

  if (status === 'loading' || featureControls.length === 0) return true;

  const flag = featureControls.find((f) => f.slug === slug);
  return flag ? flag.is_enabled : true;
}
