import { useEffect, useRef } from 'react';

interface FeatureUnlockConfig {
  id: string;
  name: string;
  description: string;
  condition: () => boolean;
}

export function useFeatureUnlocks(
  features: FeatureUnlockConfig[],
  onUnlock: (feature: FeatureUnlockConfig) => void
) {
  const unlockedRef = useRef<Set<string>>(new Set());

  useEffect(() => {
    // Check each feature
    features.forEach(feature => {
      // Skip if already unlocked
      if (unlockedRef.current.has(feature.id)) {
        return;
      }

      // Check condition
      if (feature.condition()) {
        // Mark as unlocked
        unlockedRef.current.add(feature.id);

        // Notify
        onUnlock(feature);
      }
    });
  }, [features, onUnlock]);

  return {
    isUnlocked: (featureId: string) => unlockedRef.current.has(featureId)
  };
}
