import { OfflineDictionary } from './integrations/offlineDic' ; import type { DefinitionProvider , DictionaryWord , PartOfSpeech , PartOfSpeechProvider , Synonym , SynonymProvider , } from "src/integrations/types" ; import { FreeDictionaryDefinitionProvider , FreeDictionarySynonymProvider , } from "src/integrations/freeDictionaryAPI" ; import { OpenThesaurusSynonymAPI as OpenThesaurusSynonymProvider } from "src/integrations/openThesaurusAPI" ; import { AltervistaSynonymProvider } from "src/integrations/altervistaAPI" ; import type DictionaryPlugin from "src/main" ; export default class APIManager { plugin : DictionaryPlugin ; definitionProvider : DefinitionProvider [ ] = [ new FreeDictionaryDefinitionProvider ( ) , new OfflineDictionary ( this ) , ] ; synonymProvider : SynonymProvider [ ] = [ new FreeDictionarySynonymProvider ( ) , new OpenThesaurusSynonymProvider ( ) , new AltervistaSynonymProvider ( ) , ] ; partOfSpeechProvider : PartOfSpeechProvider [ ] = [ ] ; constructor ( plugin ) { this . plugin = plugin ; } param query returns public async requestDefinitions ( query ) { const api = this . getDefinitionAPI ( ) ; const { cache , settings } = this . plugin ; if ( settings . useCaching && ! api . name . toLowerCase ( ) . contains ( "offline" ) ) { const cachedDefinition = cache . cachedDefinitions . find ( ( c ) => { return c . content . word . toLowerCase ( ) == query . toLowerCase ( ) && c . lang == settings . defaultLanguage && c . api == api . name ; } ) ; if ( cachedDefinition ) { return new Promise ( ( resolve ) => resolve ( cachedDefinition . content ) ) ; } else { const result = api . requestDefinitions ( query , settings . defaultLanguage ) ; const awaitedResult = await result ; if ( awaitedResult ) { cache . cachedDefinitions . push ( { content : awaitedResult , api : api . name , lang : settings . defaultLanguage } ) ; await this . plugin . saveCache ( ) ; } return result ; } } else { return api . requestDefinitions ( query , this . plugin . settings . defaultLanguage ) ; } } param query param pos returns public async requestSynonyms ( query , pos ? ) { const api = this . getSynonymAPI ( ) ; if ( ! api ) { throw ( "No Synonym API selected/available" ) ; } const { cache , settings } = this . plugin ; if ( settings . useCaching && ! api . name . toLowerCase ( ) . contains ( "offline" ) ) { const cachedSynonymCollection = cache . cachedSynonyms . find ( ( s ) => { return s . word . toLowerCase ( ) == query . toLowerCase ( ) && s . lang == settings . defaultLanguage && s . api == api . name ; } ) ; if ( cachedSynonymCollection ) { return new Promise ( ( resolve ) => resolve ( cachedSynonymCollection . content ) ) ; } else { const result = api . requestSynonyms ( query , settings . defaultLanguage ) ; const awaitedResult = await result ; if ( awaitedResult ) { cache . cachedSynonyms . push ( { content : awaitedResult , api : api . name , word : query , lang : settings . defaultLanguage } ) ; await this . plugin . saveCache ( ) ; } return result ; } } else { return api . requestSynonyms ( query , this . plugin . settings . defaultLanguage , pos ) ; } } param word param leftContext param rightContext returns public requestPartOfSpeech ( word , leftContext , rightContext ) { return this . getPartOfSpeechAPI ( ) ?. requestPartOfSpeech ( word , leftContext , rightContext , this . plugin . settings . defaultLanguage ) ; } returns private getDefinitionAPI ( ) { const lang = this . plugin . settings . defaultLanguage ; return this . definitionProvider . find ( ( api ) => api . name == this . plugin . settings . apiSettings [ lang ] . definitionApiName ) ; } returns private getSynonymAPI ( ) { const lang = this . plugin . settings . defaultLanguage ; return this . synonymProvider . find ( ( api ) => api . name == this . plugin . settings . apiSettings [ lang ] . synonymApiName ) ; } returns private getPartOfSpeechAPI ( ) { return this . plugin . settings . advancedSynonymAnalysis ? this . partOfSpeechProvider . find ( ( api ) => api . name == this . plugin . settings . partOfSpeechApiName ) : null ; } }