"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__, {
    Attachment: ()=>Attachment,
    Attachments: ()=>Attachments
});
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 external_index_cjs_namespaceObject = require("../../../index.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const index_cjs_namespaceObject = require("../../service/index.cjs");
const file_to_icon_cjs_namespaceObject = require("../../utils/file-to-icon.cjs");
const external_action_button_cjs_namespaceObject = require("./action-button.cjs");
const external_tooltip_cjs_namespaceObject = require("./tooltip.cjs");
const AttachmentIcon = (0, material_namespaceObject.styled)('span')(({ fileType, width, height })=>({
        width: width ?? apollo_core_default().Spacing.SpacingM,
        height: height ?? apollo_core_default().Spacing.SpacingM,
        boxSizing: 'border-box',
        flexShrink: 0,
        display: 'inline-flex',
        alignItems: 'center',
        justifyContent: 'center',
        borderRadius: apollo_core_default().Border.BorderRadiusM,
        lineHeight: 0,
        ...fileType === index_cjs_namespaceObject.AutopilotChatFileType.File ? {
            backgroundColor: '#078E9E'
        } : {
            backgroundColor: 'var(--color-background-secondary)'
        },
        '& svg': {
            transform: 'scale(0.8)',
            display: 'block'
        }
    }));
const AttachmentsContainer = (0, material_namespaceObject.styled)('div')(({ removeSpacing, disableOverflow })=>({
        display: 'flex',
        flexWrap: 'wrap',
        padding: removeSpacing ? 0 : `0 ${apollo_core_default().Spacing.SpacingBase}`,
        marginTop: removeSpacing ? 0 : apollo_core_default().Spacing.SpacingMicro,
        gap: `0 ${apollo_core_default().Spacing.SpacingXs}`,
        boxSizing: 'border-box',
        ...disableOverflow ? {
            overflow: 'hidden',
            maxWidth: '100%',
            width: '100%'
        } : {
            overflowY: 'auto',
            maxHeight: '190px'
        }
    }));
const StyledAttachment = (0, material_namespaceObject.styled)('div')(({ showRemoveIcon, isFullWidth })=>({
        display: 'flex',
        alignItems: 'center',
        gap: apollo_core_default().Spacing.SpacingXs,
        padding: apollo_core_default().Spacing.SpacingMicro,
        margin: `${apollo_core_default().Spacing.SpacingMicro} 0`,
        boxSizing: 'border-box',
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        border: `${apollo_core_default().Border.BorderThickS} solid var(--color-border-de-emp)`,
        position: 'relative',
        maxWidth: isFullWidth ? '100%' : `calc(50% - ${apollo_core_default().Spacing.SpacingXs} / 2)`,
        outlineColor: 'var(--color-focus-indicator)',
        backgroundColor: 'var(--color-background)',
        '& .attachment-name': {
            textOverflow: 'ellipsis',
            overflow: 'hidden',
            whiteSpace: 'nowrap',
            color: 'var(--color-foreground)'
        },
        '& .attachment-remove': {
            opacity: showRemoveIcon ? 1 : 0,
            marginLeft: 'auto',
            position: 'absolute',
            right: apollo_core_default().Spacing.SpacingMicro,
            backgroundColor: 'var(--color-background)',
            height: apollo_core_default().Spacing.SpacingM,
            display: 'flex',
            '& .MuiButtonBase-root': {
                height: apollo_core_default().Spacing.SpacingM,
                width: apollo_core_default().Spacing.SpacingM,
                padding: 0
            }
        }
    }));
const Attachment = /*#__PURE__*/ external_react_default().memo(({ attachment, onRemove, shouldFocus, isFullWidth, loading })=>{
    const { _ } = (0, react_namespaceObject.useLingui)();
    const { spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const removeButtonRef = external_react_default().useRef(null);
    const [isRemoveIconVisible, setIsRemoveIconVisible] = external_react_default().useState(false);
    const [isFocused, setIsFocused] = external_react_default().useState(false);
    const attachmentRef = external_react_default().useRef(null);
    const lastMousePosition = external_react_default().useRef({
        x: 0,
        y: 0
    });
    external_react_default().useEffect(()=>{
        if (shouldFocus && removeButtonRef.current) removeButtonRef.current.focus();
    }, [
        shouldFocus
    ]);
    external_react_default().useEffect(()=>{
        if (!onRemove) return;
        const handleMouseMove = (e)=>{
            if (isFocused) return;
            lastMousePosition.current = {
                x: e.clientX,
                y: e.clientY
            };
            if (attachmentRef.current) {
                const rect = attachmentRef.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,
        onRemove
    ]);
    const [IconComponent, friendlyType] = external_react_default().useMemo(()=>{
        const { icon: IconComp, friendlyType: fileType } = (0, file_to_icon_cjs_namespaceObject.fileToIcon)(attachment.name);
        return [
            IconComp,
            fileType
        ];
    }, [
        attachment
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledAttachment, {
        ref: attachmentRef,
        id: attachment.name,
        showRemoveIcon: (isRemoveIconVisible || isFocused) && !!onRemove,
        isFullWidth: isFullWidth,
        tabIndex: 0,
        onFocus: ()=>setIsFocused(true),
        onBlur: ()=>setIsFocused(false),
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AttachmentIcon, {
                fileType: friendlyType,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(IconComponent, {})
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_tooltip_cjs_namespaceObject.AutopilotChatTooltip, {
                title: attachment.name,
                placement: "top",
                disableInteractive: true,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApTypography, {
                    variant: spacing.primaryFontToken,
                    className: "attachment-name",
                    children: attachment.name
                })
            }),
            onRemove && !loading && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: "attachment-remove",
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                    ref: removeButtonRef,
                    onFocus: ()=>setIsFocused(true),
                    onBlur: ()=>setIsFocused(false),
                    iconSize: apollo_core_default().Icon.IconS,
                    iconName: "close",
                    tooltip: _({
                        id: 'autopilot-chat.common.attachments.remove-file'
                    }),
                    ariaLabel: _({
                        id: 'autopilot-chat.common.attachments.remove-file-name',
                        values: {
                            0: attachment.name
                        }
                    }),
                    onClick: ()=>{
                        if (onRemove) onRemove(attachment.name);
                        requestAnimationFrame(()=>{
                            const event = new MouseEvent('mousemove', {
                                clientX: lastMousePosition.current.x,
                                clientY: lastMousePosition.current.y
                            });
                            document.dispatchEvent(event);
                        });
                    },
                    "data-testid": "autopilot-chat-attachment-remove"
                })
            }),
            loading && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: "attachment-loading",
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.CircularProgress, {
                    size: 16,
                    color: "primary"
                })
            })
        ]
    });
});
const Attachments = /*#__PURE__*/ external_react_default().memo(({ attachments, attachmentsLoading, onRemove, removeSpacing, disableOverflow })=>{
    const [focusedAttachmentIndex, setFocusedAttachmentIndex] = external_react_default().useState(null);
    const prevAttachmentsLength = external_react_default().useRef(attachments.length);
    external_react_default().useEffect(()=>{
        const currentLength = attachments.length;
        const prevLength = prevAttachmentsLength.current;
        if (currentLength < prevLength && null !== focusedAttachmentIndex) {
            if (focusedAttachmentIndex >= currentLength) 0 === currentLength ? setFocusedAttachmentIndex(null) : setFocusedAttachmentIndex(currentLength - 1);
        }
        prevAttachmentsLength.current = currentLength;
    }, [
        attachments.length,
        focusedAttachmentIndex
    ]);
    const handleRemoveAttachment = external_react_default().useCallback((name, index)=>{
        if (onRemove) {
            setFocusedAttachmentIndex(index);
            onRemove(name, index);
        }
    }, [
        onRemove
    ]);
    const hasOddNumberOfAttachments = attachments.length % 2 !== 0;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AttachmentsContainer, {
        removeSpacing: removeSpacing,
        disableOverflow: disableOverflow,
        children: attachments.map((attachment, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Attachment, {
                attachment: attachment,
                loading: attachmentsLoading?.find((a)=>a.name === attachment.name && a.size === attachment.size)?.loading,
                onRemove: onRemove ? (name)=>handleRemoveAttachment(name, index) : void 0,
                shouldFocus: index === focusedAttachmentIndex,
                isFullWidth: hasOddNumberOfAttachments && index === attachments.length - 1
            }, attachment.name + attachment.size))
    });
});
exports.Attachment = __webpack_exports__.Attachment;
exports.Attachments = __webpack_exports__.Attachments;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "Attachment",
    "Attachments"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
