"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__, {
    AutopilotAttachmentsProvider: ()=>AutopilotAttachmentsProvider,
    AutopilotAttachmentsContext: ()=>AutopilotAttachmentsContext,
    useAttachments: ()=>useAttachments
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const fp_namespaceObject = require("lodash/fp");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../service/index.cjs");
const external_chat_service_provider_cjs_namespaceObject = require("./chat-service.provider.cjs");
const external_chat_state_provider_cjs_namespaceObject = require("./chat-state-provider.cjs");
const external_error_provider_cjs_namespaceObject = require("./error-provider.cjs");
const AutopilotAttachmentsContext = /*#__PURE__*/ external_react_default().createContext({
    attachments: [],
    attachmentsLoading: [],
    addAttachments: ()=>{},
    removeAttachment: ()=>{},
    clearAttachments: ()=>{}
});
function AutopilotAttachmentsProvider({ children }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, external_chat_service_provider_cjs_namespaceObject.useChatService)();
    const prompt = chatService?.getPrompt?.();
    const { allowedAttachments } = (0, external_chat_state_provider_cjs_namespaceObject.useChatState)();
    const { setError } = (0, external_error_provider_cjs_namespaceObject.useError)();
    const [attachments, setAttachments] = external_react_default().useState(prompt?.attachments ?? []);
    const [attachmentsLoading, setAttachmentsLoading] = external_react_default().useState([]);
    const attachmentsRef = external_react_default().useRef(attachments);
    external_react_default().useEffect(()=>{
        const currentAttachmentsWithoutContent = attachmentsRef.current.map(({ content: _content, ...rest })=>rest);
        const newAttachmentsWithoutContent = attachments.map(({ content: _content, ...rest })=>rest);
        if (!(0, fp_namespaceObject.isEqual)(currentAttachmentsWithoutContent, newAttachmentsWithoutContent)) {
            const added = attachments.filter((newAtt)=>!attachmentsRef.current.some((currentAtt)=>currentAtt.name === newAtt.name && currentAtt.size === newAtt.size));
            const removed = attachmentsRef.current.filter((currentAtt)=>!attachments.some((newAtt)=>newAtt.name === currentAtt.name && newAtt.size === currentAtt.size));
            attachmentsRef.current = attachments;
            const eventBus = chatService?._eventBus;
            if (eventBus && (added.length > 0 || removed.length > 0)) {
                eventBus.publish(index_cjs_namespaceObject.AutopilotChatEvent.Attachments, attachments);
                eventBus.publish(index_cjs_namespaceObject.AutopilotChatEvent.SetAttachments, {
                    added,
                    removed
                });
            }
        }
    }, [
        attachments,
        chatService
    ]);
    const addAttachments = external_react_default().useCallback((newFiles)=>{
        const filesToAdd = [
            ...attachmentsRef.current,
            ...newFiles.filter((file)=>!attachmentsRef.current.some((existing)=>existing.name === file.name && existing.size === file.size))
        ];
        const totalCount = filesToAdd.length;
        if (allowedAttachments.maxCount && totalCount > allowedAttachments.maxCount) return void setError(_({
            id: 'autopilot-chat.error.too-many-files',
            values: {
                0: allowedAttachments.maxCount
            }
        }));
        if (!allowedAttachments.multiple && totalCount > 1) return void setError(_({
            id: 'autopilot-chat.error.multiple-files'
        }));
        setAttachments(filesToAdd);
    }, [
        allowedAttachments.maxCount,
        allowedAttachments.multiple,
        setError,
        _
    ]);
    const removeAttachment = external_react_default().useCallback((name, index)=>{
        setAttachments((current)=>current.filter((file, i)=>i !== index || file.name !== name));
    }, []);
    const clearAttachments = external_react_default().useCallback(()=>{
        setAttachments([]);
    }, []);
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        const unsubscribeSetAttachmentsLoading = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetAttachmentsLoading, (state)=>{
            setAttachmentsLoading(state);
        });
        const unsubscribe = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.SetPrompt, (p)=>{
            const newAttachments = 'string' == typeof p ? [] : p.attachments ?? [];
            addAttachments(newAttachments);
        });
        return ()=>{
            unsubscribeSetAttachmentsLoading();
            unsubscribe();
        };
    }, [
        chatService,
        addAttachments
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AutopilotAttachmentsContext.Provider, {
        value: {
            attachments,
            attachmentsLoading,
            addAttachments,
            removeAttachment,
            clearAttachments
        },
        children: children
    });
}
function useAttachments() {
    const context = external_react_default().useContext(AutopilotAttachmentsContext);
    if (!context) throw new Error('useAttachments must be used within a AutopilotAttachmentsProvider');
    return context;
}
exports.AutopilotAttachmentsContext = __webpack_exports__.AutopilotAttachmentsContext;
exports.AutopilotAttachmentsProvider = __webpack_exports__.AutopilotAttachmentsProvider;
exports.useAttachments = __webpack_exports__.useAttachments;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotAttachmentsContext",
    "AutopilotAttachmentsProvider",
    "useAttachments"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
