"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__, {
    AutopilotChatActionsList: ()=>AutopilotChatActionsList
});
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 use_scheduled_callback_cjs_namespaceObject = require("../../../hooks/use-scheduled-callback.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 index_cjs_namespaceObject = require("../../../service/index.cjs");
const action_button_cjs_namespaceObject = require("../../common/action-button.cjs");
const ActionsListContainer = (0, material_namespaceObject.styled)('div')(({ isRequest })=>({
        display: 'flex',
        gap: apollo_core_default().Spacing.SpacingMicro,
        position: 'absolute',
        bottom: isRequest ? `-${apollo_core_default().Spacing.SpacingS}` : `-${apollo_core_default().Spacing.SpacingXl}`,
        ...isRequest ? {
            right: apollo_core_default().Spacing.SpacingS,
            backgroundColor: 'var(--color-background)',
            borderRadius: apollo_core_default().Border.BorderRadiusM,
            border: "1px solid var(--color-border-grid)"
        } : {
            left: 0
        },
        transition: 'opacity 0.2s ease-in-out',
        zIndex: 1
    }));
function AutopilotChatActionsListComponent({ message, defaultActions, isVisible, setIsVisible, onHoverChange, actionsContainerRef }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const { portalContainer } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const [anchorEl, setAnchorEl] = external_react_default().useState(null);
    const overflowMenuOpen = Boolean(anchorEl);
    const overflowButtonRef = external_react_default().useRef(null);
    const popoverActionRef = external_react_default().useRef(null);
    const updatePosition = external_react_default().useCallback(()=>{
        if (popoverActionRef.current) popoverActionRef.current.updatePosition();
    }, []);
    const schedulePositionUpdate = (0, use_scheduled_callback_cjs_namespaceObject.useScheduledCallback)(updatePosition);
    const handleTransitionEnter = external_react_default().useCallback(()=>{
        schedulePositionUpdate();
    }, [
        schedulePositionUpdate
    ]);
    const handleTransitionEntered = external_react_default().useCallback(()=>{
        schedulePositionUpdate();
    }, [
        schedulePositionUpdate
    ]);
    const mainActions = [
        ...defaultActions?.filter((action)=>!action.showInOverflow) || [],
        ...message?.actions?.filter((action)=>!action.showInOverflow) || []
    ];
    const overflowActions = [
        ...defaultActions?.filter((action)=>action.showInOverflow) || [],
        ...message?.actions?.filter((action)=>action.showInOverflow) || []
    ];
    const shouldBeVisible = isVisible || overflowMenuOpen;
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const handleAction = external_react_default().useCallback(async (action)=>{
        if (!action.eventName || !chatService) return;
        const group = chatService.getMessagesInGroup(message.groupId ?? '');
        const currentMessage = chatService.getConversation().find((m)=>m.id === message.id);
        if (action.details?.preHookAction) {
            const proceed = await chatService.getPreHook(action.details.preHookAction)({
                isPositive: action.details.isPositive,
                group,
                message: currentMessage
            });
            if (!proceed) return;
        }
        chatService._eventBus.publish(action.eventName, {
            group,
            message: currentMessage,
            action
        });
    }, [
        message,
        chatService
    ]);
    const handleOpenOverflowMenu = external_react_default().useCallback((event)=>{
        setAnchorEl(event.currentTarget);
        setIsVisible(true);
    }, [
        setIsVisible
    ]);
    const handleCloseOverflowMenu = external_react_default().useCallback(()=>{
        setAnchorEl(null);
        setIsVisible(false);
    }, [
        setIsVisible
    ]);
    const handleMouseEnter = external_react_default().useCallback(()=>{
        setIsVisible(true);
        onHoverChange?.(true);
    }, [
        setIsVisible,
        onHoverChange
    ]);
    const handleMouseLeave = external_react_default().useCallback(()=>{
        if (!overflowMenuOpen) onHoverChange?.(false);
    }, [
        overflowMenuOpen,
        onHoverChange
    ]);
    const containerRef = external_react_default().useCallback((node)=>{
        if (actionsContainerRef) actionsContainerRef(node);
    }, [
        actionsContainerRef
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ActionsListContainer, {
        ref: containerRef,
        isRequest: message.role === index_cjs_namespaceObject.AutopilotChatRole.User,
        style: {
            opacity: shouldBeVisible ? 1 : 0
        },
        role: "group",
        "aria-label": _({
            id: 'autopilot-chat.message.actions.label'
        }),
        onMouseEnter: handleMouseEnter,
        onMouseLeave: handleMouseLeave,
        onFocus: handleMouseEnter,
        children: [
            mainActions.map((action)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                    onClick: ()=>handleAction(action),
                    onKeyDown: (ev)=>{
                        if ('Enter' === ev.key || ' ' === ev.key) handleAction(action);
                    },
                    onMouseEnter: handleMouseEnter,
                    onMouseLeave: handleMouseLeave,
                    onFocus: handleMouseEnter,
                    iconName: action.icon ?? '',
                    iconSize: "16px",
                    tooltip: action.label,
                    ariaLabel: action.label,
                    variant: action.disabled ? 'normal' : 'outlined',
                    disableInteractiveTooltip: false,
                    disabled: action.disabled ?? false,
                    "data-testid": `autopilot-chat-message-action-${action.name}`
                }, action.name)),
            overflowActions.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                        ref: overflowButtonRef,
                        iconName: "more_vert",
                        iconSize: "16px",
                        tooltip: _({
                            id: 'autopilot-chat.message.actions.more'
                        }),
                        ariaLabel: _({
                            id: 'autopilot-chat.message.actions.more'
                        }),
                        onClick: handleOpenOverflowMenu,
                        onFocus: handleMouseEnter,
                        onMouseEnter: handleMouseEnter,
                        onMouseLeave: handleMouseLeave,
                        disableInteractiveTooltip: false,
                        "data-testid": "autopilot-chat-message-actions-overflow"
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Menu, {
                        anchorEl: anchorEl,
                        open: overflowMenuOpen,
                        onClose: handleCloseOverflowMenu,
                        container: portalContainer,
                        action: popoverActionRef,
                        TransitionProps: {
                            onEnter: handleTransitionEnter,
                            onEntered: handleTransitionEntered
                        },
                        children: overflowActions.map((action)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.MenuItem, {
                                onClick: ()=>{
                                    handleAction(action);
                                    handleCloseOverflowMenu();
                                },
                                "aria-label": action.label,
                                children: action.label
                            }, action.name))
                    })
                ]
            })
        ]
    });
}
const AutopilotChatActionsList = /*#__PURE__*/ external_react_default().memo(AutopilotChatActionsListComponent);
exports.AutopilotChatActionsList = __webpack_exports__.AutopilotChatActionsList;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatActionsList"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
