"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__, {
    AlwaysOnVoiceButton: ()=>AlwaysOnVoiceButton,
    VoiceButtonContainer: ()=>VoiceButtonContainer
});
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");
const chat_service_provider_cjs_namespaceObject = require("../../providers/chat-service.provider.cjs");
const index_cjs_namespaceObject = require("../../service/index.cjs");
const chat_audio_input_cjs_namespaceObject = require("../audio/chat-audio-input.cjs");
const chat_audio_output_cjs_namespaceObject = require("../audio/chat-audio-output.cjs");
const action_button_cjs_namespaceObject = require("../common/action-button.cjs");
const VoiceButtonContainer = (0, material_namespaceObject.styled)('div', {
    shouldForwardProp: (prop)=>'active' !== prop
})(({ active })=>({
        '& .MuiIconButton-root': {
            borderRadius: apollo_core_default().Border.BorderRadiusM,
            ...active && {
                backgroundColor: "var(--color-foreground) !important",
                '&:hover, &:active, &:focus': {
                    backgroundColor: "var(--color-foreground-de-emp) !important"
                }
            }
        }
    }));
const AlwaysOnVoiceButton = ({ disabled = false, onActiveChange })=>{
    const { _ } = (0, react_namespaceObject.useLingui)();
    const [isActive, setIsActive] = (0, external_react_namespaceObject.useState)(false);
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const isActiveRef = (0, external_react_namespaceObject.useRef)(false);
    const onActiveChangeRef = (0, external_react_namespaceObject.useRef)(onActiveChange);
    onActiveChangeRef.current = onActiveChange;
    const { queueOutputAudio, clearOutputAudioQueue } = (0, chat_audio_output_cjs_namespaceObject.useAudioOutput)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!chatService) return;
        return chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.OutputStream, (event)=>{
            if (!isActiveRef.current) return;
            if (event.mediaChunks) {
                for (const chunk of event.mediaChunks)if (chunk.mimeType.startsWith('audio/pcm;')) queueOutputAudio(chunk.mimeType, chunk.data, chunk.sequenceNumber);
            }
            if (event.interrupted) clearOutputAudioQueue();
        });
    }, [
        chatService,
        queueOutputAudio,
        clearOutputAudioQueue
    ]);
    const handleAudioInputStart = (0, external_react_namespaceObject.useCallback)((automaticActivityDetectionEnabled)=>{
        if (!chatService) return;
        isActiveRef.current = true;
        setIsActive(true);
        onActiveChangeRef.current?.(true);
        chatService.sendInputStreamEvent({
            activityStart: {
                automaticActivityDetectionEnabled
            }
        });
    }, [
        chatService
    ]);
    const handleAudioInputEnd = (0, external_react_namespaceObject.useCallback)((sequenceNumber)=>{
        if (!chatService) return;
        isActiveRef.current = false;
        setIsActive(false);
        onActiveChangeRef.current?.(false);
        clearOutputAudioQueue();
        chatService.sendInputStreamEvent({
            activityEnd: {
                sequenceNumber
            }
        });
    }, [
        chatService,
        clearOutputAudioQueue
    ]);
    const handleAudioInputData = (0, external_react_namespaceObject.useCallback)((mimeType, data, sequenceNumber)=>{
        if (!chatService) return;
        chatService.sendInputStreamEvent({
            mediaChunks: [
                {
                    mimeType,
                    data,
                    sequenceNumber
                }
            ]
        });
    }, [
        chatService
    ]);
    const { startAudioInput, stopAudioInput } = (0, chat_audio_input_cjs_namespaceObject.useAudioInput)({
        handleAudioInputData,
        handleAudioInputStart,
        handleAudioInputEnd
    });
    (0, external_react_namespaceObject.useEffect)(()=>()=>{
            if (isActiveRef.current) {
                isActiveRef.current = false;
                stopAudioInput();
                clearOutputAudioQueue();
                onActiveChangeRef.current?.(false);
            }
        }, [
        stopAudioInput,
        clearOutputAudioQueue
    ]);
    const handleClick = (0, external_react_namespaceObject.useCallback)(async ()=>{
        if (disabled) return;
        if (isActive) stopAudioInput();
        else await startAudioInput(true);
    }, [
        disabled,
        isActive,
        startAudioInput,
        stopAudioInput
    ]);
    const iconColor = disabled ? 'var(--color-foreground-disable)' : isActive ? 'var(--color-background)' : void 0;
    const stopLabel = _({
        id: 'autopilot-chat.input.actions.stop'
    });
    const voiceLabel = _({
        id: 'autopilot-chat.input.actions.voice-interaction'
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(VoiceButtonContainer, {
        active: isActive,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
            iconName: isActive ? 'stop' : 'graphic_eq',
            tooltip: isActive ? stopLabel : voiceLabel,
            overrideColor: iconColor,
            preventHover: true,
            disabled: disabled,
            onClick: handleClick,
            "data-testid": "autopilot-chat-always-on-voice-button",
            ariaLabel: isActive ? stopLabel : voiceLabel,
            ariaPressed: isActive
        })
    });
};
exports.AlwaysOnVoiceButton = __webpack_exports__.AlwaysOnVoiceButton;
exports.VoiceButtonContainer = __webpack_exports__.VoiceButtonContainer;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AlwaysOnVoiceButton",
    "VoiceButtonContainer"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
