import { IReactionEffect } from 'core/types/Calculation/Store/effect';
import { Process } from 'core/types/Calculation/Store/process';
import { get } from 'lodash';

const mapObjectCategoryToUseFor = {
  100000000: [100000000, 100000002],
  100000001: [
    100000000,
    100000001,
    100000002,
    100000003,
    100000004,
    100000005,
    100000006,
    100000007,
    100000008,
    100000009,
    100000010,
    100000011,
    100000012,
    100000013,
  ],
  100000002: [100000002, 100000004, 100000005, 100000006, 100000009, 100000010],
  100000003: [
    100000002,
    100000004,
    100000006,
    100000007,
    100000008,
    100000011,
    100000012,
    100000013,
  ],
  100000004: [100000002, 100000009],
  null: [100000014, 100000015, 100000016, 100000017, 100000018, 100000019],
  undefined: [100000014, 100000015, 100000016, 100000017, 100000018, 100000019],
};

const insuranceReactions: IReactionEffect[] = [
  calculationStore => ({
    expression: () => {
      return calculationStore.getValue('leaseObjectCategory');
    },
    effect: leaseObjectCategory => {
      const allowedObjectUseFor: number[] =
        mapObjectCategoryToUseFor[leaseObjectCategory];
      calculationStore.setFilter('selectLeaseObjectUseFor', options =>
        options.filter(
          option =>
            option.value &&
            typeof option.value === 'number' &&
            allowedObjectUseFor?.includes(option.value),
        ),
      );
    },
  }),

  (calculationStore, calculationProcess) => ({
    expression: () => {
      return calculationStore.getValue('INNForCalc');
    },
    effect: INNForCalc => {
      if (calculationProcess.hasProcess(Process.LoadKp)) {
        return;
      }

      const mapINNForCalcToInfuranceOPF = {
        10: 100000000,
        12: 100000001,
      };

      const OPFValue = get(
        mapINNForCalcToInfuranceOPF,
        INNForCalc ? INNForCalc.toString().length : 0,
        null,
      );

      calculationStore.setValue('infuranceOPF', OPFValue);
    },
  }),
];

export default insuranceReactions;
