import {
  AbstractControl,
  FormGroup,
  ValidationErrors,
  ValidatorFn,
} from '@angular/forms';

export class CrossValidator {
  public crossValidate() {
    return (control: AbstractControl): ValidationErrors | null => {
      if (!(control instanceof FormGroup)) {
        return null;
      }

      const contactType = control.get('contactType')?.value;

      if (contactType == ('None' || null)) return null;

      let error = null;
      if (contactType == 'Email') {
        error =
          control.get('email')?.value != control.get('emailConfirmation')?.value
            ? { mismatchEmail: true }
            : null;
      } else {
        error =
          control.get('phone')?.value != control.get('phoneConfirmation')?.value
            ? { mismatchPhone: true }
            : null;
      }

      console.log('Error: ', error);
      return error;
    };
  }
}
