"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// ============================================================================
// Chatbot Main - Message - Content - Code Block
// ============================================================================
const react_1 = __importDefault(require("react"));
const react_syntax_highlighter_1 = __importDefault(require("react-syntax-highlighter"));
const hljs_1 = require("react-syntax-highlighter/dist/esm/styles/hljs");
// Import PatternFly components
const react_core_1 = require("@patternfly/react-core");
const check_icon_1 = require("@patternfly/react-icons/dist/esm/icons/check-icon");
const copy_icon_1 = require("@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_1.default.useState(false);
    const buttonRef = react_1.default.useRef();
    const tooltipID = react_1.default.useId();
    const language = (_b = /language-(\w+)/.exec(className || '')) === null || _b === void 0 ? void 0 : _b[1];
    // Handle clicking copy button
    const handleCopy = react_1.default.useCallback((event, text) => {
        navigator.clipboard.writeText(text.toString());
        setCopied(true);
    }, []);
    // Reset copied state
    react_1.default.useEffect(() => {
        if (copied) {
            const timer = setTimeout(() => {
                setCopied(false);
            }, 3000);
            return () => clearTimeout(timer);
        }
    });
    if (!String(children).includes('\n')) {
        return (react_1.default.createElement("code", Object.assign({}, props, { className: "pf-chatbot__message-inline-code" }), children));
    }
    // Setup code block header
    const actions = (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.CodeBlockAction, null,
            language && react_1.default.createElement("div", { className: "pf-chatbot__message-code-block-language" }, language),
            react_1.default.createElement(react_core_1.Button, { ref: buttonRef, "aria-label": ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : 'Copy code', variant: "plain", className: "pf-chatbot__button--copy", onClick: (event) => handleCopy(event, children) }, copied ? react_1.default.createElement(check_icon_1.CheckIcon, null) : react_1.default.createElement(copy_icon_1.CopyIcon, null)),
            react_1.default.createElement(react_core_1.Tooltip, { id: tooltipID, content: "Copy", position: "top", triggerRef: buttonRef }))));
    return (react_1.default.createElement("div", { className: "pf-chatbot__message-code-block" },
        react_1.default.createElement(react_core_1.CodeBlock, { actions: actions },
            react_1.default.createElement(react_core_1.CodeBlockCode, null, language ? (react_1.default.createElement(react_syntax_highlighter_1.default, Object.assign({}, props, { language: language, style: hljs_1.obsidian, PreTag: "div", CodeTag: "div", wrapLongLines: true }), String(children).replace(/\n$/, ''))) : (react_1.default.createElement(react_1.default.Fragment, null, children))))));
};
exports.default = CodeBlockMessage;
