import {ImageSource} from 'expo-image';
import {ImageSourcePropType} from 'react-native';

export const extractUriFromSource = (
  source: ImageSourcePropType | ImageSource,
): string | undefined => {
  if (!source) {
    return undefined;
  }

  if (typeof source === 'string') {
    return source;
  }

  if (typeof source === 'number') {
    return undefined;
  }

  if ('uri' in source && typeof source.uri === 'string') {
    return source.uri;
  }

  if (Array.isArray(source) && source.length > 0) {
    return extractUriFromSource(source[0]);
  }

  return undefined;
};
