import { Injectable } from '@angular/core' ; import { StorageCacheService } from '@app/cache' ; import { CurrencyOverviewHttpService , CurrencyOverviewType } from '@data/poe-ninja' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { CurrencyChaosEquivalents } from './currency-chaos-equivalents' ; const CACHE_EXPIRY = 1000 * 60 * 30 ; @ Injectable ( { providedIn : 'root' } ) export class CurrencyChaosEquivalentsProvider { constructor ( private readonly currencyOverview , private readonly cache ) { } public provide ( leagueId ) { const key = `currency_chaos_equivalents_${ leagueId }` ; return this . cache . proxy ( key , ( ) => this . fetch ( leagueId ) , CACHE_EXPIRY ) ; } private fetch ( leagueId ) { return this . currencyOverview . get ( leagueId , CurrencyOverviewType . Currency ) . pipe ( map ( response => { const currencyChaosEquivalents = { } ; response . lines . forEach ( line => { currencyChaosEquivalents [ line . currencyTypeName ] = line . chaosEquivalent ; } ) ; currencyChaosEquivalents [ 'Chaos Orb' ] = 1 ; return currencyChaosEquivalents ; } ) ) ; } }