import { KeyboardAvoidingView, Platform, StatusBar, ViewStyle } from 'react-native';
import React from 'react';
import { initialWindowMetrics } from 'react-native-safe-area-context';

interface UNKeyboardAvoidingViewProps {
  children: React.ReactNode,
  style?: ViewStyle,
  keyboardVerticalOffset?: number,
}

const topInset = initialWindowMetrics?.insets.top ?? 0;
const defaultOffset = topInset > 0 ? topInset : (StatusBar.currentHeight || 0);

const UNKeyboardAvoidingView = ({ children, style, keyboardVerticalOffset }: UNKeyboardAvoidingViewProps) => {
  return (
    <KeyboardAvoidingView
      style={style ?? { height: '100%' }}
      behavior="padding"
      keyboardVerticalOffset={keyboardVerticalOffset ?? defaultOffset}
      enabled={Platform.OS === 'android'}
    >
      {children}
    </KeyboardAvoidingView>
  );
};

export default UNKeyboardAvoidingView;
