import { InterceptorConfig } from '../InterceptorConfig';
import { registerInterceptor } from '../InterceptorGenerator';
import { InterceptorHandler } from '../InterceptorHandler';
export class ValidatorRegex extends InterceptorConfig {
  constructor(regex, message) {
    super('validatorAlphanumericChar');
    this.regexFlags = 'gm';
    this.regex = regex;
    this.message = message;
  }
}
ValidatorRegex.id = 'validatorAlphanumericChar';
export class ValidatorRegexHandler extends InterceptorHandler {
  constructor(config) {
    super();
    this.config = config;
  }
  run(context, _, field, value) {
    if (typeof value === 'string' || value instanceof String) {
      if (!new RegExp(this.config.regex, this.config.regexFlags).test(value)) {
        const errorMessage = this.config.message || context.strings.interceptors.validatorRegex.message;
        return {
          error: errorMessage.replace('{title}', field.errorTitle || field.title),
          value,
        };
      }
    }
    return { value };
  }
}
registerInterceptor(ValidatorRegex.id, (config) => new ValidatorRegexHandler(config));
