import { GoogleTranslationApiService , LanguageDetection , } from './google-translate-api-service' ; export class LanguageDetector { constructor ( private readonly service , readonly whitelist ) { } async detect ( text ) { let detectedLanguages = await this . service . detectLanguage ( text ) ; if ( this . whitelist ) { detectedLanguages = this . applyWhitelist ( detectedLanguages ) ; } detectedLanguages = this . sortByConfidence ( detectedLanguages ) ; if ( detectedLanguages . length === 0 ) { return null ; } return detectedLanguages [ 0 ] . languageCode ; } private applyWhitelist ( detectedLanguages ) { return detectedLanguages . filter ( d => this . whitelist . includes ( d . languageCode ) ) ; } private sortByConfidence ( detectedLanguages ) { return detectedLanguages . sort ( ( a , b ) => a . confidence < b . confidence ? 1 : - 1 ) ; } }