var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
// ============================================================================
// Chatbot Main - Message - Content - Code Block
// ============================================================================
import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { obsidian } from 'react-syntax-highlighter/dist/esm/styles/hljs';
// Import PatternFly components
import { CodeBlock, CodeBlockAction, CodeBlockCode, Button, Tooltip } from '@patternfly/react-core';
import { CheckIcon } from '@patternfly/react-icons/dist/esm/icons/check-icon';
import { CopyIcon } from '@patternfly/react-icons/dist/esm/icons/copy-icon';
const CodeBlockMessage = (_a) => {
    var _b;
    var { children, className, 'aria-label': ariaLabel } = _a, props = __rest(_a, ["children", "className", 'aria-label']);
    const [copied, setCopied] = React.useState(false);
    const buttonRef = React.useRef();
    const tooltipID = React.useId();
    const language = (_b = /language-(\w+)/.exec(className || '')) === null || _b === void 0 ? void 0 : _b[1];
    // Handle clicking copy button
    const handleCopy = React.useCallback((event, text) => {
        navigator.clipboard.writeText(text.toString());
        setCopied(true);
    }, []);
    // Reset copied state
    React.useEffect(() => {
        if (copied) {
            const timer = setTimeout(() => {
                setCopied(false);
            }, 3000);
            return () => clearTimeout(timer);
        }
    });
    if (!String(children).includes('\n')) {
        return (React.createElement("code", Object.assign({}, props, { className: "pf-chatbot__message-inline-code" }), children));
    }
    // Setup code block header
    const actions = (React.createElement(React.Fragment, null,
        React.createElement(CodeBlockAction, null,
            language && React.createElement("div", { className: "pf-chatbot__message-code-block-language" }, language),
            React.createElement(Button, { ref: buttonRef, "aria-label": ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : 'Copy code button', variant: "plain", className: "pf-chatbot__button--copy", onClick: (event) => handleCopy(event, children) }, copied ? React.createElement(CheckIcon, null) : React.createElement(CopyIcon, null)),
            React.createElement(Tooltip, { id: tooltipID, content: "Copy", position: "top", triggerRef: buttonRef }))));
    return (React.createElement("div", { className: "pf-chatbot__message-code-block" },
        React.createElement(CodeBlock, { actions: actions },
            React.createElement(CodeBlockCode, null, language ? (React.createElement(SyntaxHighlighter, Object.assign({}, props, { language: language, style: obsidian, PreTag: "div", CodeTag: "div", wrapLongLines: true }), String(children).replace(/\n$/, ''))) : (React.createElement(React.Fragment, null, children))))));
};
export default CodeBlockMessage;
