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

export function passwordStrengthValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const value = control.value;
    if (!value) {
      return null; // Don't validate empty value
    }
    const hasUpperCase = /[A-Z]/.test(value);
    const hasNumber = /\d/.test(value);
    //Special character validation commented out until Chrome decides to generate passwords with special characters -.-
    //const hasSymbol = /[!@#$%^&*(),.?":{}|<>-]/.test(value);

    const valid = hasUpperCase && hasNumber;
    return valid ? null : { passwordStrength: true };
  };
}

export function hasUpperCaseValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const value = control.value;
    if (!value) {
      return null;
    }
    const hasUpperCase = /[A-Z]/.test(value);
    //Special character validation commented out until Chrome decides to generate passwords with special characters -.-
    //const hasSymbol = /[!@#$%^&*(),.?":{}|<>-]/.test(value);

    const valid = hasUpperCase;
    return valid ? null : { hasUpperCase: true };
  };
}

export function hasNumberValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const value = control.value;
    if (!value) {
      return null;
    }
    const hasNumber = /\d/.test(value);
    //Special character validation commented out until Chrome decides to generate passwords with special characters -.-
    //const hasSymbol = /[!@#$%^&*(),.?":{}|<>-]/.test(value);

    const valid = hasNumber;
    return valid ? null : { hasNumber: true };
  };
}
