/**
 * Copyright (c) 2020 BlockDev AG
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import { action , computed , makeObservable , observable , runInAction } from "mobx" ; import { DNSOption , QualityLevel } from "mysterium-vpn-js" ; import * as termsPackageJson from "@mysteriumnetwork/terms/package.json" ; import * as _ from "lodash" ; import { RootStore } from "../store" ; import { log } from "../../shared/log/log" ; import { tequilapi } from "../tequilapi" ; import { locations } from "../navigation/locations" ; export interface Config { desktop : DesktopConfig ; payments ? : { consumer ? : { "price-gib-max" ? : number ; "price-hour-max" ? : number ; } ; } ; } export interface DesktopConfig { "terms-agreed" ? : { at ? : string ; version ? : string ; } ; onboarded ? : boolean ; dns ? : DNSOption ; "nat-compatibility" ? : "auto" | "off" ; "quick-connect" ? : boolean ; filters ? : ProposalFilters ; } export interface ProposalFilters { preset ? : { id ? : number | null ; } ; quality ? : { "include-failed" ? : boolean ; level ? : QualityLevel ; } ; other ? : { country ? : string | null ; } ; } export interface PriceCeiling { perGibMax : number ; } export class ConfigStore { config : Config = { desktop : { } } ; loaded = false ; root : RootStore ; constructor ( root ) { this . root = root ; makeObservable ( this , { config : observable , loaded : observable , loadConfig : action , updateConfigPartial : action , persistConfig : action , persistConfigDebounced : action , currentTermsAgreed : computed , agreeToTerms : action , dnsOption : computed , setDnsOption : action , autoNATCompatibility : computed , setAutoNATCompatibility : action , onboarded : computed , setOnboarded : action , quickConnect : computed , setQuickConnect : action , } ) ; } loadConfig = async ( ) : Promise < void > => { const config = await tequilapi . userConfig ( ) ; runInAction ( ( ) => { this . config = { desktop : { } , ... config . data , } ; log . info ( "Using config:" , JSON . stringify ( this . config ) ) ; } ) ; runInAction ( ( ) => { this . loaded = true ; } ) ; } ; updateConfigPartial = async ( desktopConfig ) : Promise < void > => { this . config . desktop = _ . merge ( { } , this . config . desktop , desktopConfig ) ; return this . persistConfigDebounced ( ) ; } ; persistConfig = async ( ) : Promise < void > => { const cfg = this . config ; log . info ( "Persisting user configuration:" , JSON . stringify ( cfg ) ) ; await tequilapi . updateUserConfig ( { data : cfg , } ) ; } ; persistConfigDebounced = _ . debounce ( this . persistConfig , 2000 ) ; get currentTermsAgreed ( ) : boolean { const version = this . config . desktop ?. [ "terms-agreed" ] ?. version ; const at = this . config . desktop ?. [ "terms-agreed" ] ?. at ; return ! ! version && ! ! at && version == termsPackageJson . version ; } agreeToTerms = async ( ) : Promise < void > => { await this . updateConfigPartial ( { "terms-agreed" : { version : termsPackageJson . version , at : new Date ( ) . toISOString ( ) , } , } ) ; if ( this . onboarded ) { this . root . navigation . goHome ( ) ; } else { this . root . router . push ( locations . onboardingIntro ) ; } } ; get onboarded ( ) : boolean { return this . config . desktop ?. onboarded ?? false ; } setOnboarded = async ( ) : Promise < void > => { return this . updateConfigPartial ( { onboarded : true } ) ; } ; get dnsOption ( ) : DNSOption { return this . config . desktop ?. dns ?? "provider" ; } setDnsOption = async ( dns ) : Promise < void > => { return this . updateConfigPartial ( { dns } ) ; } ; get autoNATCompatibility ( ) : boolean { return this . config . desktop ?. [ "nat-compatibility" ] !== "off" ; } setAutoNATCompatibility = async ( enabled ) : Promise < void > => { return this . updateConfigPartial ( { "nat-compatibility" : enabled ? "auto" : "off" } ) ; } ; get quickConnect ( ) : boolean { return this . config . desktop ?. [ "quick-connect" ] !== false ; } setQuickConnect = async ( enabled ) : Promise < void > => { return this . updateConfigPartial ( { "quick-connect" : enabled } ) ; } ; }