import { InterceptorConfig } from '../InterceptorConfig';
import { registerInterceptor } from '../InterceptorGenerator';
import { InterceptorHandler } from '../InterceptorHandler';
export class ValidatorNotEmpty extends InterceptorConfig {
  constructor() {
    super('validatorNotEmpty');
  }
}
ValidatorNotEmpty.id = 'validatorNotEmpty';
export class ValidatorNotEmptyHandler extends InterceptorHandler {
  run(context, _, field, value) {
    if (value === undefined // for all types
      || value === '' // for strings
      || value === 0 // for numbers
      || (Array.isArray(value) && value.length === 0) // for arrays
    ) {
      return {
        error: context.strings.interceptors.validatorNotEmpty.message.replace('{title}', field.errorTitle || field.title),
        value,
      };
    }
    return { value };
  }
}
registerInterceptor(ValidatorNotEmpty.id, (_) => new ValidatorNotEmptyHandler());
