import { useState, useEffect } from 'react';

export function useReadmeWaka() {
  const [wakaContent, setWakaContent] = useState<string>('');
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    const fetchWakaContent = async () => {
      try {
        const response = await fetch(
          'https://raw.githubusercontent.com/imnyang/imnyang/refs/heads/main/README.md'
        );
        if (!response.ok) throw new Error('Failed to fetch README');
        
        const text = await response.text();
        const startMarker = '<!--START_SECTION:waka-->';
        const endMarker = '<!--END_SECTION:waka-->';
        
        const startIdx = text.indexOf(startMarker);
        const endIdx = text.indexOf(endMarker);
        
        if (startIdx !== -1 && endIdx !== -1) {
          const content = text.slice(startIdx + startMarker.length, endIdx).trim();
          setWakaContent(content);
        } else {
          setError('Could not find waka section');
        }
      } catch (err) {
        setError(err instanceof Error ? err.message : 'Unknown error');
      } finally {
        setIsLoading(false);
      }
    };

    fetchWakaContent();
  }, []);

  return { wakaContent, isLoading, error };
}
