import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface,
} from 'class-validator';
import { TASK_STATUS } from '../../constants';

@ValidatorConstraint()
export class IsStatusConstraint implements ValidatorConstraintInterface {
  private readonly STATUS: string[] = Object.values(TASK_STATUS);
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  validate(value: string, args: ValidationArguments): boolean {
    if (value) {
      // ,で分割した文字列のすべてがTASK_STATUSのプロパティに存在する値であった場合のみtrue
      return value.split(',').every((state) => this.STATUS.includes(state));
    } else {
      return false;
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  defaultMessage(validationArguments?: ValidationArguments): string {
    return `invalid status string`;
  }
}
/**
 * ,で分割した文字列のすべてがTASK_STATUSのプロパティに存在する値であるかをチェックする
 * @param [validationOptions]
 * @returns
 */
export function IsStatus(validationOptions?: ValidationOptions) {
  return (object: object, propertyName: string) => {
    registerDecorator({
      name: 'IsStatus',
      target: object.constructor,
      propertyName,
      options: validationOptions,
      validator: IsStatusConstraint,
    });
  };
}
