import Link from "@/components/TempDesignSystem/Link"
import { removeMultipleSlashes } from "@/utils/url"

import styles from "./uspgrid.module.css"

import { EmbedEnum } from "@/types/requests/utils/embeds"
import type { EmbedByUid } from "@/types/transitionTypes/jsontohtml"
import { RTEItemTypeEnum, RTETypeEnum } from "@/types/transitionTypes/rte/enums"
import type {
  RTEDefaultNode,
  RTENext,
  RTENode,
  RTERegularNode,
} from "@/types/transitionTypes/rte/node"
import type { RenderOptions } from "@/types/transitionTypes/rte/option"

export const renderOptions: RenderOptions = {
  [RTETypeEnum.p]: (
    node: RTEDefaultNode,
    embeds: EmbedByUid,
    next: RTENext,
    fullRenderOptions: RenderOptions
  ) => {
    return (
      <p key={node.uid} className={styles.p}>
        {next(node.children, embeds, fullRenderOptions)}
      </p>
    )
  },
  [RTETypeEnum.a]: (
    node: RTERegularNode,
    embeds: EmbedByUid,
    next: RTENext,
    fullRenderOptions: RenderOptions
  ) => {
    if (node.attrs.url) {
      return (
        <a
          href={node.attrs.url}
          target={node.attrs.target ?? "_blank"}
          key={node.uid}
          className={styles.a}
        >
          {next(node.children, embeds, fullRenderOptions)}
        </a>
      )
    }
    return null
  },
  [RTETypeEnum.reference]: (
    node: RTENode,
    embeds: EmbedByUid,
    next: RTENext,
    fullRenderOptions: RenderOptions
  ) => {
    if ("attrs" in node) {
      const type = node.attrs.type
      if (type !== RTEItemTypeEnum.asset) {
        const href = node.attrs?.locale
          ? `/${node.attrs.locale}${node.attrs.href}`
          : node.attrs.href

        return (
          <Link href={href} key={node.uid} className={styles.a}>
            {next(node.children, embeds, fullRenderOptions)}
          </Link>
        )
      }

      return null
    }
  },
}
