import { useEffect, useState } from 'react';
import { Keyboard, Platform } from 'react-native';

/**
 * Liefert die aktuelle Keyboard-Höhe in px (0 wenn versteckt).
 *
 * Pattern aus components/PostCommentsSheet.tsx — iOS nutzt `keyboardWillShow`
 * für glatte Animation, Android `keyboardDidShow` weil iOS-Will-Events dort nicht feuern.
 *
 * Für Standard-Forms reicht `<KeyboardAdjustedView>` (das nutzt diesen Hook intern).
 * Direkten Hook-Zugriff nur wenn man die Höhe selbst irgendwo einrechnen muss
 * (z.B. SOS-Chat mit FlatList + Input-Bar im selben Layout).
 */
export function useKeyboardHeight(): number {
  const [keyboardHeight, setKeyboardHeight] = useState(0);

  useEffect(() => {
    const showEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow';
    const hideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide';
    const showSub = Keyboard.addListener(showEvent, (e) => {
      setKeyboardHeight(e.endCoordinates.height);
    });
    const hideSub = Keyboard.addListener(hideEvent, () => {
      setKeyboardHeight(0);
    });
    return () => {
      showSub.remove();
      hideSub.remove();
    };
  }, []);

  return keyboardHeight;
}
