import { InteractionElementsNames } from 'client/Containers/Calculation/types/elements';
import { TableNames } from 'client/Containers/Calculation/types/tables';
import { ElementParam } from 'core/types/Calculation/Store';
import { makeAutoObservable } from 'mobx';
import { ElementStatus } from 'types/elements';

type BypassParams = {
  param: ElementParam;
  target: (InteractionElementsNames | TableNames)[];
  value: ElementStatus;
};

const calculationProcess = makeAutoObservable({
  processes: new Set(),
  addProcess(process) {
    this.processes.add(process);
  },
  deleteProcess(process) {
    this.processes.delete(process);
  },
  hasProcess(process) {
    return this.processes.has(process);
  },
  noProcesses() {
    return this.processes.size === 0;
  },

  //TODO: move to substore
  bypass: {
    status: undefined,
  },
  setBypass({ param, target, value }: BypassParams) {
    this.bypass[param] = { target, value };
  },
  clearBypass(param: ElementParam) {
    this.bypass[param] = undefined;
  },
  addToBypass({ param, target }: Omit<BypassParams, 'value'>) {
    const newTarget = this.bypass[param].target.concat(target);
    this.bypass[param].target.replace(newTarget);
  },
  removeFromByPass({ param, target }: Omit<BypassParams, 'value'>) {
    const newTarget = this.bypass[param].target.filter(
      x => target.indexOf(x) < 0,
    );
    this.bypass[param].target.replace(newTarget);
  },
});

export default calculationProcess;

export type TCalculationProcess = typeof calculationProcess;
