import type { MDXComponents } from 'mdx/types'

export function useMDXComponents(components: MDXComponents): MDXComponents {
  return {
    ...components,
    // Typography
    h1: (props) => (
      <h1 {...props} className="scroll-m-20 heading-xl text-ink mt-10 mb-4">
        {props.children}
      </h1>
    ),
    h2: (props) => (
      <h2 {...props} className="scroll-m-20 border-b border-border pb-2 heading-lg text-ink mt-12 mb-4 first:mt-0">
        {props.children}
      </h2>
    ),
    h3: (props) => (
      <h3 {...props} className="scroll-m-20 heading-md text-ink mt-8 mb-3">
        {props.children}
      </h3>
    ),
    p: ({ children }) => (
      <p className="leading-7 [&:not(:first-child)]:mt-6 text-ink">
        {children}
      </p>
    ),
    blockquote: ({ children }) => (
      <blockquote className="border-l-4 border-border pl-4 italic text-ink-soft my-6">
        {children}
      </blockquote>
    ),
    hr: () => <hr className="my-8 border-border" />,
    a: ({ href, children, ...props }) => {
      const isExternal = typeof href === 'string' && (href.startsWith('http://') || href.startsWith('https://'))
      return (
        <a
          href={href}
          target={isExternal ? '_blank' : undefined}
          rel={isExternal ? 'noopener noreferrer' : undefined}
          className="font-medium underline underline-offset-4 hover:text-accent transition-colors"
          {...props}
        >
          {children}
        </a>
      )
    },
    code: ({ children, ...props }) => (
      <code
        {...props}
        className="rounded bg-surface px-[0.3rem] py-[0.2rem] font-mono text-sm text-ink"
      >
        {children}
      </code>
    ),
    pre: ({ children, ...props }) => (
      <pre {...props} className="overflow-x-auto rounded-xl bg-surface text-sm my-6 p-4">
        {children}
      </pre>
    ),
    img: ({ src, alt, ...rest }) => (
      <img src={src} alt={alt} className="my-8 rounded-xl w-full" {...rest} />
    ),
    table: ({ children }) => (
      <div className="my-6 overflow-x-auto">
        <table className="w-full border-collapse text-sm">
          {children}
        </table>
      </div>
    ),

    // Lists
    ul: (props) => <ul className="my-6 ml-6 list-disc [&>li]:mt-2" {...props} />,
    ol: (props) => <ol className="my-6 ml-6 list-decimal [&>li]:mt-2" {...props} />,
    li: (props) => <li className="leading-7" {...props} />,

    // Figures and captions
    figure: (props) => <figure className="my-8" {...props} />,
    figcaption: (props) => <figcaption className="text-center text-sm text-ink-soft mt-2" {...props} />,

    // Subscripts and superscripts
    sup: (props) => <sup className="text-xs" {...props} />,
    sub: (props) => <sub className="text-xs" {...props} />,

    // Keyboard keys
    kbd: (props) => (
      <kbd className="rounded bg-surface px-2 py-1 font-mono text-xs text-ink border border-border" {...props} />
    ),

    // Collapsible sections
    details: (props) => <details className="my-4 rounded-lg border border-border p-4" {...props} />,
    summary: (props) => <summary className="cursor-pointer font-semibold" {...props} />,
  }
}
