import type { CollectionEntry } from 'astro:content';

export const getUniqueCategories = (
  courses: Array<CollectionEntry<'courses'>>
) => {
  const coursesEntries = courses.flatMap(
    // @ts-ignore
    // eslint-disable-next-line no-unsafe-optional-chaining
    (course: CollectionEntry<'courses'>) => [...course?.data?.categories]
  );
  return [...new Set(coursesEntries)];
};

export const getUniqueLevels = (courses: CollectionEntry<'courses'>[]) => {
  const coursesEntries = courses.map(
    (course: CollectionEntry<'courses'>) => course.data.level
  );
  return [...new Set(coursesEntries)];
};

export const sortCoursesByDate = (posts: CollectionEntry<'courses'>[]) =>
  posts
    .filter(({ data }) => !data?.draft)
    .sort(
      (a, b) =>
        Math.floor(new Date(b?.data?.date).getTime() / 1000) -
        Math.floor(new Date(a?.data?.date).getTime() / 1000)
    );
