import type RootStore from '../../root'; import type { CalculationValues, Values } from './types'; import defaultValues from '@/config/default-values'; import { makeAutoObservable, toJS } from 'mobx'; import { pick } from 'radash'; export default class ValuesStore { private root: RootStore; private values = defaultValues; constructor(rootStore: RootStore) { makeAutoObservable(this); this.root = rootStore; } public hydrate = (initialValues: CalculationValues) => { this.values = { ...defaultValues, ...initialValues }; }; public getValues = (keys?: K[]) => { if (keys) return pick(this.values, keys); return toJS(this.values); }; public setValues = (values: Partial) => { (Object.keys(values) as Array).forEach((valueName) => { const value = values[valueName]; if (value !== undefined) { this.setValue(valueName, value); } }); }; public getValue(valueName: V) { return this.values[valueName]; } public setValue = (valueName: V, value: CalculationValues[V]) => { this.values[valueName] = value; }; public resetValue = (valueName: Values) => { this.setValue(valueName, defaultValues[valueName]); }; }