"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__, {
    AutopilotChatHistoryItem: ()=>AutopilotChatHistoryItem
});
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 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 external_service_index_cjs_namespaceObject = require("../../service/index.cjs");
const action_button_cjs_namespaceObject = require("../common/action-button.cjs");
const GroupItem = (0, material_namespaceObject.styled)('div')(({ isActive, showRemoveIcon })=>({
        width: `calc(100% - 2 * ${apollo_core_default().Spacing.SpacingBase})`,
        padding: `0 calc(${apollo_core_default().Padding.PadL} + ${apollo_core_default().Spacing.SpacingBase})`,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'space-between',
        cursor: 'pointer',
        borderRadius: apollo_core_default().Border.BorderRadiusS,
        outlineColor: 'var(--color-focus-indicator)',
        outlineWidth: '1px',
        outlineOffset: '-1px',
        '&:hover, &:active': {
            backgroundColor: 'var(--color-background-hover)'
        },
        ...isActive && {
            backgroundColor: 'var(--color-background-selected)',
            borderLeft: "4px solid var(--color-selection-indicator)"
        },
        '& .delete-button-wrapper': {
            opacity: showRemoveIcon ? 1 : 0,
            position: 'relative',
            left: apollo_core_default().Spacing.SpacingXs,
            marginRight: apollo_core_default().Spacing.SpacingBase
        }
    }));
const GroupTitle = (0, material_namespaceObject.styled)('div')(()=>({
        flex: 1,
        minWidth: 0,
        '& .MuiTypography-root': {
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap'
        }
    }));
const AutopilotChatHistoryItemComponent = ({ item, isHistoryOpen })=>{
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const [isActive, setIsActive] = external_react_default().useState(chatService.activeConversationId === item.id);
    const { setWaitingResponse } = (0, loading_provider_cjs_namespaceObject.useLoading)();
    const [isRemoveIconVisible, setIsRemoveIconVisible] = external_react_default().useState(false);
    const [isFocused, setIsFocused] = external_react_default().useState(false);
    const itemRef = external_react_default().useRef(null);
    const lastMousePosition = external_react_default().useRef({
        x: 0,
        y: 0
    });
    const focusButtonRef = external_react_default().useRef(null);
    external_react_default().useEffect(()=>{
        const handleMouseMove = (e)=>{
            if (isFocused) return;
            lastMousePosition.current = {
                x: e.clientX,
                y: e.clientY
            };
            if (itemRef.current) {
                const rect = itemRef.current.getBoundingClientRect();
                const isInside = e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom;
                setIsRemoveIconVisible(isInside);
            }
        };
        document.addEventListener('mousemove', handleMouseMove);
        return ()=>document.removeEventListener('mousemove', handleMouseMove);
    }, [
        isFocused
    ]);
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        if (chatService.activeConversationId === item.id && !isActive) setIsActive(true);
        const unsubscribeOpenConversation = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.OpenConversation, (id)=>{
            if (!isActive && item.id === id) setWaitingResponse(false);
            setIsActive(id === item.id);
        });
        const unsubscribeDeleteConversation = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.DeleteConversation, (id)=>{
            if (isActive && id === item.id) chatService.newChat();
        });
        const unsubscribeNewChat = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.NewChat, ()=>{
            setIsActive(false);
        });
        return ()=>{
            unsubscribeOpenConversation();
            unsubscribeDeleteConversation();
            unsubscribeNewChat();
        };
    }, [
        chatService,
        item.id,
        isActive,
        setWaitingResponse
    ]);
    const handleDelete = external_react_default().useCallback((ev, itemId)=>{
        ev.stopPropagation();
        setIsFocused(false);
        if (!chatService) return;
        chatService.getPreHook(external_service_index_cjs_namespaceObject.AutopilotChatPreHookAction.DeleteConversation)({
            conversationId: itemId
        }).then((proceed)=>{
            if (!proceed) return;
            chatService.deleteConversation(itemId);
        });
    }, [
        chatService
    ]);
    const handleItemClick = external_react_default().useCallback((itemId)=>{
        if (isActive) return;
        chatService.openConversation(itemId);
        chatService.toggleHistory(false);
    }, [
        chatService,
        isActive
    ]);
    external_react_default().useEffect(()=>{
        if (isFocused) focusButtonRef.current?.focus();
    }, [
        isFocused
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(GroupItem, {
        showRemoveIcon: isFocused || isRemoveIconVisible,
        ref: itemRef,
        tabIndex: isHistoryOpen ? 0 : -1,
        isActive: isActive,
        onClick: ()=>handleItemClick(item.id),
        onKeyDown: (ev)=>{
            if ('Enter' === ev.key || ' ' === ev.key) handleItemClick(item.id);
        },
        "aria-label": item.name,
        role: "button",
        "aria-pressed": isActive,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(GroupTitle, {
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                    variant: spacing.primaryFontToken,
                    color: 'var(--color-foreground)',
                    children: item.name
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: "delete-button-wrapper",
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                    disabled: !isHistoryOpen,
                    ref: focusButtonRef,
                    onClick: (ev)=>handleDelete(ev, item.id),
                    onFocus: ()=>{
                        setIsFocused(true);
                    },
                    onBlur: ()=>{
                        setIsFocused(false);
                    },
                    onKeyDown: (ev)=>{
                        if ('Escape' !== ev.key) ev.stopPropagation();
                        if ('Enter' === ev.key || ' ' === ev.key) handleDelete(ev, item.id);
                    },
                    iconName: "delete",
                    iconSize: "16px",
                    tooltip: (isRemoveIconVisible || isFocused) && isHistoryOpen ? _({
                        id: 'autopilot-chat.history.delete'
                    }) : '',
                    "data-testid": "autopilot-chat-history-delete",
                    ariaLabel: _({
                        id: 'autopilot-chat.history.delete'
                    })
                })
            })
        ]
    }, item.id);
};
const AutopilotChatHistoryItem = /*#__PURE__*/ external_react_default().memo(AutopilotChatHistoryItemComponent);
exports.AutopilotChatHistoryItem = __webpack_exports__.AutopilotChatHistoryItem;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatHistoryItem"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
