"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__, {
    AutopilotChatInputActions: ()=>AutopilotChatInputActions
});
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 index_cjs_namespaceObject = require("../../../ap-typography/index.cjs");
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 error_provider_cjs_namespaceObject = require("../../providers/error-provider.cjs");
const picker_provider_cjs_namespaceObject = require("../../providers/picker-provider.cjs");
const external_service_index_cjs_namespaceObject = require("../../service/index.cjs");
const file_reader_cjs_namespaceObject = require("../../utils/file-reader.cjs");
const action_button_cjs_namespaceObject = require("../common/action-button.cjs");
const shared_controls_cjs_namespaceObject = require("../common/shared-controls.cjs");
const external_always_on_voice_button_cjs_namespaceObject = require("./always-on-voice-button.cjs");
const external_chat_input_agent_mode_selector_cjs_namespaceObject = require("./chat-input-agent-mode-selector.cjs");
const external_chat_input_model_picker_cjs_namespaceObject = require("./chat-input-model-picker.cjs");
const external_chat_input_resource_trigger_cjs_namespaceObject = require("./chat-input-resource-trigger.cjs");
const InputActionsContainer = (0, material_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        justifyContent: 'space-between',
        padding: `0 ${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingXs}`
    }));
const InputActionsGroup = (0, material_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        gap: apollo_core_default().Spacing.SpacingMicro
    }));
const SubmitButtonContainer = (0, material_namespaceObject.styled)('div')(()=>({
        '& .MuiIconButton-root': {
            borderRadius: apollo_core_default().Border.BorderRadiusM,
            backgroundColor: 'var(--color-background-disabled)',
            '&:not(.Mui-disabled)': {
                backgroundColor: "var(--color-foreground) !important",
                '&:hover, &:active, &:focus': {
                    backgroundColor: "var(--color-foreground-de-emp) !important"
                }
            }
        }
    }));
function AutopilotChatInputActionsComponent({ handleSubmit, disableSubmit, isInputEmpty, waitingResponse, onResourceTriggerClick, onVoiceInteractionChange, isVoiceInteractionActive }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { addAttachments } = (0, attachements_provider_cjs_namespaceObject.useAttachments)();
    const { setError } = (0, error_provider_cjs_namespaceObject.useError)();
    const { disabledFeatures, allowedAttachments } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const { models, agentModes } = (0, picker_provider_cjs_namespaceObject.usePicker)();
    const [isSpeechToTextActive, setIsSpeechToTextActive] = external_react_default().useState(()=>chatService?.isSpeechToTextActive ?? false);
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        setIsSpeechToTextActive(chatService.isSpeechToTextActive);
        return chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.SetSpeechToTextState, (isActive)=>{
            setIsSpeechToTextActive(isActive);
        });
    }, [
        chatService
    ]);
    const handleSpeechToTextClick = external_react_default().useCallback(()=>{
        chatService?.publishSpeechToTextToggle();
    }, [
        chatService
    ]);
    const fileInputRef = external_react_default().useRef(null);
    const handleFileButtonClick = ()=>{
        if (fileInputRef.current) {
            fileInputRef.current.value = '';
            fileInputRef.current.click();
        }
    };
    const handleAttachment = external_react_default().useCallback(async (event)=>{
        try {
            const parsedFiles = await (0, file_reader_cjs_namespaceObject.parseFiles)(Array.from(event.target.files ?? []));
            const oversizedFiles = parsedFiles.filter((file)=>file.size > allowedAttachments.maxSize);
            if (oversizedFiles.length > 0) {
                const errorMessages = oversizedFiles.map((file)=>{
                    const fileSizeMB = Math.round(file.size / 1024 / 1024);
                    const fileName = file.name;
                    const fileSize = fileSizeMB;
                    const maxSize = allowedAttachments.maxSize / 1024 / 1024;
                    return _({
                        id: 'autopilot-chat.input.actions.error.file-too-large',
                        values: {
                            fileName: fileName,
                            fileSize: fileSize,
                            maxSize: maxSize
                        }
                    });
                });
                setError(errorMessages.join('\n'));
            } else setError(void 0);
            addAttachments(parsedFiles.filter((file)=>file.size <= allowedAttachments.maxSize));
        } catch (err) {
            setError(err);
        }
    }, [
        addAttachments,
        setError,
        allowedAttachments.maxSize,
        _
    ]);
    const acceptedExtensions = external_react_default().useMemo(()=>Object.values(allowedAttachments.types).flat().join(','), [
        allowedAttachments
    ]);
    const attachmentDescription = external_react_default().useMemo(()=>{
        const parts = [];
        const maxCount = allowedAttachments.maxCount;
        const maxSize = allowedAttachments.maxSize / 1024 / 1024;
        const fileTypes = acceptedExtensions.split(',').join(', ');
        if (allowedAttachments.maxCount && allowedAttachments.maxCount > 1 && allowedAttachments.multiple) parts.push(_({
            id: 'autopilot-chat.input.actions.attachments.max-count',
            values: {
                maxCount: maxCount
            }
        }));
        parts.push(_({
            id: 'autopilot-chat.input.actions.attachments.max-size',
            values: {
                maxSize: maxSize
            }
        }), _({
            id: 'autopilot-chat.input.actions.attachments.allowed-types',
            values: {
                fileTypes: fileTypes
            }
        }));
        return parts.join('. ');
    }, [
        allowedAttachments,
        acceptedExtensions,
        _
    ]);
    const showVoice = false === disabledFeatures.audioStreaming && (isVoiceInteractionActive || !!isInputEmpty && !waitingResponse);
    const hasMultipleFeatures = [
        !disabledFeatures.attachments,
        models.length > 0,
        (agentModes?.length ?? 0) > 0
    ].filter(Boolean).length > 1;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(InputActionsContainer, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(InputActionsGroup, {
                style: isVoiceInteractionActive ? {
                    visibility: 'hidden'
                } : void 0,
                children: [
                    !disabledFeatures.attachments && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("input", {
                                type: "file",
                                ref: fileInputRef,
                                style: {
                                    display: 'none'
                                },
                                accept: acceptedExtensions,
                                multiple: allowedAttachments.multiple,
                                onChange: handleAttachment
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(shared_controls_cjs_namespaceObject.VisuallyHidden, {
                                id: "autopilot-chat-attach-file-description",
                                children: attachmentDescription
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                                iconName: "attach_file",
                                onClick: handleFileButtonClick,
                                tooltipPlacement: "top",
                                "data-testid": "autopilot-chat-attach-file-button",
                                ariaLabel: _({
                                    id: 'autopilot-chat.input.actions.attach-file'
                                }),
                                ariaDescribedby: "autopilot-chat-attach-file-description",
                                tooltip: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                                    children: [
                                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                            color: 'var(--color-foreground-inverse)',
                                            variant: apollo_core_namespaceObject.FontVariantToken.fontSizeM,
                                            children: _({
                                                id: 'autopilot-chat.input.actions.attach-file'
                                            })
                                        }),
                                        allowedAttachments.maxCount && allowedAttachments.maxCount > 1 && allowedAttachments.multiple && (()=>{
                                            const maxCount = allowedAttachments.maxCount;
                                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                                color: 'var(--color-foreground-inverse)',
                                                variant: apollo_core_namespaceObject.FontVariantToken.fontSizeXs,
                                                children: _({
                                                    id: 'autopilot-chat.input.actions.attachments.max-count',
                                                    values: {
                                                        maxCount: maxCount
                                                    }
                                                })
                                            });
                                        })(),
                                        (()=>{
                                            const maxSize = allowedAttachments.maxSize / 1024 / 1024;
                                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                                color: 'var(--color-foreground-inverse)',
                                                variant: apollo_core_namespaceObject.FontVariantToken.fontSizeXs,
                                                children: _({
                                                    id: 'autopilot-chat.input.actions.attachments.max-size',
                                                    values: {
                                                        maxSize: maxSize
                                                    }
                                                })
                                            });
                                        })(),
                                        (()=>{
                                            const fileTypes = acceptedExtensions.split(',').join(', ');
                                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                                color: 'var(--color-foreground-inverse)',
                                                variant: apollo_core_namespaceObject.FontVariantToken.fontSizeXs,
                                                children: _({
                                                    id: 'autopilot-chat.input.actions.attachments.allowed-types',
                                                    values: {
                                                        fileTypes: fileTypes
                                                    }
                                                })
                                            });
                                        })()
                                    ]
                                })
                            })
                        ]
                    }),
                    onResourceTriggerClick && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_resource_trigger_cjs_namespaceObject.ResourceTriggerButton, {
                        onClick: onResourceTriggerClick
                    }),
                    models.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_model_picker_cjs_namespaceObject.AutopilotChatInputModelPicker, {
                        useIcon: hasMultipleFeatures
                    }),
                    agentModes.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_input_agent_mode_selector_cjs_namespaceObject.AutopilotChatAgentModeSelector, {
                        useIcon: false
                    })
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(InputActionsGroup, {
                children: [
                    !isVoiceInteractionActive && !disabledFeatures.audio && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_always_on_voice_button_cjs_namespaceObject.VoiceButtonContainer, {
                        active: isSpeechToTextActive,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                            iconName: "mic",
                            onClick: handleSpeechToTextClick,
                            tooltipPlacement: "top",
                            tooltip: _({
                                id: 'autopilot-chat.input.actions.dictate'
                            }),
                            overrideColor: isSpeechToTextActive ? 'var(--color-background)' : void 0,
                            "data-testid": "autopilot-chat-stt-button",
                            ariaLabel: _({
                                id: 'autopilot-chat.input.actions.dictate'
                            }),
                            ariaPressed: isSpeechToTextActive
                        })
                    }),
                    showVoice && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_always_on_voice_button_cjs_namespaceObject.AlwaysOnVoiceButton, {
                        disabled: isSpeechToTextActive,
                        onActiveChange: onVoiceInteractionChange
                    }),
                    !showVoice && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SubmitButtonContainer, {
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                            iconName: waitingResponse ? 'stop' : 'arrow_upward',
                            tooltip: waitingResponse ? _({
                                id: 'autopilot-chat.input.actions.stop'
                            }) : _({
                                id: 'autopilot-chat.input.actions.send'
                            }),
                            overrideColor: disableSubmit ? 'var(--color-foreground-disable)' : 'var(--color-background)',
                            variant: waitingResponse ? 'normal' : 'outlined',
                            preventHover: true,
                            disabled: disableSubmit,
                            onClick: handleSubmit,
                            "data-testid": "autopilot-chat-submit-button",
                            ariaLabel: waitingResponse ? _({
                                id: 'autopilot-chat.input.actions.stop'
                            }) : _({
                                id: 'autopilot-chat.input.actions.send'
                            })
                        })
                    })
                ]
            })
        ]
    });
}
const AutopilotChatInputActions = /*#__PURE__*/ external_react_default().memo(AutopilotChatInputActionsComponent);
exports.AutopilotChatInputActions = __webpack_exports__.AutopilotChatInputActions;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatInputActions"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
