import { forwardRef, type ButtonHTMLAttributes, type ReactNode } from "react";
import { Check, Copy } from "lucide-react";
import { CODE_LANGUAGES, resolveLanguage } from "@/shared/editor/schema/code-block-model";
import { bidAttribute, type BlockIdentityProps } from "./attrs";
export interface CodeBlockFrameProps extends BlockIdentityProps {
language: unknown;
controls?: ReactNode;
children?: ReactNode;
}
export interface CodeBlockCopyButtonChromeProps extends ButtonHTMLAttributes {
copied?: boolean;
}
export function getCodeLanguageLabel(language: unknown): string {
const normalizedLanguage = resolveLanguage(typeof language === "string" ? language : null);
return CODE_LANGUAGES[normalizedLanguage]?.name ?? CODE_LANGUAGES.text.name;
}
export function getCodeCopyButtonLabel(copied: boolean): string {
return copied ? "Code copied" : "Copy code";
}
export const CodeBlockCopyButtonChrome = forwardRef(
function CodeBlockCopyButtonChrome(
{ copied = false, className, type = "button", title, "aria-label": ariaLabel, ...props },
ref,
) {
const label = typeof ariaLabel === "string" ? ariaLabel : getCodeCopyButtonLabel(copied);
return (
);
},
);
export function CodeBlockFrame({ bid, language, controls, children }: CodeBlockFrameProps) {
return (
);
}
export function CodeBlockContent({ children }: { children?: ReactNode }) {
return (
{children}
);
}
export function CodeBlockPresentation(props: CodeBlockFrameProps) {
return (
{props.children}
);
}
function asOptionalString(value: unknown): string | null {
return typeof value === "string" ? value : null;
}