import { Expose, Transform } from 'class-transformer';
import { IsArray, IsIn, IsOptional } from 'class-validator';
import { SortableField, SortingOrder } from '../../business/constants/sortable';
import { ValidationClass } from './validationClass';

export class SortableVC extends ValidationClass {
    @IsOptional()
    @Transform(({ value }) => {
        let parsedValue: string[] | undefined = value as string[] | undefined;
        if (typeof value === 'string') parsedValue = value.split(',');
        return parsedValue ?? undefined;
    })
    @IsArray()
    @IsIn(Object.values(SortableField), { each: true })
    @Expose()
    orderBy?: SortableField[];

    @IsOptional()
    @Transform(({ value }) => {
        let parsedValue: string[] | undefined = value as string[] | undefined;
        if (typeof value === 'string') parsedValue = value.split(',').map((order) => order.toUpperCase());
        return parsedValue ?? undefined;
    })
    @IsArray()
    @IsIn(Object.values(SortingOrder), { each: true })
    @Expose()
    order?: SortingOrder[];
}
