import { Entry } from 'contentful'
import contentfulClient from './contentfulClient'
import { parseContentfulContentImage } from './contentImage'
import {BlogPost, BlogPostEntry, BlogPostSkeleton} from "../../types/blogPost";
import {parseContentfulAuthor} from "./authors";
import dayjs from "dayjs";
import {BlogPostCategory, BlogPostCategoryEntry, BlogPostCategorySkeleton} from "../../types/blogPostCategory";

type ListEntry = BlogPostCategoryEntry<undefined, string>



export function parseContentfulBlogPostCategory(entry?: ListEntry): BlogPostCategory | null {
    if (!entry) {
        return null
    }
    return {
        title: entry.fields.title || '',
        slug: entry.fields.slug || ''
    }
}

interface FetchListOptions {
    preview: boolean
    local?: string
}
export async function fetchBlogPostCategories({ preview }: FetchListOptions): Promise<BlogPostCategory[]> {
    const contentful = contentfulClient({ preview })

    const results = await contentful.getEntries<BlogPostCategorySkeleton>({
        content_type: 'blogPostCategory',
        order: ['fields.title'],
    })

    return results.items.map((entry) => parseContentfulBlogPostCategory(entry) as BlogPostCategory)
}
