import { InterceptorConfig } from '../InterceptorConfig';
import { registerInterceptor } from '../InterceptorGenerator';
import { InterceptorHandler } from '../InterceptorHandler';
export class ValidatorEmail extends InterceptorConfig {
  constructor() {
    super('validatorEmail');
  }
}
ValidatorEmail.id = 'validatorEmail';
const isValidEmail = (email) => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String(email).toLowerCase());
export class ValidatorEmailHandler extends InterceptorHandler {
  run(context, _values, field, value) {
    if (!value || !isValidEmail(value)) {
      return {
        error: context.strings.interceptors.validatorEmail.message.replace('{title}', field.errorTitle || field.title),
        value,
      };
    }
    return { value };
  }
}
registerInterceptor(ValidatorEmail.id, (_config) => new ValidatorEmailHandler());
