"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
// ============================================================================
// Chatbot Main - Message - Content - Code Block
// ============================================================================
const react_1 = require("react");
// 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 DEFAULT_EXPANDED_TEXT = 'Show less';
const DEFAULT_COLLAPSED_TEXT = 'Show more';
const CodeBlockMessage = (_a) => {
    var _b;
    var { children, className, 'aria-label': ariaLabel, isExpandable = false, expandableSectionProps, expandableSectionToggleProps, expandedText = DEFAULT_EXPANDED_TEXT, collapsedText = DEFAULT_COLLAPSED_TEXT, customActions, isPrimary } = _a, props = __rest(_a, ["children", "className", 'aria-label', "isExpandable", "expandableSectionProps", "expandableSectionToggleProps", "expandedText", "collapsedText", "customActions", "isPrimary"]);
    const [copied, setCopied] = (0, react_1.useState)(false);
    const [isExpanded, setIsExpanded] = (0, react_1.useState)(false);
    const buttonRef = (0, react_1.useRef)();
    const tooltipID = (0, react_core_1.getUniqueId)();
    const toggleId = (0, react_core_1.getUniqueId)();
    const contentId = (0, react_core_1.getUniqueId)();
    const codeBlockRef = (0, react_1.useRef)(null);
    const language = (_b = /language-(\w+)/.exec(className || '')) === null || _b === void 0 ? void 0 : _b[1];
    // Get custom toggle text from data attributes if available - for use with rehype plugins
    const customExpandedText = props['data-expanded-text'];
    const customCollapsedText = props['data-collapsed-text'];
    const finalExpandedText = customExpandedText || expandedText;
    const finalCollapsedText = customCollapsedText || collapsedText;
    if ((customExpandedText && expandedText !== DEFAULT_EXPANDED_TEXT) ||
        (customCollapsedText && collapsedText !== DEFAULT_COLLAPSED_TEXT)) {
        // eslint-disable-next-line no-console
        console.error('Message:', 'Custom rehype plugins that rely on data-expanded-text or data-collapsed-text will override expandedText and collapsedText props if both are passed in.');
    }
    const onToggle = (isExpanded) => {
        setIsExpanded(isExpanded);
    };
    // Handle clicking copy button
    const handleCopy = (0, react_1.useCallback)((event, text) => {
        navigator.clipboard.writeText(text.toString());
        setCopied(true);
    }, []);
    // Reset copied state
    (0, react_1.useEffect)(() => {
        if (copied) {
            const timer = setTimeout(() => {
                setCopied(false);
            }, 3000);
            return () => clearTimeout(timer);
        }
    });
    if (!String(children).includes('\n')) {
        return ((0, jsx_runtime_1.jsx)("code", Object.assign({}, props, { className: `pf-chatbot__message-inline-code ${isPrimary ? 'pf-m-primary' : ''}`, children: children })));
    }
    // Setup code block header
    const actions = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(react_core_1.CodeBlockAction, { className: "pf-chatbot__message-code-block-default-action", children: [language && (0, jsx_runtime_1.jsx)("div", { className: "pf-chatbot__message-code-block-language", children: language }), (0, jsx_runtime_1.jsx)(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), children: copied ? (0, jsx_runtime_1.jsx)(check_icon_1.CheckIcon, {}) : (0, jsx_runtime_1.jsx)(copy_icon_1.CopyIcon, {}) }), (0, jsx_runtime_1.jsx)(react_core_1.Tooltip, { id: tooltipID, content: "Copy", position: "top", triggerRef: buttonRef })] }), customActions] }));
    return ((0, jsx_runtime_1.jsx)("div", { className: "pf-chatbot__message-code-block", ref: codeBlockRef, children: (0, jsx_runtime_1.jsxs)(react_core_1.CodeBlock, { actions: actions, children: [(0, jsx_runtime_1.jsx)(react_core_1.CodeBlockCode, { children: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: isExpandable ? ((0, jsx_runtime_1.jsx)(react_core_1.ExpandableSection, Object.assign({ variant: react_core_1.ExpandableSectionVariant.truncate, isExpanded: isExpanded, isDetached: true, toggleId: toggleId, contentId: contentId }, expandableSectionProps, { children: children }))) : (children) }) }), isExpandable && ((0, jsx_runtime_1.jsx)(react_core_1.ExpandableSectionToggle, Object.assign({ isExpanded: isExpanded, onToggle: onToggle, direction: "up", toggleId: toggleId, contentId: contentId, hasTruncatedContent: true, className: "pf-chatbot__message-code-toggle" }, expandableSectionToggleProps, { children: isExpanded ? finalExpandedText : finalCollapsedText })))] }) }));
};
exports.default = CodeBlockMessage;
