"use client" import React, { useMemo, useState } from "react" import clsx from "clsx" import { HighlightProps, Highlight, themes } from "prism-react-renderer" import { CopyButton, Tooltip, LegacyLink } from "@/components" import { useColorMode } from "@/providers" import { ExclamationCircle, PlaySolid, SquareTwoStack } from "@medusajs/icons" import { CodeBlockHeader, CodeBlockHeaderMeta } from "./Header" import { CodeBlockLine } from "./Line" import { ApiAuthType, ApiDataOptions, ApiMethod } from "types" import { CSSTransition } from "react-transition-group" import { ApiRunner } from "./ApiRunner" import { GITHUB_ISSUES_PREFIX } from "../.." export type Highlight = { line: number text?: string tooltipText?: string } export type CodeBlockMetaFields = { title?: string npm2yarn?: boolean highlights?: string[][] apiTesting?: boolean testApiMethod?: ApiMethod testApiUrl?: string testAuthType?: ApiAuthType testPathParams?: ApiDataOptions testQueryParams?: ApiDataOptions testBodyParams?: ApiDataOptions noCopy?: boolean noReport?: boolean noLineNumbers?: boolean } & CodeBlockHeaderMeta export type CodeBlockStyle = "loud" | "subtle" export type CodeBlockProps = { source: string lang?: string className?: string collapsed?: boolean blockStyle?: CodeBlockStyle children?: React.ReactNode } & CodeBlockMetaFields & Omit export const CodeBlock = ({ source, lang = "", className, collapsed = false, title = "", highlights = [], apiTesting = false, blockStyle = "loud", noCopy = false, noReport = false, noLineNumbers = false, children, ...rest }: CodeBlockProps) => { if (!source && typeof children === "string") { source = children } const { colorMode } = useColorMode() const [showTesting, setShowTesting] = useState(false) const canShowApiTesting = useMemo( () => apiTesting && rest.testApiMethod && rest.testApiUrl, [apiTesting, rest] ) const bgColor = useMemo( () => clsx( blockStyle === "loud" && [ colorMode === "light" && "bg-medusa-code-bg-base", colorMode === "dark" && "bg-medusa-bg-component", ], blockStyle === "subtle" && [ colorMode === "light" && "bg-medusa-bg-subtle", colorMode === "dark" && "bg-medusa-code-bg-base", ] ), [blockStyle, colorMode] ) const lineNumbersColor = useMemo( () => clsx( blockStyle === "loud" && [ colorMode === "light" && "text-medusa-code-text-subtle", colorMode === "dark" && "text-medusa-fg-muted", ], blockStyle === "subtle" && [ colorMode === "light" && "text-medusa-fg-muted", colorMode === "dark" && "text-medusa-code-text-subtle", ] ), [blockStyle, colorMode] ) const borderColor = useMemo( () => clsx( blockStyle === "loud" && [ colorMode === "light" && "border-medusa-code-border", colorMode === "dark" && "border-medusa-border-base", ], blockStyle === "subtle" && [ colorMode === "light" && "border-medusa-border-base", colorMode === "dark" && "border-medusa-code-border", ] ), [blockStyle, colorMode] ) const iconColor = useMemo( () => clsx( blockStyle === "loud" && [ colorMode === "light" && "text-medusa-code-icon", colorMode === "dark" && "text-medusa-fg-muted", ], blockStyle === "subtle" && [ colorMode === "light" && "text-medusa-fg-muted", colorMode === "dark" && "text-medusa-code-icon", ] ), [blockStyle, colorMode] ) if (!source.length) { return <> } const transformedHighlights: Highlight[] = highlights .filter((highlight) => highlight.length !== 0) .map((highlight) => ({ line: parseInt(highlight[0]), text: highlight.length >= 2 ? highlight[1] : undefined, tooltipText: highlight.length >= 3 ? highlight[2] : undefined, })) return ( <> {title && ( )}
{({ className: preClassName, style, tokens, ...rest }) => ( <>
                 1 && "pt-docs_1 pr-docs_1",
                    tokens.length <= 1 && "!py-docs_0.25 px-[6px]"
                  )}
                >
                  {tokens.map((line, i) => {
                    const highlightedLines = transformedHighlights.filter(
                      (highlight) => highlight.line - 1 === i
                    )

                    return (
                       1}
                        key={i}
                        bgColorClassName={bgColor}
                        lineNumberColorClassName={lineNumbersColor}
                        {...rest}
                      />
                    )
                  })}
                
              
)}
{canShowApiTesting && ( )} ) }