//@ts-nocheck
import { initFields } from 'client/Containers/Calculation/Components/ELT/lib/resetIns';
import { isEqual } from 'lodash';
import { makeAutoObservable, toJS } from 'mobx';

const ELTStore = makeAutoObservable(
  Object.assign(
    {},
    ...['osago', 'kasko'].map(x => ({
      [x]: {
        isReseted() {
          return this.list.every(x =>
            isEqual(Object.keys(toJS(x)), initFields),
          );
        },
        reset(list) {
          this.selectedKey = undefined;
          this.list.replace(list);
        },
        list: [],
        selectedKey: undefined,
        setKey(key) {
          this.selectedKey = key;
        },

        getCompany(selectedKey) {
          if (selectedKey) return this.list.find(x => x.key === selectedKey);
          return this.list.find(x => x.key === this.selectedKey);
        },
      },
    })),
    {
      setCompanyRes(insType, index, res) {
        this[insType].list[index] = res;
      },
      addToCompanyRes(insType, index, res) {
        const companyData = this[insType].list[index];
        this[insType].list[index] = Object.assign(companyData, res);
      },
    },
  ),
);

export type TELTStore = typeof ELTStore;

export default ELTStore;
