import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useLingui } from "@lingui/react";
import { styled } from "@mui/material/styles";
import apollo_core from "@uipath/apollo-core";
import react from "react";
import { useAttachments } from "../../providers/attachements-provider.js";
import { useChatService } from "../../providers/chat-service.provider.js";
import { useChatState } from "../../providers/chat-state-provider.js";
import { usePicker } from "../../providers/picker-provider.js";
import { AutopilotChatMode, AutopilotChatPreHookAction } from "../../service/index.js";
import { AutopilotChatActionButton } from "../common/action-button.js";
import { AutopilotChatHeaderActionMenu } from "./header-action-menu.js";
const StyledActions = styled('div')(()=>({
        display: 'flex',
        alignItems: 'center',
        gap: apollo_core.Spacing.SpacingMicro
    }));
function AutopilotChatHeaderActionsComponent() {
    const { _ } = useLingui();
    const chatService = useChatService();
    const { disabledFeatures, chatMode, historyOpen, settingsOpen, setHistoryAnchorElement, readOnly } = useChatState();
    const { clearAttachments } = useAttachments();
    const { customHeaderActions, handleCustomHeaderAction } = usePicker();
    const [actionMenuAnchorEl, setActionMenuAnchorEl] = react.useState(null);
    const handleOpenActionMenu = react.useCallback((event)=>{
        setActionMenuAnchorEl(event.currentTarget);
    }, [
        setActionMenuAnchorEl
    ]);
    const handleCloseActionMenu = react.useCallback(()=>{
        setActionMenuAnchorEl(null);
    }, [
        setActionMenuAnchorEl
    ]);
    const handleActionMenuItemClick = react.useCallback((action)=>{
        handleCustomHeaderAction(action);
        setActionMenuAnchorEl(null);
    }, [
        handleCustomHeaderAction,
        setActionMenuAnchorEl
    ]);
    const handleClose = react.useCallback(()=>{
        if (!chatService) return;
        chatService.getPreHook(AutopilotChatPreHookAction.CloseChat)().then((proceed)=>{
            if (!proceed) return;
            chatService?.close();
        });
    }, [
        chatService
    ]);
    const handleToggleChat = react.useCallback(()=>{
        if (!chatService) return;
        chatService.getPreHook(AutopilotChatPreHookAction.ToggleChat)({
            chatMode
        }).then((proceed)=>{
            if (!proceed) return;
            chatService?.setChatMode(chatMode === AutopilotChatMode.FullScreen ? AutopilotChatMode.SideBySide : AutopilotChatMode.FullScreen);
        });
    }, [
        chatService,
        chatMode
    ]);
    const handleNewChat = react.useCallback(()=>{
        if (!chatService) return;
        chatService.getPreHook(AutopilotChatPreHookAction.NewChat)().then((proceed)=>{
            if (!proceed) return;
            chatService.newChat();
            chatService.stopResponse();
            chatService.setPrompt('');
            clearAttachments();
        });
    }, [
        clearAttachments,
        chatService
    ]);
    const toggleHistory = react.useCallback(()=>{
        if (!chatService) return;
        chatService.getPreHook(AutopilotChatPreHookAction.ToggleHistory)({
            historyOpen
        }).then((proceed)=>{
            if (!proceed) return;
            chatService?.toggleHistory();
        });
    }, [
        chatService,
        historyOpen
    ]);
    const toggleSettings = react.useCallback(()=>{
        if (!chatService) return;
        chatService.getPreHook(AutopilotChatPreHookAction.ToggleSettings)({
            settingsOpen
        }).then((proceed)=>{
            if (!proceed) return;
            chatService?.toggleSettings();
        });
    }, [
        chatService,
        settingsOpen
    ]);
    return /*#__PURE__*/ jsxs(StyledActions, {
        children: [
            !readOnly && !disabledFeatures.newChat && /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                iconName: "new_chat",
                variant: "custom",
                tooltip: _({
                    id: 'autopilot-chat.header.actions.new-chat'
                }),
                ariaLabel: _({
                    id: 'autopilot-chat.header.actions.new-chat'
                }),
                onClick: handleNewChat,
                "data-testid": "autopilot-chat-new-chat"
            }),
            !readOnly && !disabledFeatures.settings && /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                iconName: "settings",
                tooltip: _({
                    id: 'autopilot-chat.header.actions.settings'
                }),
                ariaLabel: _({
                    id: 'autopilot-chat.header.actions.settings'
                }),
                onClick: toggleSettings,
                "data-testid": "autopilot-chat-settings"
            }),
            !readOnly && !disabledFeatures.history && /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                ref: setHistoryAnchorElement,
                iconName: "history",
                variant: "custom",
                tooltip: _({
                    id: 'autopilot-chat.header.actions.history'
                }),
                ariaLabel: _({
                    id: 'autopilot-chat.header.actions.history'
                }),
                onClick: toggleHistory,
                "data-testid": "autopilot-chat-history"
            }),
            !disabledFeatures.fullScreen && chatMode !== AutopilotChatMode.Embedded && /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                iconName: chatMode !== AutopilotChatMode.FullScreen ? 'open_in_full' : 'close_fullscreen',
                tooltip: chatMode !== AutopilotChatMode.FullScreen ? _({
                    id: 'autopilot-chat.header.actions.expand'
                }) : _({
                    id: 'autopilot-chat.header.actions.collapse'
                }),
                ariaLabel: chatMode !== AutopilotChatMode.FullScreen ? _({
                    id: 'autopilot-chat.header.actions.expand'
                }) : _({
                    id: 'autopilot-chat.header.actions.collapse'
                }),
                onClick: handleToggleChat,
                "data-testid": "autopilot-chat-toggle-fullscreen"
            }),
            !readOnly && customHeaderActions.length > 0 && /*#__PURE__*/ jsxs(Fragment, {
                children: [
                    /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                        iconName: "more_vert",
                        variant: "outlined",
                        tooltip: _({
                            id: 'autopilot-chat.header.actions.custom-actions'
                        }),
                        onClick: handleOpenActionMenu,
                        "data-testid": "autopilot-chat-action-menu"
                    }),
                    /*#__PURE__*/ jsx(AutopilotChatHeaderActionMenu, {
                        actions: customHeaderActions,
                        onActionClick: handleActionMenuItemClick,
                        anchorEl: actionMenuAnchorEl,
                        open: !!actionMenuAnchorEl,
                        onClose: handleCloseActionMenu
                    })
                ]
            }),
            (chatMode !== AutopilotChatMode.Embedded && !disabledFeatures.close || chatMode === AutopilotChatMode.Embedded && false === disabledFeatures.close) && /*#__PURE__*/ jsx(AutopilotChatActionButton, {
                iconName: "remove",
                onClick: handleClose,
                tooltip: _({
                    id: 'autopilot-chat.header.actions.close'
                }),
                ariaLabel: _({
                    id: 'autopilot-chat.header.actions.close'
                }),
                "data-testid": "autopilot-chat-close"
            })
        ]
    });
}
const AutopilotChatHeaderActions = /*#__PURE__*/ react.memo(AutopilotChatHeaderActionsComponent);
export { AutopilotChatHeaderActions };
