import { Transform } from 'class-transformer';

export function TransformBoolean(): PropertyDecorator {
  return Transform(({ value }) => {
    if (value === undefined || value === '') return undefined;

    if (typeof value === 'boolean') return value;

    if (['true', 'on', 'yes', '1'].includes(value.toString().toLowerCase())) {
      return true;
    }

    if (['false', 'off', 'no', '0'].includes(value.toString().toLowerCase())) {
      return false;
    }

    return undefined;
  });
}
