import { componentMap } from "../export/componentMap";
import { getExportHref } from "../helper/function";
export interface SectionData {
  [key: string]: any;
}

export interface Section {
  id?: string;
  type: string;        // hero, features, about
  variant: string;     // split, centered, grid
  data: SectionData;
  animation?: string;
}
interface SectionRendererProps {
  section: Section;
  exportMode?: boolean; // for HTML export if needed
  preferences?: any;
}

export default function SectionRenderer({
  section,
  exportMode = false,
  preferences,
}: SectionRendererProps) {

  const { type, variant, data } = section;

    const pageSlugs = (preferences?.activepages || []).map((p: any) =>
      p.toLowerCase()
    );
    if (type === "cta") {
      if (data?.primaryCta?.href) {
        data.primaryCta.href = getExportHref(data?.primaryCta?.href, pageSlugs, exportMode);
      }
      if (data?.secondaryCta?.href) {
        data.secondaryCta.href = getExportHref(data?.secondaryCta?.href, pageSlugs, exportMode);
      }
    }
    if (type === "header") {
      if (data?.links) {
        data.links = data?.links?.map((link: any) => {
          return {
            ...link,
            href: getExportHref(link.href, pageSlugs, exportMode)
          };
        });
      }
      if (data?.ctaHref) {
        data.ctaHref = getExportHref(data?.ctaHref, pageSlugs, exportMode);
      }
    }
    if (type === "footer") {
      if (data?.links) {
        data.links = data?.links?.map((group: any) => ({
          ...group,
          items: group?.items?.map((item: any) => {
            return {
              ...item,
              href: getExportHref(item?.href, pageSlugs, exportMode)
            };
          })
        }));
      }
    }
    if (type === "hero") {
      if (data?.actions?.primary?.href) {
        data.actions.primary.href = getExportHref(data?.actions?.primary?.href, pageSlugs, exportMode);
      }
      if (data?.actions?.secondary?.href) {
        data.actions.secondary.href = getExportHref(data?.actions?.secondary?.href, pageSlugs, exportMode);
      }
    }
  if (exportMode) {
    if (preferences?.logo) {
      const fullPath = preferences?.logo;
      const logorelativePath = fullPath.includes(`img/`)
        ? fullPath.substring(fullPath.indexOf("img/"))
        : "";
      preferences.logo = logorelativePath;
    }

    if (preferences?.favicon) {
      const fullPathfavicon = preferences?.favicon;
      const faviconrelativePath = fullPathfavicon.includes(`img/`)
        ? fullPathfavicon.substring(fullPathfavicon.indexOf("img/"))
        : "";
      preferences.favicon = faviconrelativePath;
    }
  }
  // Get component
  const Component = (componentMap as any)[type]?.[variant];
  // If component not found
  if (!Component) {
    return (
      <div className="py-10 text-center text-red-500">
        Missing section: {type} / {variant}
      </div>
    );
  }
  if (exportMode && type != "header") {
    return (
      <div key={section?.id} id={type ?? section?.id} >
        <Component {...data} preferences={preferences} exportMode={exportMode} />
      </div>
    );
  } else {
    return (
      <Component {...data} preferences={preferences} exportMode={exportMode} />
    );
  }

}