import {
  registerDecorator,
  ValidationOptions,
  ValidationArguments,
} from 'class-validator';
import { Assignee } from '../../features/tasks/types/types';
/**
 * Validations options
 * @param [validationOptions]
 * @returns
 */
export const IsAssignees = (validationOptions?: ValidationOptions) => {
  return (object: any, propertyName: string) => {
    registerDecorator({
      name: 'IsAssignees',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: {
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        validate: (values: Assignee[], args: ValidationArguments) => {
          return values.every((value) => {
            const { typistUserId, typistGroupId, typistName } = value;
            if (typistUserId === undefined && typistGroupId === undefined) {
              return false;
            }
            if (typistUserId !== undefined && typistGroupId !== undefined) {
              return false;
            }
            if (!typistName) {
              return false;
            }
            return true;
          });
        },
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        defaultMessage: (args?: ValidationArguments): string => {
          return 'Request body is invalid format';
        },
      },
    });
  };
};
