/** @license MIT | @cube-dev/ui-kit v0.123.0 | Cube Dev Team */
import { RendererPrism, ensureYamlSqlExtensions } from "./prismSetup.js";
import { CONTAINER_STYLES, tasty } from "@tenphi/tasty";
import { forwardRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Highlight } from "prism-react-renderer";
import "prismjs/components/prism-javascript.js";
import "prismjs/components/prism-yaml.js";
import "prismjs/components/prism-bash.js";
import "prismjs/components/prism-sql.js";

//#region src/components/content/PrismCode/PrismCode.tsx
const PreElement = tasty({
	as: "pre",
	qa: "CodeBlock",
	styleProps: CONTAINER_STYLES.concat([]),
	className: "cube-prism-code",
	styles: {
		margin: 0,
		padding: 0,
		overflow: "auto",
		scrollbar: "styled",
		Code: { display: "block" }
	}
});
function isDiffCode(code) {
	const lines = code.split("\n");
	const additionPattern = /^\+/;
	const deletionPattern = /^-+/;
	const headerPattern = /^(diff --git|---|\+\+\+)/;
	for (const line of lines) if (additionPattern.test(line) || deletionPattern.test(line) || headerPattern.test(line)) return true;
	return false;
}
function PrismCode(props, ref) {
	let { code = "", language = "javascript", ...otherProps } = props;
	if (!code) code = "";
	if (typeof code !== "string" && code) throw new Error("UIKit: code prop should be a string in PrismCode. Found: " + typeof code);
	const isDiff = isDiffCode(code || "");
	const grammarLang = isDiff ? `diff-${language}` : language;
	if (isDiff && !RendererPrism.languages[grammarLang]) RendererPrism.languages[grammarLang] = RendererPrism.languages.diff;
	if (language === "yaml" || grammarLang === "diff-yaml") ensureYamlSqlExtensions();
	return /* @__PURE__ */ jsx(PreElement, {
		ref,
		...otherProps,
		children: /* @__PURE__ */ jsx(Highlight, {
			prism: RendererPrism,
			code,
			language: grammarLang,
			children: ({ className, style, tokens, getLineProps, getTokenProps }) => {
				return /* @__PURE__ */ jsx("code", {
					"data-element": "Code",
					className: `${className}${isDiff ? " diff-highlight" : ""}`,
					style: {
						...style,
						color: void 0,
						backgroundColor: void 0
					},
					children: tokens.map((line, i) => {
						const { key: _lineKey, ...lineProps } = getLineProps({
							line,
							key: i
						});
						return /* @__PURE__ */ jsxs("span", {
							...lineProps,
							style: {
								...lineProps.style,
								color: void 0
							},
							children: [line.map((token, key) => {
								const { key: _tokenKey, ...tokenProps } = getTokenProps({
									token,
									key
								});
								return /* @__PURE__ */ jsx("span", {
									...tokenProps,
									style: {
										...tokenProps.style,
										color: void 0,
										backgroundColor: void 0
									}
								}, key);
							}), "\n"]
						}, i);
					})
				});
			}
		})
	});
}
/**
* Code block with syntax highlighting
*/
const _PrismCode = forwardRef(PrismCode);
_PrismCode.displayName = "PrismCode";

//#endregion
export { _PrismCode };
//# sourceMappingURL=PrismCode.js.map