"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AutopilotChatInput: ()=>AutopilotChatInput
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const material_namespaceObject = require("@mui/material");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const attachements_provider_cjs_namespaceObject = require("../../providers/attachements-provider.cjs");
const chat_service_provider_cjs_namespaceObject = require("../../providers/chat-service.provider.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const loading_provider_cjs_namespaceObject = require("../../providers/loading-provider.cjs");
const resource_data_provider_cjs_namespaceObject = require("../../providers/resource-data-provider.cjs");
const streaming_provider_cjs_namespaceObject = require("../../providers/streaming-provider.cjs");
const index_cjs_namespaceObject = require("../../service/index.cjs");
const file_reader_cjs_namespaceObject = require("../../utils/file-reader.cjs");
const font_by_variant_cjs_namespaceObject = require("../../utils/font-by-variant.cjs");
const external_chat_input_actions_cjs_namespaceObject = require("./chat-input-actions.cjs");
const external_chat_input_attachments_cjs_namespaceObject = require("./chat-input-attachments.cjs");
const external_chat_input_editor_cjs_namespaceObject = require("./chat-input-editor.cjs");
const external_chat_input_error_cjs_namespaceObject = require("./chat-input-error.cjs");
const external_chat_input_footer_cjs_namespaceObject = require("./chat-input-footer.cjs");
const InputContainer = (0, material_namespaceObject.styled)('div')(({ primaryFontToken })=>({
        border: `${apollo_core_default().Border.BorderThickM} solid transparent`,
        boxShadow: `inset 0 0 0 ${apollo_core_default().Border.BorderThickS} var(--color-border)`,
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        gap: apollo_core_default().Spacing.SpacingBase,
        marginBottom: apollo_core_default().Spacing.SpacingXs,
        '&:focus-within': {
            borderColor: 'var(--color-focus-indicator)',
            boxShadow: 'none'
        },
        '& .MuiTextField-root': {
            width: '100%',
            height: apollo_core_default().Spacing.SpacingM,
            verticalAlign: 'middle'
        },
        '& .autopilot-chat-input': {
            position: 'relative'
        },
        '& .autopilot-chat-input .tiptap-editor-container': {
            padding: `0 ${apollo_core_default().Spacing.SpacingBase}`,
            color: 'var(--color-foreground)',
            '&:focus': {
                boxShadow: 'none'
            },
            ...primaryFontToken && (()=>{
                const fontToken = (0, font_by_variant_cjs_namespaceObject.fontByVariant)(primaryFontToken);
                return fontToken ? {
                    fontSize: fontToken.fontSize,
                    fontFamily: fontToken.fontFamily,
                    lineHeight: fontToken.lineHeight,
                    fontWeight: fontToken.fontWeight
                } : {};
            })()
        }
    }));
const GradientContainer = (0, material_namespaceObject.styled)('div')(()=>({
        position: 'absolute',
        zIndex: 1,
        bottom: '2px',
        left: apollo_core_default().Spacing.SpacingBase,
        width: `calc(100% - 2 * ${apollo_core_default().Spacing.SpacingBase})`,
        height: apollo_core_default().Spacing.SpacingXs,
        background: `linear-gradient(
        to bottom,
        var(--color-background)50 0%,
        var(--color-background)75 25%,
        var(--color-background) 50%
    )`
    }));
function AutopilotChatInputComponent() {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const initialPrompt = chatService?.getPrompt?.();
    const { disabledFeatures, overrideLabels, spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const { hasResources } = (0, resource_data_provider_cjs_namespaceObject.useResourceData)();
    const [message, setMessage] = external_react_default().useState('string' == typeof initialPrompt ? initialPrompt : initialPrompt?.content ?? '');
    const editorRef = external_react_default().useRef(null);
    const inputContainerRef = external_react_default().useRef(null);
    const { waitingResponse, skeletonLoader } = (0, loading_provider_cjs_namespaceObject.useLoading)();
    const { streaming } = (0, streaming_provider_cjs_namespaceObject.useStreaming)();
    const { attachments, clearAttachments, addAttachments, attachmentsLoading } = (0, attachements_provider_cjs_namespaceObject.useAttachments)();
    const editorLineHeight = external_react_default().useMemo(()=>{
        const fontToken = (0, font_by_variant_cjs_namespaceObject.fontByVariant)(spacing.primaryFontToken);
        return fontToken?.lineHeight;
    }, [
        spacing.primaryFontToken
    ]);
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        const unsubscribeSetInputFocused = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetInputFocused, (value)=>{
            if (value) editorRef.current?.focus();
        });
        const unsubscribeSetPrompt = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.SetPrompt, (prompt)=>{
            setMessage('string' == typeof prompt ? prompt : prompt.content);
        });
        return ()=>{
            unsubscribeSetInputFocused();
            unsubscribeSetPrompt();
        };
    }, [
        chatService
    ]);
    const handleChange = external_react_default().useCallback((value)=>{
        if (0 === value.trim().length) return void chatService.setPrompt('');
        chatService.setPrompt(value);
    }, [
        chatService
    ]);
    const handleSubmit = external_react_default().useCallback(()=>{
        if (waitingResponse || streaming) return void chatService.stopResponse();
        if (chatService.isSpeechToTextActive) chatService.publishSpeechToTextToggle();
        const serializedContent = editorRef.current?.getSerializedContent() ?? message;
        chatService.sendRequest({
            content: serializedContent,
            attachments
        });
        editorRef.current?.clear();
        setMessage('');
        clearAttachments();
    }, [
        message,
        attachments,
        clearAttachments,
        chatService,
        waitingResponse,
        streaming
    ]);
    const handleKeyDown = external_react_default().useCallback((event)=>{
        if ('Enter' !== event.key || event.shiftKey) return false;
        if (waitingResponse || streaming) {
            event.preventDefault();
            return true;
        }
        const editorContent = editorRef.current?.getSerializedContent() ?? message;
        if ((editorContent.trim().length > 0 || attachments.length > 0) && !skeletonLoader) {
            handleSubmit();
            return true;
        }
        return false;
    }, [
        message,
        handleSubmit,
        waitingResponse,
        streaming,
        skeletonLoader,
        attachments
    ]);
    const handlePaste = external_react_default().useCallback(async (event)=>{
        if (disabledFeatures?.attachments) return;
        const items = event.clipboardData?.items;
        if (!items) return;
        const allowedAttachments = Object.keys(chatService?.getConfig()?.allowedAttachments?.types ?? {});
        const attachmentsToAdd = [];
        let foundAllowedAttachment = false;
        for(let i = 0; i < items.length; i++)if (allowedAttachments.includes(items[i].type)) {
            const blob = items[i].getAsFile();
            if (blob) {
                attachmentsToAdd.push(blob);
                foundAllowedAttachment = true;
            }
        }
        if (foundAllowedAttachment) event.preventDefault();
        const parsedFiles = await (0, file_reader_cjs_namespaceObject.parseFiles)(attachmentsToAdd);
        addAttachments(parsedFiles);
    }, [
        chatService,
        addAttachments,
        disabledFeatures?.attachments
    ]);
    external_react_default().useEffect(()=>{
        const container = inputContainerRef.current;
        if (!container) return;
        container.addEventListener('paste', handlePaste);
        return ()=>{
            container.removeEventListener('paste', handlePaste);
        };
    }, [
        handlePaste
    ]);
    const hasLoadingAttachments = attachmentsLoading.filter((attachment)=>attachment.loading).length > 0;
    const isInputEmpty = 0 === message.trim().length && 0 === attachments.length;
    const handleResourceTriggerClick = external_react_default().useCallback(()=>{
        editorRef.current?.openResourcePicker();
    }, []);
    const [isVoiceInteractionActive, setIsVoiceInteractionActive] = external_react_default().useState(false);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            !isVoiceInteractionActive && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_error_cjs_namespaceObject.AutopilotChatInputError, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(InputContainer, {
                primaryFontToken: spacing.primaryFontToken,
                onClick: isVoiceInteractionActive ? void 0 : ()=>editorRef?.current?.focus(),
                ref: inputContainerRef,
                sx: isVoiceInteractionActive ? {
                    borderColor: 'transparent',
                    boxShadow: 'none',
                    background: 'transparent'
                } : void 0,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_attachments_cjs_namespaceObject.AutopilotChatInputAttachments, {}),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(material_namespaceObject.Box, {
                        className: "autopilot-chat-input",
                        sx: {
                            padding: `${apollo_core_default().Spacing.SpacingS} 0 0 !important`,
                            ...isVoiceInteractionActive && {
                                visibility: 'hidden'
                            }
                        },
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                className: "tiptap-editor-container",
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_editor_cjs_namespaceObject.ChatInputEditor, {
                                    ref: editorRef,
                                    value: message,
                                    placeholder: overrideLabels?.inputPlaceholder ?? _({
                                        id: 'autopilot-chat.input.placeholder'
                                    }),
                                    onChange: handleChange,
                                    onKeyDown: handleKeyDown,
                                    minRows: spacing.promptBox.minRows,
                                    maxRows: spacing.promptBox.maxRows,
                                    lineHeight: editorLineHeight
                                })
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(GradientContainer, {})
                        ]
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_actions_cjs_namespaceObject.AutopilotChatInputActions, {
                        disableSubmit: isInputEmpty && !waitingResponse && !streaming || skeletonLoader && !waitingResponse && !streaming || hasLoadingAttachments,
                        isInputEmpty: isInputEmpty,
                        waitingResponse: waitingResponse || streaming,
                        handleSubmit: handleSubmit,
                        onResourceTriggerClick: hasResources ? handleResourceTriggerClick : void 0,
                        onVoiceInteractionChange: setIsVoiceInteractionActive,
                        isVoiceInteractionActive: isVoiceInteractionActive
                    })
                ]
            }),
            !disabledFeatures?.footer && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_footer_cjs_namespaceObject.AutopilotChatInputFooter, {})
        ]
    });
}
const AutopilotChatInput = /*#__PURE__*/ external_react_default().memo(AutopilotChatInputComponent);
exports.AutopilotChatInput = __webpack_exports__.AutopilotChatInput;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatInput"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
