import { reaction } from 'mobx';
import type { ReactionsContext } from 'process/types';

export default function validationReactions({ store }: ReactionsContext) {
  const { $tables } = store;

  reaction(
    () => {
      const hasPaymentsErrors = $tables.payments.validation.hasErrors;
      const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany');

      return {
        hasPaymentsErrors,
        finGAPInsuranceCompany,
      };
    },
    ({ hasPaymentsErrors, finGAPInsuranceCompany }) => {
      $tables.fingap.validate({
        invalid: finGAPInsuranceCompany !== null && hasPaymentsErrors,
        message: 'Неверно заполнены платежи',
      });
    },
    {
      fireImmediately: true,
    }
  );
}
