import { Fragment } from "react";
import PageTitle from "@/blocks/PageTitle";
import PageTitleAlt from "@/blocks/PageTitleAlt";
import PageTitleWithImage from "@/blocks/PageTitleWithImage";
import TwoColumnWithTitle from "@/blocks/TwoColumnWithTitle";
import TwoColumn from "@/blocks/TwoColumn";
import LeftImageWithTitle from "@/blocks/LeftImageWithTitle";
import TwoColumnWithImage from "@/blocks/TwoColumnWithImage";
import RelatedProjects from "@/blocks/RelatedProjects";
import RelatedInsights from "@/blocks/RelatedInsights";
import RelatedServices from "@/blocks/RelatedServices";
import ThreeInfoBlocks from "@/blocks/ThreeInfoBlocks";
import ImageWithCaption from "@/blocks/ImageWithCaption";
import MasonryImageGrid from "@/blocks/MasonryImageGrid";
import GreenWithLargeTitleAndText from "@/blocks/GreenWithLargeTitleAndText";
import GreenWithLargeTitle from "@/blocks/GreenWithLargeTitle";
import ContainedContent from "@/blocks/ContainedContent";
import TeamQuestion from "@/blocks/TeamQuestion";
import ClientQuote from "@/blocks/ClientQuote";
import ContactBlock from "@/blocks/ContactBlock";
import CodeBlock from "@/blocks/CodeBlock";
import FaqsBlock from "@/blocks/FaqsBlock";
import CookieTrigger from "@/blocks/CookieTrigger";
import EventBlock from "@/blocks/EventBlock";
import ThreeOptionsWithTitle from "@/blocks/ThreeOptionsWithTitle";
import TitleWithMap from "@/blocks/TitleWithMap";
import LlmsCheckerBlock from "@/blocks/LlmsCheckerBlock";
import CarbonCalculatorBlock from "@/blocks/CarbonCalculatorBlock";
import ReadMore from "@/blocks/ReadMore";
import ServiceList from "@/blocks/ServiceList";
import ServiceListAlternate from "@/blocks/ServiceListAlternate";
import ServiceFaqBlock from "@/blocks/ServiceFaqBlock";
import UspCard from "@/blocks/UspCard";
import Quote from "@/blocks/Quote";
import LogoMarquee from "@/blocks/LogoMarquee";
import TitleCtaBlock from "@/blocks/TitleCtaBlock";
import CtaBlock from "@/blocks/CtaBlock";
import BulletServiceList from "@/blocks/BulletServiceList";
import CaseStudyBlock from "@/blocks/CaseStudyBlock";
import ContentsJumpMenu from "@/blocks/ContentsJumpMenu";

type BlockProps = {
  blockName: string;
  attrs: {
    data: object;
  };
};

type BlockResolverProps = {
  blocks: Array<BlockProps>;
};

type ComposedBlockProps = {
  [key: string]: any;
};

type BlockMapProps = {
  [key: string]: Function;
};

const blockMap: BlockMapProps = {
  "teapot/contained-content": (props: ComposedBlockProps) => (
    <ContainedContent {...props} />
  ),
  "teapot/page-title": (props: ComposedBlockProps) => <PageTitle {...props} />,
  "teapot/page-title-alt": (props: ComposedBlockProps) => <PageTitleAlt {...props} />,
  "teapot/page-title-with-image": (props: ComposedBlockProps) => (
    <PageTitleWithImage {...props} />
  ),
  "teapot/two-column-with-title": (props: ComposedBlockProps) => (
    <TwoColumnWithTitle {...props} />
  ),
  "teapot/two-column": (props: ComposedBlockProps) => <TwoColumn {...props} />,
  "teapot/left-image-with-title": (props: ComposedBlockProps) => (
    <LeftImageWithTitle {...props} />
  ),
  "teapot/two-column-with-image": (props: ComposedBlockProps) => (
    <TwoColumnWithImage {...props} />
  ),
  "teapot/green-with-large-title-and-text": (props: ComposedBlockProps) => (
    <GreenWithLargeTitleAndText {...props} />
  ),
  "teapot/green-with-large-title": (props: ComposedBlockProps) => (
    <GreenWithLargeTitle {...props} />
  ),
  "teapot/related-projects": (props: ComposedBlockProps) => (
    <RelatedProjects {...props} />
  ),
  "teapot/related-insights": (props: ComposedBlockProps) => (
    <RelatedInsights {...props} />
  ),
  "teapot/related-services": (props: ComposedBlockProps) => (
    <RelatedServices {...props} />
  ),
  "teapot/three-info-blocks": (props: ComposedBlockProps) => (
    <ThreeInfoBlocks {...props} />
  ),
  "teapot/info-block-with-text": (props: ComposedBlockProps) => <></>,
  "teapot/team-question": (props: ComposedBlockProps) => (
    <TeamQuestion {...props} />
  ),
  "teapot/client-quote": (props: ComposedBlockProps) => (
    <ClientQuote {...props} />
  ),
  "teapot/three-options-with-title": (props: ComposedBlockProps) => (
    <ThreeOptionsWithTitle {...props} />
  ),
  "teapot/image-with-caption": (props: ComposedBlockProps) => (
    <ImageWithCaption {...props} />
  ),
  "teapot/masonry-image-grid": (props: ComposedBlockProps) => (
    <MasonryImageGrid {...props} />
  ),
  "teapot/contact-block": (props: ComposedBlockProps) => (
    <ContactBlock {...props} />
  ),
  "teapot/code-block": (props: ComposedBlockProps) => <CodeBlock {...props} />,
  "teapot/faqs": (props: ComposedBlockProps) => <FaqsBlock {...props} />,
  "teapot/cookie-trigger": (props: ComposedBlockProps) => (
    <CookieTrigger {...props} />
  ),
  "teapot/events": (props: ComposedBlockProps) => <EventBlock {...props} />,
  "teapot/title-with-map": (props: ComposedBlockProps) => (
    <TitleWithMap {...props} />
  ),
  "teapot/llms-checker": (props: ComposedBlockProps) => (
    <LlmsCheckerBlock {...props} />
  ),
  "teapot/carbon-calculator": (props: ComposedBlockProps) => (
    <CarbonCalculatorBlock {...props} />
  ),
  "teapot/read-more": (props: ComposedBlockProps) => <ReadMore {...props} />,
  "teapot/service-list": (props: ComposedBlockProps) => (
    <ServiceList {...props} />
  ),
  "teapot/service-list-alternate": (props: ComposedBlockProps) => (
    <ServiceListAlternate {...props} />
  ),
  "teapot/service-faq": (props: ComposedBlockProps) => (
    <ServiceFaqBlock {...props} />
  ),
  "teapot/usp-card": (props: ComposedBlockProps) => <UspCard {...props} />,
  "teapot/quote": (props: ComposedBlockProps) => <Quote {...props} />,
  "teapot/logo-marquee": (props: ComposedBlockProps) => (
    <LogoMarquee {...props} />
  ),
  "teapot/title-cta": (props: ComposedBlockProps) => (
    <TitleCtaBlock {...props} />
  ),
  "teapot/cta": (props: ComposedBlockProps) => <CtaBlock {...props} />,
  "teapot/bullet-service-list": (props: ComposedBlockProps) => (
    <BulletServiceList {...props} />
  ),
  "teapot/case-study": (props: ComposedBlockProps) => (
    <CaseStudyBlock {...props} />
  ),
  "teapot/contents-jump": (props: ComposedBlockProps) => (
    <ContentsJumpMenu {...props} />
  ),
};

const BlockResolver = (props: BlockResolverProps) => {
  const { blocks = [] } = props;

  const buildComponent = (index: number, blockName: string, attrs: any) => {
    if (!blockName) return null;

    const blockFunc = blockMap[blockName];
    if (typeof blockFunc !== "function") {
      return null;
    }

    let component;
    try {
      component = blockFunc({
        ...attrs.data,
        anchor: attrs.anchor,
      });
    } catch (err) {
      console.error(`Error rendering block "${blockName}"`, err);
      return null;
    }

    if (
      !component ||
      (typeof component !== "object" && typeof component !== "function")
    ) {
      console.error(`Invalid component returned for block "${blockName}"`, component);
      return null;
    }

    return <Fragment key={index}>{component}</Fragment>;
  };

  return (
    <>
      {blocks.map((block, index) =>
        buildComponent(index, block.blockName, block.attrs),
      )}
    </>
  )
};

export default BlockResolver;
