function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React from 'react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneLight } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { AIChatStore } from "../../../../model/AIChat";
import { useCopyToClipboard } from 'react-use';
import { CheckOutlined, CopyOutlined, PlaySquareOutlined } from '@ant-design/icons';
import { Button, Space, Tooltip } from 'antd';
import styles from "./MarkdownCodeBlock.module.less";
import { useIntl } from 'dumi'; // 定义 props 类型，它将接收 react-markdown 传递的所有属性

// 定义 props 类型，它将接收 react-markdown 传递的所有属性

export var MarkdownCodeBlock = /*#__PURE__*/React.memo(function (_ref) {
  var inline = _ref.inline,
    className = _ref.className,
    children = _ref.children,
    _ref$showRunButton = _ref.showRunButton,
    showRunButtonProp = _ref$showRunButton === void 0 ? true : _ref$showRunButton;
  var intl = useIntl();
  var _useCopyToClipboard = useCopyToClipboard(),
    _useCopyToClipboard2 = _slicedToArray(_useCopyToClipboard, 2),
    copyState = _useCopyToClipboard2[0],
    copyToClipboard = _useCopyToClipboard2[1];
  // 1. 处理行内代码：如果是行内代码，不做特殊处理，直接返回一个 <code> 标签
  if (inline || typeof children === 'string' && !children.includes('\n')) {
    return /*#__PURE__*/React.createElement("code", {
      className: className
    }, children);
  }

  // 2. 提取语言：从 className 中提取代码语言，例如 "language-javascript" -> "javascript"
  var match = /language-(\w+)/.exec(className || '');
  var language = match ? match[1] : 'text'; // 如果没有指定语言，默认为纯文本

  // 3. 将 children 转换为字符串，并移除末尾的换行符
  var codeString = String(children).replace(/\n$/, '');

  // 4. 判断是否显示“运行”按钮
  var showRunButton = showRunButtonProp && /\bimport\b/.test(codeString);

  // 5. 定义运行代码的逻辑
  var handleRunCode = function handleRunCode() {
    // console.log("准备运行的代码:", codeString);
    AIChatStore.codeBlock = codeString;
  };

  // 6. 返回最终的 JSX 结构
  return /*#__PURE__*/React.createElement("div", {
    style: {
      position: 'relative',
      margin: '1em 0'
    }
  }, /*#__PURE__*/React.createElement(Space, {
    className: styles.button
  }, /*#__PURE__*/React.createElement(Tooltip, {
    title: intl.formatMessage({
      id: 'ai.markdown.copy'
    })
  }, /*#__PURE__*/React.createElement(Button, {
    variant: "link",
    size: "small",
    onClick: function onClick() {
      return copyToClipboard(codeString);
    },
    title: intl.formatMessage({
      id: 'ai.markdown.copy'
    })
  }, copyState.value === codeString ? /*#__PURE__*/React.createElement(CheckOutlined, null) : /*#__PURE__*/React.createElement(CopyOutlined, null))), showRunButton && /*#__PURE__*/React.createElement(Tooltip, {
    title: intl.formatMessage({
      id: 'ai.markdown.run'
    })
  }, /*#__PURE__*/React.createElement(Button, {
    variant: "link",
    size: "small",
    onClick: handleRunCode,
    title: intl.formatMessage({
      id: 'ai.markdown.run'
    })
  }, /*#__PURE__*/React.createElement(PlaySquareOutlined, null)))), /*#__PURE__*/React.createElement(SyntaxHighlighter, {
    style: oneLight,
    language: language === 'vue' ? 'html' : language,
    PreTag: "div" // 使用 div 作为外层标签，避免 pre 标签的默认样式冲突
    ,
    showLineNumbers: false // (可选) 显示行号
    ,
    customStyle: {
      paddingTop: '2em',
      background: '#fafafa'
    }
  }, codeString));
});