import { Component, ComponentType, ForwardedRef, forwardRef } from 'react';
import { View } from 'react-native';
import { VideoProperties } from './types';

class FallbackComponent extends Component<VideoProperties & { innerRef: ForwardedRef<View> }> {
  render() {
    return <View ref={this.props.innerRef} />;
  }
}

const ForwardedComponent = forwardRef<View, VideoProperties>((props: VideoProperties, ref) => (
  <FallbackComponent {...props} innerRef={ref} />
));

function getVideoInstance(): ComponentType<VideoProperties> {
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    return require('@react-native-bedrock/native/react-native-video')?.default;
  } catch {
    return ForwardedComponent;
  }
}

export const Video = getVideoInstance();

export const isAvailable = Video !== ForwardedComponent;
