import {useMemo} from 'react';
import {CameraDevice, useCameraDevices} from 'react-native-vision-camera';

/**
 * Picks the iOS back camera with the most physical cameras
 * (triple > dual > wide/tele > single)
 */
export function useBestIPhoneDevice(): CameraDevice | undefined {
  const devices = useCameraDevices().filter(
    device => device.position === 'back',
  );

  return useMemo(() => {
    // devices[] contains grouped physical cameras
    const sorted = [...devices].sort(
      (a, b) => b.physicalDevices.length - a.physicalDevices.length,
    );
    return sorted[0];
  }, [devices]);
}
