import Container from "@/components/Container/Container";
import Section from "@/components/Section/Section";
import { Fragment, ReactNode } from "react";
import Post from "@/components/Post/Post";

const Listing = ({
  posts = [],
  children,
  postBasePath = "/insights",
}: {
  posts: any[];
  children?: ReactNode;
  postBasePath?: string;
}) => {
  if (!posts) return <></>;

  return (
    <Container>
      <Section
        gridArea="wide"
        className="grid auto-rows-auto grid-cols-1 lg:grid-cols-3 gap-y-sm gap-x-md"
      >
        {posts.map((post: any, index: number) => (
          <Fragment key={index}>
            <Post
              author={post.author_name}
              category={post.tags_rendered[0] || "web"}
              excerpt={post.excerpt_truncated}
              slug={post.slug}
              title={post.title.rendered}
              imageUrl={post.randomised_media_by_tag.src}
              imageAlt={post.randomised_media_by_tag.alt}
              basePath={postBasePath}
            />
          </Fragment>
        ))}
        {children}
      </Section>
    </Container>
  );
};

export default Listing;
