'use strict';

var chunk2SMCH62O_cjs = require('./chunk-2SMCH62O.cjs');
var chunkWGEGR3DF_cjs = require('./chunk-WGEGR3DF.cjs');
var prismReactRenderer = require('prism-react-renderer');
var react = require('react');
var i18n = require('@djangocfg/i18n');
var hooks = require('@djangocfg/ui-core/hooks');
var jsxRuntime = require('react/jsx-runtime');

var globalScope = typeof globalThis !== "undefined" ? globalThis : {};
if (!globalScope.Prism) {
  globalScope.Prism = prismReactRenderer.Prism;
}
var ready = false;
var loading = null;
var listeners = /* @__PURE__ */ new Set();
function subscribe(cb) {
  listeners.add(cb);
  return () => listeners.delete(cb);
}
chunkWGEGR3DF_cjs.__name(subscribe, "subscribe");
function getSnapshot() {
  return ready;
}
chunkWGEGR3DF_cjs.__name(getSnapshot, "getSnapshot");
function getServerSnapshot() {
  return true;
}
chunkWGEGR3DF_cjs.__name(getServerSnapshot, "getServerSnapshot");
async function ensurePrismLanguages() {
  if (ready) return;
  if (loading) return loading;
  loading = (async () => {
    await import('prismjs/components/prism-markup-templating');
    await Promise.all([
      import('prismjs/components/prism-bash'),
      import('prismjs/components/prism-java'),
      import('prismjs/components/prism-ruby'),
      import('prismjs/components/prism-php')
    ]);
    ready = true;
    listeners.forEach((cb) => cb());
  })();
  return loading;
}
chunkWGEGR3DF_cjs.__name(ensurePrismLanguages, "ensurePrismLanguages");
function useEnsurePrismLanguages() {
  const loaded = react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
  if (!loaded) {
    void ensurePrismLanguages();
  }
  return loaded;
}
chunkWGEGR3DF_cjs.__name(useEnsurePrismLanguages, "useEnsurePrismLanguages");
void ensurePrismLanguages();
var PrettyCode = /* @__PURE__ */ chunkWGEGR3DF_cjs.__name(({ data, language, className, mode, inline = false, customBg, isCompact = false, scrollIsolation, maxLines, variant = "card" }) => {
  const containerRef = react.useRef(null);
  const t = i18n.useAppT();
  const detectedTheme = hooks.useResolvedTheme();
  useEnsurePrismLanguages();
  const labels = react.useMemo(() => ({
    copyCode: t("tools.code.copyCode"),
    noContent: t("tools.code.noContent")
  }), [t]);
  const fontSize = isCompact ? "0.75rem" : "0.875rem";
  const currentTheme = mode || detectedTheme;
  const isDarkMode = currentTheme === "dark";
  const prismTheme = isDarkMode ? prismReactRenderer.themes.vsDark : prismReactRenderer.themes.vsLight;
  const contentJson = typeof data === "string" ? data : JSON.stringify(data || {}, null, 2);
  const lineCount = contentJson ? contentJson.split("\n").length : 0;
  const lineHeightRatio = isCompact ? 1.4 : 1.5;
  const fontSizePx = isCompact ? 12 : 14;
  const verticalPadPx = 16 + 12;
  const shouldScroll = maxLines !== void 0 && lineCount > maxLines;
  const maxHeightPx = maxLines !== void 0 ? maxLines * fontSizePx * lineHeightRatio + verticalPadPx : void 0;
  if (!contentJson || contentJson.trim() === "") {
    const emptyBgClass = customBg || (isDarkMode ? "bg-zinc-900" : "bg-white");
    const emptyBorderClass = isDarkMode ? "border-zinc-700" : "border-border";
    return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `relative h-full ${emptyBgClass} rounded-sm border ${emptyBorderClass} ${className || ""}`, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "h-full overflow-auto p-4", children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground text-sm italic", children: labels.noContent }) }) });
  }
  const getLanguageDisplayName = /* @__PURE__ */ chunkWGEGR3DF_cjs.__name((lang) => {
    switch (lang.toLowerCase()) {
      case "bash":
      case "shell":
        return "Bash";
      case "python":
      case "py":
        return "Python";
      case "javascript":
      case "js":
        return "JavaScript";
      case "typescript":
      case "ts":
        return "TypeScript";
      case "json":
        return "JSON";
      case "yaml":
      case "yml":
        return "YAML";
      case "html":
        return "HTML";
      case "css":
        return "CSS";
      case "sql":
        return "SQL";
      case "xml":
        return "XML";
      case "markdown":
      case "md":
        return "Markdown";
      case "plaintext":
      case "text":
        return "Text";
      case "mermaid":
        return "Mermaid";
      case "ruby":
      case "rb":
        return "Ruby";
      case "java":
        return "Java";
      case "php":
        return "PHP";
      case "go":
      case "golang":
        return "Go";
      default:
        return lang.charAt(0).toUpperCase() + lang.slice(1);
    }
  }, "getLanguageDisplayName");
  const normalizedLanguage = (() => {
    const lang = language.toLowerCase();
    switch (lang) {
      case "javascript":
      case "js":
        return "javascript";
      case "typescript":
      case "ts":
        return "typescript";
      // Try TypeScript first
      case "python":
      case "py":
        return "python";
      case "json":
        return "json";
      case "css":
        return "css";
      case "html":
        return "markup";
      case "xml":
        return "markup";
      case "bash":
      case "shell":
      case "sh":
        return "bash";
      case "ruby":
      case "rb":
        return "ruby";
      case "java":
        return "java";
      case "php":
        return "php";
      case "go":
      case "golang":
        return "go";
      case "sql":
        return "sql";
      case "yaml":
      case "yml":
        return "yaml";
      case "markdown":
      case "md":
        return "markdown";
      case "mermaid":
        return "text";
      // Mermaid is handled separately in MarkdownMessage
      default:
        return lang || "text";
    }
  })();
  const displayLanguage = getLanguageDisplayName(language);
  if (variant === "plain") {
    return /* @__PURE__ */ jsxRuntime.jsx(prismReactRenderer.Highlight, { theme: prismTheme, code: contentJson, language: normalizedLanguage, children: ({ className: prismClassName, style, tokens, getLineProps, getTokenProps }) => {
      const { backgroundColor: _bg, ...restStyle } = style;
      return /* @__PURE__ */ jsxRuntime.jsx(
        "pre",
        {
          className: `${prismClassName} ${className || ""}`,
          style: {
            ...restStyle,
            background: "transparent",
            margin: 0,
            padding: isCompact ? "0.75rem 1rem" : "1rem",
            fontSize,
            lineHeight: lineHeightRatio,
            fontFamily: "monospace",
            // ``break-all`` (not ``break-word``) so long unbroken
            // strings without whitespace — typical of escaped
            // JSON bodies in generated code samples — wrap at any
            // character rather than overflowing the container.
            whiteSpace: "pre-wrap",
            wordBreak: "break-all",
            overflowWrap: "anywhere",
            // Hard cap on width — parents (grid cells, panels)
            // should constrain us but some callers render inside
            // ``flex: 1 1 auto`` which lets the pre grow past
            // intended bounds. ``max-width: 100%`` pins to parent.
            maxWidth: "100%"
          },
          children: tokens.map((line, i) => /* @__PURE__ */ jsxRuntime.jsx("div", { ...getLineProps({ line }), children: line.map((token, key) => /* @__PURE__ */ jsxRuntime.jsx("span", { ...getTokenProps({ token }) }, key)) }, i))
        }
      );
    } });
  }
  if (inline) {
    const inlineBgClass = customBg || (isDarkMode ? "bg-zinc-800" : "bg-zinc-100");
    return /* @__PURE__ */ jsxRuntime.jsx(prismReactRenderer.Highlight, { theme: prismTheme, code: contentJson, language: normalizedLanguage, children: ({ className: className2, style, tokens, getTokenProps }) => /* @__PURE__ */ jsxRuntime.jsx(
      "code",
      {
        className: `${className2} ${inlineBgClass} px-2 py-1 rounded ${isCompact ? "text-xs" : "text-sm"} font-mono inline-block`,
        style: {
          ...style,
          fontSize,
          fontFamily: "monospace"
        },
        children: tokens.map((line) => line.map((token, key) => /* @__PURE__ */ jsxRuntime.jsx("span", { ...getTokenProps({ token }) }, key)))
      }
    ) });
  }
  const bgClass = customBg || (isDarkMode ? "bg-zinc-900" : "bg-white");
  const borderClass = isDarkMode ? "border-zinc-700" : "border-border";
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "div",
    {
      ref: containerRef,
      className: `group relative ${bgClass} rounded-lg border ${borderClass} ${className || ""}`,
      style: (
        // maxHeight caps growth at ``maxLines`` rows; without maxLines we
        // let the block grow to fit its content (no scroll).
        maxHeightPx ? { maxHeight: `${maxHeightPx}px` } : void 0
      ),
      children: [
        /* @__PURE__ */ jsxRuntime.jsx("div", { className: "absolute inset-x-0 top-0 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-150 z-30", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "pointer-events-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
          chunk2SMCH62O_cjs.FloatingToolbar,
          {
            containerRef,
            scrollIsolation: shouldScroll ? scrollIsolation : false,
            label: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-muted/80 text-muted-foreground border border-border/50 backdrop-blur-sm", children: displayLanguage }),
            children: /* @__PURE__ */ jsxRuntime.jsx(chunk2SMCH62O_cjs.CopyAction, { value: contentJson, title: labels.copyCode })
          }
        ) }) }),
        /* @__PURE__ */ jsxRuntime.jsx("div", { className: shouldScroll ? "h-full overflow-auto" : "", style: shouldScroll ? { maxHeight: maxHeightPx } : void 0, children: /* @__PURE__ */ jsxRuntime.jsx(prismReactRenderer.Highlight, { theme: prismTheme, code: contentJson, language: normalizedLanguage, children: ({ className: className2, style, tokens, getLineProps, getTokenProps }) => {
          const { backgroundColor: _bg, ...restStyle } = style;
          return /* @__PURE__ */ jsxRuntime.jsx(
            "pre",
            {
              className: `${className2} rounded-lg`,
              style: {
                ...restStyle,
                margin: 0,
                // Top padding gives the hover toolbar room to sit over
                // the first line without covering it. Before: 2.5rem —
                // too much empty space on short snippets. Now: 1rem,
                // toolbar overlays with translucent bg on hover only.
                padding: "1rem 1rem 0.75rem 1rem",
                fontSize,
                lineHeight: lineHeightRatio,
                fontFamily: "monospace",
                whiteSpace: "pre-wrap",
                wordBreak: "break-word",
                overflowWrap: "break-word"
              },
              children: tokens.map((line, i) => /* @__PURE__ */ jsxRuntime.jsx("div", { ...getLineProps({ line }), children: line.map((token, key) => /* @__PURE__ */ jsxRuntime.jsx("span", { ...getTokenProps({ token }) }, key)) }, i))
            }
          );
        } }) })
      ]
    }
  );
}, "PrettyCode");
var PrettyCode_client_default = PrettyCode;

module.exports = PrettyCode_client_default;
//# sourceMappingURL=PrettyCode.client-RPDIE5CH.cjs.map
//# sourceMappingURL=PrettyCode.client-RPDIE5CH.cjs.map