import Container from "@/components/Container/Container";
import Section from "@/components/Section/Section";
import Post from "@/components/Post/Post";
import { getCachedPostsByIds } from "@/services/wp";

const RelatedInsights = async (props: { [rest: string]: any }) => {
  const postIds = [];

  for (let i = 0; i < props.insights; i++) {
    const key = `insights_${i}_insight`;

    // @ts-ignore
    if (props[key]) {
      // @ts-ignore
      postIds.push(props[key]);
    }
  }

  const posts = await getCachedPostsByIds(postIds);


  if (!posts) return <></>;

  return (
    <Container id={props.anchor}>
      <Section gridArea="wide" className="grid gap-sm grid-cols-3">
        <h3 className="h1 col-span-3 ps-lg">{props.title}</h3>
        {posts.map((post: any) => (
          <Post
            key={post.id}
            title={post.title.rendered}
            imageUrl={""}
            author={"Dale"}
            category={"Development"}
            excerpt={
              "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sed vulputate nulla, in accumsan risus. Sed at felis diam. Nullam iaculis arcu a erat consequat."
            }
            slug={post.slug}
          />
        ))}
      </Section>
    </Container>
  );
};

export default RelatedInsights;
