import React from "react";
import type { MDXComponents } from "mdx/types";
import H1 from "../../layout/H1";
import H2 from "../../layout/H2";
import H3 from "../../layout/H3";
import H4 from "../../layout/H4";
import CodeBlock from "../../elements/CodeBlock";
import { twMerge } from "tailwind-merge";

type Params = {
    components: MDXComponents;
    codeBgColor?: string;
};

export default function useMDXComponents({
    components,
    codeBgColor,
}: Params): MDXComponents {
    return {
        h1: ({ children }) => <H1>{children}</H1>,
        h4: ({ children }) => <H4>{children}</H4>,
        pre: ({ children, ...props }) => {
            if (React.isValidElement(children) && children.props) {
                return (
                    // @ts-ignore
                    <CodeBlock {...props} backgroundColor={codeBgColor}>
                        {/* @ts-ignore */}
                        {children.props.children}
                    </CodeBlock>
                );
            }
            return (
                // @ts-ignore
                <CodeBlock {...props} backgroundColor={codeBgColor}>
                    {children}
                </CodeBlock>
            );
        },
        ...components,
    };
}
