import { ElementsNames } from 'client/Containers/Calculation/types/elements';
import { ValidationCondition } from 'core/validation/validate';
import { omit } from 'lodash';
import { conditions as kaskoConditions } from '../../Kasko/lib/validation';

export const requiredFields: ElementsNames[] = [
  'selectBrand',
  'selectModel',
  'tbxLeaseObjectYear',
  'tbxLeaseObjectMotorPower',
  'tbxLeasingPeriod',
  'tbxLeaseObjectPrice',
  'selectLeaseObjectUseFor',
  'tbxMaxMass',
  'tbxCountSeats',
  'cbxWithTrailer',
  'tbxINNForCalc',
  'radioObjectRegistration',
  'radioInfuranceOPF',
];

const osagoConditions: Partial<Record<ElementsNames, ValidationCondition>> = {
  selectLeaseObjectType: calculationStore => {
    const leaseObjectType = calculationStore.getOption('selectLeaseObjectType');
    // Проверяем на мотоцикл
    if (leaseObjectType?.evo_id && ['11'].includes(leaseObjectType?.evo_id)) {
      return {
        isValid: false,
        message: `По данному Типу предмета лизинга возможен только индивидуальный запрос тарифов КАСКО и ОСАГО. Просьба обратиться на адрес strakhovka@evoleasing.ru`,
      };
    }

    if (
      leaseObjectType?.evo_id &&
      !['1', '2', '3', '6', '7', '9', '10'].includes(leaseObjectType?.evo_id)
    ) {
      return {
        isValid: false,
        message: `Для выбранной категории ТС расчет в ЭЛТ недоступен`,
      };
    }
    return {
      isValid: true,
    };
  },
  selectTownRegistration: calculationStore => {
    const objectRegistration = calculationStore.getValue('objectRegistration');
    if (objectRegistration === 100000001) {
      const townRegistration = calculationStore.getValue('townRegistration');
      if (!townRegistration) {
        return {
          isValid: false,
          message: 'Не указан Город регистрации',
        };
      }
    }
    return {
      isValid: true,
    };
  },
};

const conditions = Object.assign(
  osagoConditions,
  omit(kaskoConditions, [
    'selectEngineType',
    'selectLeaseObjectType',
    'selectLeasingWithoutKasko',
  ]),
);

export const resetFields: ElementsNames[] = [
  ...requiredFields,
  ...(Object.keys(conditions) as ElementsNames[]),
  'selectLegalClientTown',
  'selectLegalClientRegion',
  'radioObjectRegistration',
  'selectTownRegistration',
];

export default {
  requiredFields,
  conditions,
};
