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

export function genderLastNameValidator(isB2b: boolean): ValidatorFn | null {
  if (isB2b) {
    return (control: FormGroup): ValidationErrors | null => {
      const gender = control.get('gender').value;
      const lastName = control.get('lastName').value;
      if (!!lastName && gender === 0) {
        control.get('gender').setValidators([Validators.required, Validators.min(1)]);
        return { genderNotSet: true };
      } else {
        control.get('gender').setValidators(null);
        return null;
      }
    };
  } else {
    return null;
  }
}
