import { Transform } from 'class-transformer';
import { isArray, isString, toNumber } from 'lodash';

export const OptionalNumbersArrayOrStringToNumbersArray = (defaultValue: number[] = []) =>
    Transform(({ value }) => {
        if (value === undefined || value === null) return defaultValue;
        if (isArray(value)) return value.map((item) => toNumber(item));
        if (isString(value)) {
            if (value === '') return defaultValue;
            return value.split(',').map((stringNumber) => toNumber(stringNumber));
        }
        return defaultValue;
    });
