import {
  registerDecorator,
  ValidationOptions,
  ValidationArguments,
} from 'class-validator';
import { LexoRank } from 'lexorank';

export function IsLexoRank(
  displayName?: string,
  validationOptions: ValidationOptions = {
    message: (args: ValidationArguments) => {
      return `${displayName ?? `'${args.property}'`} deve ser um lexo rank`;
    },
  },
) {
  return function (object: object, propertyName: string) {
    registerDecorator({
      name: 'maxDate',
      target: object.constructor,
      propertyName: propertyName,
      constraints: ['maxDate'],
      options: validationOptions,
      validator: {
        validate(value: any) {
          try {
            LexoRank.parse(value);

            return true;
          } catch {
            return false;
          }
        },
      },
    });
  };
}
