import {
  type FeatureFlagKey,
  isScopedFeatureConfig,
} from 'src/config/firebase';
import { useFeatureFlag } from 'src/contexts/FeatureFlagsContext';
import { useAuth } from 'src/contexts/JWTContext';

/**
 * @deprecated use src/hooks/useFeatureFlag instead
 */
export const useScopedFeature = (
  flagKey: Extract<FeatureFlagKey, 'ENABLE_WEB_EXPERIMENTAL_RESULTS'>,
) => {
  const { instance } = useAuth();
  const { value, isLoading } = useFeatureFlag(flagKey);

  const allowedInstances = isScopedFeatureConfig(value)
    ? value.instances
    : null;

  if (!allowedInstances) {
    return {
      isEnabled: true,
      isLoading,
    };
  }

  if (allowedInstances.length === 0) {
    return {
      isEnabled: false,
      isLoading,
    };
  }

  return {
    isEnabled: !!instance && allowedInstances.includes(instance?.id),
    isLoading: isLoading || !instance,
  };
};
