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;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect, useRef } from 'react';
import path from 'path-browserify';
import * as monaco from 'monaco-editor';
import { loader } from '@monaco-editor/react';
// Import PatternFly components
import { CodeEditor } from '@patternfly/react-code-editor';
import { Button, getResizeObserver, ModalBody, ModalFooter, ModalHeader, Stack, StackItem } from '@patternfly/react-core';
import FileDetails, { extensionToLanguage } from '../FileDetails';
import { ChatbotDisplayMode } from '../Chatbot';
import ChatbotModal from '../ChatbotModal/ChatbotModal';
// Configure Monaco loader to use the npm package instead of CDN
loader.config({ monaco });
export const CodeModal = (_a) => {
    var { fileName, code, codeEditorControlClassName: codeEditorClassName, handleModalToggle, isCopyEnabled, isLineNumbersVisible, isModalOpen, isReadOnly, onPrimaryAction, onSecondaryAction, primaryActionBtn, secondaryActionBtn, title, displayMode = ChatbotDisplayMode.default, isCompact, modalHeaderClassName, modalBodyClassName, modalFooterClassName } = _a, props = __rest(_a, ["fileName", "code", "codeEditorControlClassName", "handleModalToggle", "isCopyEnabled", "isLineNumbersVisible", "isModalOpen", "isReadOnly", "onPrimaryAction", "onSecondaryAction", "primaryActionBtn", "secondaryActionBtn", "title", "displayMode", "isCompact", "modalHeaderClassName", "modalBodyClassName", "modalFooterClassName"]);
    const [newCode, setNewCode] = useState(code);
    const [editorInstance, setEditorInstance] = useState(null);
    const [isEditorReady, setIsEditorReady] = useState(false);
    const containerRef = useRef(null);
    useEffect(() => {
        if (!isModalOpen || !isEditorReady || !editorInstance || !containerRef.current) {
            return;
        }
        const handleResize = () => {
            if (editorInstance && isEditorReady && isModalOpen) {
                try {
                    window.requestAnimationFrame(() => {
                        editorInstance.layout();
                    });
                }
                catch (error) {
                    // eslint-disable-next-line no-console
                    console.error('ChatBot code modal layout error:', error);
                }
            }
        };
        const observer = getResizeObserver(containerRef.current, handleResize);
        return () => {
            observer();
        };
    }, [editorInstance, isEditorReady, isModalOpen]);
    const handlePrimaryAction = (_event) => {
        handleModalToggle(_event);
        if (!isReadOnly) {
            onPrimaryAction(_event, newCode);
        }
        else {
            onPrimaryAction(_event);
        }
    };
    const handleSecondaryAction = (_event) => {
        handleModalToggle(_event);
        onSecondaryAction(_event);
    };
    const onEditorDidMount = (editor, monaco) => {
        setEditorInstance(editor);
        monaco.editor.getModels()[0].updateOptions({ tabSize: 5 });
        if (containerRef.current) {
            setIsEditorReady(true);
            editor.layout();
            editor.focus();
        }
    };
    const onCodeChange = (value) => {
        if (!isReadOnly) {
            setNewCode(value);
        }
    };
    const modal = (_jsxs(ChatbotModal, { isOpen: isModalOpen, onClose: handleModalToggle, ouiaId: "CodeModal", "aria-labelledby": "code-modal-title", "aria-describedby": "code-modal", className: `pf-chatbot__code-modal ${isCompact ? 'pf-m-compact' : ''} pf-chatbot__code-modal--${displayMode}`, displayMode: displayMode, isCompact: isCompact, children: [_jsx(ModalHeader, { className: modalHeaderClassName, title: title, labelId: "code-modal-title" }), _jsx(ModalBody, { className: modalBodyClassName, id: "code-modal-body", children: _jsxs(Stack, { className: "pf-chatbot__code-modal-body", children: [_jsx(StackItem, { className: "pf-chatbot__code-modal-file-details", children: _jsx(FileDetails, { fileName: fileName }) }), _jsx("div", { className: "pf-v6-l-stack__item pf-chatbot__code-modal-editor", ref: containerRef, children: _jsx(CodeEditor, Object.assign({ isDarkTheme: true, isLineNumbersVisible: isLineNumbersVisible, isLanguageLabelVisible: true, isCopyEnabled: isCopyEnabled, isReadOnly: isReadOnly, code: newCode, language: extensionToLanguage[path.extname(fileName).slice(1)], onEditorDidMount: onEditorDidMount, onCodeChange: onCodeChange, className: codeEditorClassName, isFullHeight: true, options: {
                                    glyphMargin: false,
                                    folding: false,
                                    // prevents Monaco from handling resizing itself
                                    // was causing ResizeObserver issues
                                    automaticLayout: false
                                } }, props)) })] }) }), _jsxs(ModalFooter, { className: modalFooterClassName, children: [_jsx(Button, { isBlock: true, variant: "primary", onClick: handlePrimaryAction, form: "code-modal-form", children: primaryActionBtn }, "code-modal-primary"), _jsx(Button, { isBlock: true, variant: "link", onClick: handleSecondaryAction, children: secondaryActionBtn }, "code-modal-secondary")] })] }));
    return modal;
};
export default CodeModal;
