"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__, {
    AutopilotChatDropzone: ()=>AutopilotChatDropzone
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const styles_namespaceObject = require("@mui/material/styles");
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_react_dropzone_namespaceObject = require("react-dropzone");
const index_cjs_namespaceObject = require("../../../ap-typography/index.cjs");
const attachements_provider_cjs_namespaceObject = require("../../providers/attachements-provider.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const error_provider_cjs_namespaceObject = require("../../providers/error-provider.cjs");
const file_reader_cjs_namespaceObject = require("../../utils/file-reader.cjs");
const DropzoneRoot = (0, styles_namespaceObject.styled)('div')({
    height: '100%',
    position: 'relative'
});
const DropzoneContent = (0, styles_namespaceObject.styled)('div')(({ isDragActive })=>({
        opacity: isDragActive ? 0.3 : 1
    }));
const DropzoneOverlay = (0, styles_namespaceObject.styled)('div')(()=>({
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
        textAlign: 'center',
        pointerEvents: 'none',
        border: `${apollo_core_default().Border.BorderThickM} dashed var(--color-border)`,
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        margin: 0,
        padding: apollo_core_default().Spacing.SpacingXxl,
        gap: apollo_core_default().Spacing.SpacingM,
        height: '100%',
        width: '100%',
        boxSizing: 'border-box'
    }));
function AutopilotChatDropzoneComponent({ children, ...dropzoneOptions }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const { addAttachments, attachments } = (0, attachements_provider_cjs_namespaceObject.useAttachments)();
    const { setError } = (0, error_provider_cjs_namespaceObject.useError)();
    const { disabledFeatures, allowedAttachments } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const attachmentsCountRef = external_react_default().useRef(attachments.length);
    attachmentsCountRef.current = attachments.length;
    const handleRejections = external_react_default().useCallback((parsedFiles, fileRejections)=>{
        if (!allowedAttachments.multiple && (parsedFiles.length > 1 || attachmentsCountRef.current > 0 || fileRejections.length > 1)) {
            setError(_({
                id: 'autopilot-chat.error.multiple-files'
            }));
            return true;
        }
        if (fileRejections.length > 0) {
            const tooManyFilesRejection = fileRejections.find((rejection)=>rejection.errors[0]?.code === external_react_dropzone_namespaceObject.ErrorCode.TooManyFiles);
            if (tooManyFilesRejection) {
                const maxCount = allowedAttachments.maxCount;
                setError(_({
                    id: 'autopilot-chat.error.too-many-files',
                    values: {
                        maxCount: maxCount
                    }
                }));
                return true;
            }
            const errorMessages = [];
            for (const { errors, file } of fileRejections){
                const errorCode = errors[0]?.code;
                if (errorCode === external_react_dropzone_namespaceObject.ErrorCode.FileTooLarge) {
                    const fileSizeMB = Math.round(file.size / 1024 / 1024);
                    const fileName = file.name;
                    const maxSize = allowedAttachments.maxSize / 1024 / 1024;
                    errorMessages.push(_({
                        id: 'autopilot-chat.error.file-too-large',
                        values: {
                            fileName: fileName,
                            fileSizeMB: fileSizeMB,
                            maxSize: maxSize
                        }
                    }));
                }
                if (errorCode === external_react_dropzone_namespaceObject.ErrorCode.FileInvalidType) {
                    const fileName = file.name;
                    errorMessages.push(_({
                        id: 'autopilot-chat.error.file-invalid-type',
                        values: {
                            fileName: fileName
                        }
                    }));
                }
            }
            if (errorMessages.length > 0) setError(errorMessages.join('\n'));
        }
        return false;
    }, [
        _,
        setError,
        allowedAttachments.maxCount,
        allowedAttachments.maxSize,
        allowedAttachments.multiple
    ]);
    const handleDrop = external_react_default().useCallback(async (files, fileRejections)=>{
        try {
            const parsedFiles = await (0, file_reader_cjs_namespaceObject.parseFiles)(files);
            const shouldReturn = handleRejections(parsedFiles, fileRejections);
            if (shouldReturn) return;
            if (0 === fileRejections.length) setError(void 0);
            if (allowedAttachments.multiple) addAttachments(parsedFiles);
            else if (1 === parsedFiles.length) addAttachments([
                parsedFiles[0]
            ]);
        } catch (error) {
            setError(error);
            return;
        }
    }, [
        addAttachments,
        setError,
        allowedAttachments.multiple,
        handleRejections
    ]);
    const { getRootProps, getInputProps, isDragActive } = (0, external_react_dropzone_namespaceObject.useDropzone)({
        noClick: true,
        noKeyboard: true,
        onDrop: handleDrop,
        accept: allowedAttachments.types,
        maxSize: allowedAttachments.maxSize,
        maxFiles: allowedAttachments.maxCount,
        disabled: disabledFeatures.attachments,
        ...dropzoneOptions
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(DropzoneRoot, {
        ...getRootProps(),
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("input", {
                ...getInputProps(),
                multiple: allowedAttachments.multiple,
                style: {
                    display: 'none'
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DropzoneContent, {
                isDragActive: isDragActive,
                children: children
            }),
            isDragActive && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(DropzoneOverlay, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeMBold,
                        color: 'var(--color-foreground)',
                        children: _({
                            id: 'autopilot-chat.dropzone.overlay-title'
                        })
                    }),
                    allowedAttachments.maxCount && allowedAttachments.maxCount > 1 && allowedAttachments.multiple && (()=>{
                        const maxCount = allowedAttachments.maxCount;
                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                            variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                            color: 'var(--color-foreground)',
                            children: _({
                                id: 'autopilot-chat.dropzone.overlay-max-count',
                                values: {
                                    maxCount: maxCount
                                }
                            })
                        });
                    })(),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        color: 'var(--color-foreground)',
                        children: (()=>{
                            const maxSize = allowedAttachments.maxSize / 1024 / 1024;
                            return _({
                                id: 'autopilot-chat.dropzone.overlay-max-size',
                                values: {
                                    maxSize: maxSize
                                }
                            });
                        })()
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                        variant: apollo_core_namespaceObject.FontVariantToken.fontSizeS,
                        color: 'var(--color-foreground)',
                        children: (()=>{
                            const fileTypes = Object.values(allowedAttachments.types).flat().join(', ');
                            return _({
                                id: 'autopilot-chat.dropzone.allowed-file-types',
                                values: {
                                    fileTypes: fileTypes
                                }
                            });
                        })()
                    })
                ]
            })
        ]
    });
}
const AutopilotChatDropzone = /*#__PURE__*/ external_react_default().memo(AutopilotChatDropzoneComponent);
exports.AutopilotChatDropzone = __webpack_exports__.AutopilotChatDropzone;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatDropzone"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
