import React from "react"
import { useStaticQuery, graphql } from "gatsby"

import CardList from "./core/CardList"
import Button from "./core/Button"

interface INews {
    title?: string
    action?: {
        name?: string
        url?: string
        classname?: string
    }
    anime?: boolean
}

const News = (props: INews) => {
    const data = useStaticQuery(graphql`
        query getNews {
            directus {
                news(
                    limit: 3
                    filter: { status: { _eq: "published" } }
                    sort: "-date_published"
                ) {
                    id
                    content
                    title
                    date_published
                    image {
                        id
                        imageFile {
                            childImageSharp {
                                gatsbyImageData
                            }
                        }
                    }
                }
            }
        }
    `)

    const { title = "", anime = true, action } = props

    const news = data.directus.news.map((dtNew) => {
        return {
            name: dtNew.title,
            description: dtNew.content,
            image: dtNew.image?.imageFile,
            url: "#",
        }
    })

    return (
        <>
            <h2 className="mt-2 text-3xl font-extrabold tracking-tight sm:text-4xl mb-8">
                {title}
            </h2>
            <CardList cards={news} layout="grid" anime={anime} />
            <div className="flex justify-center mt-8">
                {action && <Button {...action} />}
            </div>
        </>
    )
}

export default News