"use client"

import type { ComponentProps, ReactNode } from "react"

function getTextContent(children: ReactNode): string {
  if (typeof children === "string" || typeof children === "number") {
    return String(children)
  }

  if (Array.isArray(children)) {
    return children.map((child) => getTextContent(child)).join("")
  }

  return ""
}

function slugify(children: ReactNode) {
  return getTextContent(children)
    .toLowerCase()
    .replace(/\s+/g, "-")
    .replace(/[^a-z0-9-]/g, "")
}

export const MDXComponents = {
  h2: ({ children, ...props }: ComponentProps<"h2">) => (
    <h2
      id={slugify(children)}
      className="mb-4 mt-10 border-b border-[var(--color-border)] pb-2 font-serif text-[1.6rem] font-normal text-[var(--color-text-primary)] scroll-mt-24"
      {...props}
    >
      {children}
    </h2>
  ),
  h3: ({ children, ...props }: ComponentProps<"h3">) => (
    <h3
      id={slugify(children)}
      className="mb-3 mt-8 font-serif text-xl font-normal text-[var(--color-text-primary)] scroll-mt-24"
      {...props}
    >
      {children}
    </h3>
  ),
  p: ({ children, ...props }: ComponentProps<"p">) => (
    <p
      className="mb-5 text-base leading-[1.8] text-[var(--color-text-secondary)]"
      {...props}
    >
      {children}
    </p>
  ),
  a: ({ children, ...props }: ComponentProps<"a">) => (
    <a
      className="text-[var(--color-accent)] hover:underline"
      {...props}
    >
      {children}
    </a>
  ),
  strong: ({ children, ...props }: ComponentProps<"strong">) => (
    <strong
      className="font-semibold text-[var(--color-text-primary)]"
      {...props}
    >
      {children}
    </strong>
  ),
  blockquote: ({ children, ...props }: ComponentProps<"blockquote">) => (
    <blockquote
      className="my-4 rounded-r border-l-[3px] border-[var(--color-accent)] bg-[var(--color-surface)] py-2 pl-4 italic text-[var(--color-text-secondary)]"
      {...props}
    >
      {children}
    </blockquote>
  ),
  ul: ({ children, ...props }: ComponentProps<"ul">) => (
    <ul
      className="mb-4 list-disc pl-6 text-[15px] leading-[1.8] text-[var(--color-text-secondary)]"
      {...props}
    >
      {children}
    </ul>
  ),
  ol: ({ children, ...props }: ComponentProps<"ol">) => (
    <ol
      className="mb-4 list-decimal pl-6 text-[15px] leading-[1.8] text-[var(--color-text-secondary)]"
      {...props}
    >
      {children}
    </ol>
  ),
  li: ({ children, ...props }: ComponentProps<"li">) => (
    <li className="mb-[0.4rem]" {...props}>
      {children}
    </li>
  ),
  code: ({ children, ...props }: ComponentProps<"code">) => (
    <code
      className="rounded bg-[var(--color-surface)] px-1.5 py-0.5 font-mono text-[13px] text-[var(--color-accent)]"
      {...props}
    >
      {children}
    </code>
  ),
  pre: ({ children, ...props }: ComponentProps<"pre">) => (
    <pre
      className="mb-4 overflow-x-auto rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)] p-4"
      {...props}
    >
      {children}
    </pre>
  ),
  hr: (props: ComponentProps<"hr">) => (
    <hr className="my-8 border-[var(--color-border)]" {...props} />
  ),
}
