/* eslint-disable object-curly-newline */
import defaultValues from 'config/default-values';
import { makeAutoObservable } from 'mobx';
import { pick } from 'radash';
import type RootStore from '../../root';
import type { CalculationValues, Values } from './types';

export default class ValuesStore {
  root: RootStore;
  values: CalculationValues = defaultValues;

  constructor(rootStore: RootStore) {
    makeAutoObservable(this);
    this.root = rootStore;
  }

  hydrate = (initialValues: CalculationValues) => {
    this.values = initialValues;
  };

  setValues = (params: { values: CalculationValues; exclude: Values[] }) => {
    const { values, exclude } = params;

    const savedValues = pick(this.values, exclude);
    this.values = { ...values, ...savedValues };
  };

  getValue<V extends Values>(valueName: V) {
    return this.values[valueName];
  }

  setValue = <V extends Values>(valueName: V, value: CalculationValues[V]) => {
    this.values[valueName] = value;
  };

  resetValue = (valueName: Values) => {
    this.setValue(valueName, defaultValues[valueName]);
  };
}
