import { ILocation , LocationType } from '@client/offline/reducer' ; import { IUserDetails , IGQLLocation , IIdentifier } from './userUtils' ; import { ISearchLocation } from '@opencrvs/components/lib/interface/LocationSearch/LocationSearch' ; import { IntlShape } from 'react-intl' ; import { locationMessages } from '@client/i18n/messages' ; export function filterLocations ( locations , allowedType , match ? ) { const filteredLocations = { } ; Object . values ( locations ) . forEach ( ( location ) => { if ( location . type === allowedType . toString ( ) && ( ! match || ! match . locationId || location [ match . locationLevel ] === match . locationId ) ) { filteredLocations [ location . id ] = location ; } } ) ; return filteredLocations ; } export function getLocation ( userDetails , locationKey ) { if ( ! userDetails . catchmentArea ) { throw Error ( 'The user has no catchment area' ) ; } const filteredArea = userDetails . catchmentArea . filter ( ( area ) => { if ( area . identifier ) { const relevantIdentifier = area . identifier . filter ( ( identifier ) => { return identifier . value === locationKey ; } ) ; return relevantIdentifier [ 0 ] ? area : false ; } else { throw Error ( 'The catchment area has no identifier' ) ; } } ) ; return filteredArea [ 0 ] ? filteredArea [ 0 ] . id : '' ; } export function generateLocationName ( location , intl ) { let name = location . name ; location . jurisdictionType && ( name += ` ${ intl . formatMessage ( locationMessages [ location . jurisdictionType ] ) || '' }` . trimEnd ( ) ) ; return name ; } function generateSearchableLocations ( locations , offlineLocations , intl ) { const generated = locations . map ( ( location ) => { let locationName = generateLocationName ( location , intl ) ; if ( location . partOf && location . partOf !== 'Location/0' ) { const locRef = location . partOf . split ( '/' ) [ 1 ] ; let parent ; if ( ( parent = offlineLocations [ locRef ] && generateLocationName ( offlineLocations [ locRef ] , intl ) ) ) { locationName += `, ${ parent }` ; } } return { id : location . id , searchableText : location . name , displayLabel : locationName } ; } ) ; return generated ; } export function generateLocations ( locations , intl , filterByJurisdictionTypes ? , filterByLocationTypes ? ) { let locationArray = Object . values ( locations ) ; if ( filterByLocationTypes ) { locationArray = locationArray . filter ( ( location ) => location . type && filterByLocationTypes . includes ( location . type as LocationType ) ) ; } if ( filterByJurisdictionTypes ) { locationArray = locationArray . filter ( ( location ) => location . jurisdictionType && filterByJurisdictionTypes . includes ( location . jurisdictionType ) ) ; } return generateSearchableLocations ( locationArray , locations , intl ) ; } export function generatePilotLocations ( pilotLocations , offlineLocations , intl ) { return generateSearchableLocations ( Object . values ( pilotLocations ) , offlineLocations , intl ) ; } export function getJurisidictionType ( locations , locationId ) { const relevantLocation = locations [ locationId ] ; if ( ! relevantLocation ) { throw new Error ( `Location ${ locationId } not found` ) ; } return relevantLocation . jurisdictionType as string ; }