import { getQuery } from 'h3';

import { fetchMediaMessages } from '~/utils/media-repository';
import { useResponseSuccess } from '~/utils/response';

export default defineEventHandler((event) => {
  const query = getQuery(event);
  const limit =
    typeof query.limit === 'string' && query.limit.length > 0
      ? Number.parseInt(query.limit, 10)
      : undefined;
  const rawTypes = (query.types ?? query.type ?? query.fileType) as
    | string
    | undefined;
  const fileTypes = rawTypes
    ? rawTypes
        .split(',')
        .map((item) => item.trim())
        .filter((item) => item.length > 0)
    : undefined;

  const messages = fetchMediaMessages({
    limit,
    fileTypes,
  });

  return useResponseSuccess(messages);
});

