import { Risk } from 'client/Containers/Calculation/Components/FinGAP/types';
import { makeAutoObservable, observable, toJS } from 'mobx';

const finGAP = makeAutoObservable({
  risks: observable<Risk>([]),
  setRisks(risks: Risk[]) {
    this.risks.replace(risks);
  },
  getSelectedRisks() {
    return toJS(this.risks.filter(x => this.selectedKeys.has(x.key)));
  },

  selectedKeys: new Set() as Set<string>,
  setSelectedKeys(keys: string[]) {
    this.selectedKeys = new Set(keys);
  },

  totalSum() {
    return this.risks
      .filter(risk => this.selectedKeys.has(risk.key))
      .reduce((sum, row) => sum + row.premium, 0);
  },

  clear() {
    this.risks.clear();
    this.selectedKeys.clear();
  },
});

export default finGAP;

export type TFinGAP = typeof finGAP;
