/* eslint-disable lodash/prefer-lodash-method */
import {object, string, lazy} from 'yup';

const schema = object().shape(
  {
    name: string()
      .trim()
      .max(30)
      .required(),
    phone: string().when('email', {
      is: '',
      then: string()
        .trim()
        .min(8)
        .max(16)
        .matches(/^\+[0-9]+$/)
        .required(),
      otherwise: lazy(value => {
        if (value === '') return string();

        return string()
          .trim()
          .min(8)
          .max(16)
          .matches(/^\+[0-9]+$/);
      })
    }),
    email: string()
      .trim()
      .email()
      .when('phone', {
        is: '',
        then: string().required(),
        otherwise: string()
      })
  },
  [['email', 'phone']]
);

export default schema;
