import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, of, } from 'rxjs'; import { map, } from 'rxjs/operators'; import { languageMap, } from './language-map.const'; import { MEASUREMENTS, } from './../../../models/index'; @Injectable() export class LocationService { private _location: Observable; constructor( private _http: HttpClient, ) { this._location = of(navigator.language); } public getDefaultCurrencyCode(locationUrl: string): Observable { return this._http.get<{ currency?: { code: string, }, }>(locationUrl) .pipe( map((currencyData) => { return currencyData && currencyData.currency && currencyData.currency.code ? currencyData.currency.code : 'GBP'; }), ); } public getDefaultLanguage(): Observable { return this._location.pipe( map((location) => { const language = languageMap[location]; return language ? language : 'en'; }), ); } public getMeasurementSystem(): Observable { return this._location.pipe( map((location) => ( location === 'en-US' ? MEASUREMENTS.IMPERIAL : MEASUREMENTS.METRIC )), ); } }