import { getArchiveTree } from '~~/lib/blog/read-posts';
import type { BlogPost } from '~~/lib/types/post';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const include = (content: BlogPost) => {
    if (query.tag) {
      const tags = Array.isArray(query.tag) ? query.tag : [query.tag];
      if (
        !content.tags?.length ||
        !tags.every((tag) => content.tags.includes(tag))
      ) {
        return false;
      }
    }

    return true;
  };

  return getArchiveTree(include);
});
