import { Parish } from '@/payload-types'
import { Container } from '@/components/Container/Container'
import { HTMLText } from '@/components/Text/HTMLText'
import { Section } from '@/components/Section/Section'
import { Button } from '@/components/Button/Button'
import { DonationForm } from '@/components/DonationForm/DonationForm'
import { YoutubePlayer } from '@/components/YoutubePlayer/YoutubePlayer'
import { DonationAppeal } from '@/components/DonationAppeal/DonationAppeal'

type BlocksProps = {
  content: Parish['content']
}

export function ParishBlocks({ content }: BlocksProps) {

  if (!content) return null;

  // determine if some margin at the bottom should be added
  const length = content.length;
  const shouldAddMargin = content[length - 1].blockType === "text"

  return (
    <>
      <div>
        {content.map(item => {
          if (item.blockType === "text" && item.content_html) {
            return (
              <Container key={item.id}>
                <HTMLText width={item.width} html={item.content_html} />
              </Container>
            );
          }

          if (item.blockType === "document" && typeof item.file === "object") {
            return (
              <Container key={item.id}>
                <Section padding={"medium"}>
                  <Button size={"lg"} href={item.file.url || "notfound"} schema={"contrast"}>{item.button}</Button>
                </Section>
              </Container>
            )
          }

          if (item.blockType === "donation") {
            return <Section key={item.id} padding={"small"} paddingBottom={"large"}>
              <DonationForm />
            </Section>
          }

          if (item.blockType === "youtube") {
            return <Section key={item.id} padding={"small"}>
              <Container>
                <YoutubePlayer id={item.youtube_id} />
              </Container>
            </Section>
          }

          if (item.blockType === "donationAppeal") {
            return <Section key={item.id} padding={"small"}>
              <Container>
                <DonationAppeal />
              </Container>
            </Section>
          }
        })}
      </div>

      { shouldAddMargin &&
        <Section></Section>
      }
    </>
  )
}