import { useState, useEffect } from 'react'; import { module, moduleEventEmitter } from '../module'; import { PLATFORM_ANDROID } from '../utils/constants'; import type { DocumentDetectorResponse, DocumentDetectorResult, DocumentDetectorError, DocumentDetectorSettings, } from '../types'; import { CountryCodes, Resolution, Stage } from '../types/enums'; let responseFormattedSettings: string = ''; const formattedSettings = (settings: DocumentDetectorSettings): string => { const allowedPassportList: CountryCodes[] | string[] | undefined = PLATFORM_ANDROID ? settings.allowedPassportList : settings.allowedPassportList?.map((code) => CountryCodes[code]); const resolutionSettings: Resolution | string | undefined = PLATFORM_ANDROID ? settings.resolutionSettings : Resolution[settings.resolutionSettings!]; const cafStage: Stage | string | undefined = PLATFORM_ANDROID ? settings.cafStage : Stage[settings.cafStage!]; const formatToJSON = JSON.stringify({ ...settings, allowedPassportList, cafStage, resolutionSettings, } as DocumentDetectorSettings); return formatToJSON; }; /** * @deprecated This package is deprecated since version 4.0.0 and will no longer receive updates. * Please use @caf.io/react-native-document-detector instead. * See https://docs.caf.io/caf-sdk/react-native/getting-started-with-the-sdk */ const useDocumentDetector = (settings?: DocumentDetectorSettings) => { const [response, setResponse] = useState({ result: null, error: null, cancelled: false, }); responseFormattedSettings = formattedSettings(settings!); useEffect(() => { moduleEventEmitter.addListener( 'DocumentDetector_Success', (event: DocumentDetectorResult) => { setResponse({ result: event, error: null, cancelled: false, }); }, ); moduleEventEmitter.addListener( 'DocumentDetector_Error', (event: DocumentDetectorError) => { setResponse({ result: null, error: event, cancelled: false, }); }, ); moduleEventEmitter.addListener( 'DocumentDetector_Cancel', (event: boolean) => { setResponse({ result: null, error: null, cancelled: event, }); }, ); return () => { moduleEventEmitter.removeAllListeners('DocumentDetector_Success'); moduleEventEmitter.removeAllListeners('DocumentDetector_Error'); moduleEventEmitter.removeAllListeners('DocumentDetector_Cancel'); }; }, []); return { result: response.result, error: response.error, cancelled: response.cancelled, }; }; /** * @deprecated This package is deprecated since version 4.0.0 and will no longer receive updates. * Please use @caf.io/react-native-document-detector instead. * See https://docs.caf.io/caf-sdk/react-native/getting-started-with-the-sdk */ const startDocumentDetector = ( mobileToken: string, personId: string, ): Promise => { return module.startDocumentDetector( mobileToken, personId, responseFormattedSettings, ); }; export { startDocumentDetector, useDocumentDetector };