import { NativeModules } from 'react-native' ; import { BatchUserEditor } from './BatchUserEditor' ; import { BatchEventData } from './BatchEventData' ; import { isString , isNumber } from './helpers/TypeHelpers' ; import Log from './helpers/Logger' ; const RNBatch = NativeModules . RNBatch ; /**
 * Represents a locations, using lat/lng coordinates
 */ export interface Location { /**
     * Latitude
     */ latitude : number ; /**
     * Longitude
     */ longitude : number ; /**
     * Date of the tracked location
     */ date ? : Date ; /**
     * Precision radius in meters
     */ precision ? : number ; } /**
 * Batch's user module
 */ export const BatchUser = { /**
     * Get the unique installation ID, generated by Batch. Batch must be started to read it.
     * You will get the result in a promise.
     */ getInstallationID : ( ) : Promise < string > => RNBatch . userData_getInstallationId ( ) , /**
     * Creates an editor for the user profile
     * The profile is not updated until the method `save()` is called
     */ editor : ( ) : BatchUserEditor => new BatchUserEditor ( ) , param name param label param data trackEvent : ( name , label ? , data ? ) : void => { RNBatch . userData_trackEvent ( name , isString ( label ) ? label : null , data instanceof BatchEventData ? data [ '_toInternalRepresentation' ] ( ) : null ) ; } , param amount param data trackTransaction : ( amount , data ? ) : void => { if ( typeof amount === 'undefined' ) { Log ( false , 'BatchUser - Amount must be a valid number. Ignoring transaction.' ) ; return ; } if ( ! isNumber ( amount ) || isNaN ( amount ) ) { Log ( false , 'BatchUser - Amount must be a valid number. Ignoring transaction.' ) ; return ; } if ( typeof data !== 'object' ) { data = null ; } RNBatch . userData_trackTransaction ( amount , data ) ; } , param location trackLocation : ( location ) : void => { if ( typeof location !== 'object' ) { Log ( false , 'BatchUser - Invalid trackLocation argument. Skipping.' ) ; return ; } if ( typeof location . latitude !== 'number' || isNaN ( location . latitude ) ) { Log ( false , 'BatchUser - Invalid latitude. Skipping.' ) ; return ; } if ( typeof location . longitude !== 'number' || isNaN ( location . longitude ) ) { Log ( false , 'BatchUser - Invalid longitude. Skipping.' ) ; return ; } if ( location . precision && ( typeof location . precision !== 'number' || isNaN ( location . precision ) ) ) { Log ( false , 'BatchUser - Invalid precision. Skipping.' ) ; return ; } if ( location . date && ! ( location . date instanceof Date ) ) { Log ( false , 'BatchUser - Invalid date. Skipping.' ) ; return ; } RNBatch . userData_trackLocation ( { date : location . date ? location . date . getTime ( ) : undefined , latitude : location . latitude , longitude : location . longitude , precision : location . precision , } ) ; } , } ;