"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    AutopilotChatService: ()=>AutopilotChatService
});
const external_ChatConstants_cjs_namespaceObject = require("./ChatConstants.cjs");
const external_ChatInternalService_cjs_namespaceObject = require("./ChatInternalService.cjs");
const external_ChatModel_cjs_namespaceObject = require("./ChatModel.cjs");
const external_ContentPartBuilder_cjs_namespaceObject = require("./ContentPartBuilder.cjs");
const external_EventBus_cjs_namespaceObject = require("./EventBus.cjs");
const external_LocalHistory_cjs_namespaceObject = require("./LocalHistory.cjs");
const external_StorageService_cjs_namespaceObject = require("./StorageService.cjs");
class AutopilotChatService {
    static _instances = {};
    _initialConfig = {
        mode: external_ChatModel_cjs_namespaceObject.AutopilotChatMode.Closed,
        locale: 'en',
        theme: 'light',
        allowedAttachments: {
            multiple: true,
            types: external_ChatConstants_cjs_namespaceObject.ACCEPTED_FILES,
            maxSize: external_ChatConstants_cjs_namespaceObject.ACCEPTED_FILE_MAX_SIZE,
            maxCount: external_ChatConstants_cjs_namespaceObject.ACCEPTED_FILE_MAX_COUNT
        },
        disabledFeatures: {
            settings: true,
            headerSeparator: true,
            audio: true,
            audioStreaming: true,
            fullHeight: true,
            htmlPreview: true
        },
        settingsRenderer: ()=>{},
        models: void 0,
        selectedAgentMode: void 0,
        selectedModel: void 0,
        paginatedMessages: false,
        paginatedHistory: false
    };
    _config = {
        ...this._initialConfig
    };
    _eventBus;
    _messageRenderers = [];
    _eventUnsubscribers = [];
    _conversation = [];
    _error;
    _prompt;
    _history = [];
    _historyOpen = false;
    _settingsOpen = false;
    _activeConversationId = null;
    _defaultLoadingMessages = null;
    _loadingMessage = null;
    _loadingMessageDuration = null;
    _internalService;
    _groupId;
    _instanceName;
    _contentPartBuilders = new Map();
    _customHeaderActions = [];
    _getChatModeKey = ()=>'';
    _locale = 'en';
    _theme = 'light';
    _resourceManager;
    _isSpeechToTextActive = false;
    constructor(instanceName){
        this._instanceName = instanceName;
        this._eventBus = new external_EventBus_cjs_namespaceObject.EventBus();
        this._internalService = external_ChatInternalService_cjs_namespaceObject.AutopilotChatInternalService.Instantiate();
        external_LocalHistory_cjs_namespaceObject.LocalHistoryService.Initialize(instanceName, this);
        this.getConfig = this.getConfig.bind(this);
        this.initialize = this.initialize.bind(this);
        this.on = this.on.bind(this);
        this.open = this.open.bind(this);
        this.injectMessageRenderer = this.injectMessageRenderer.bind(this);
        this.renderMessage = this.renderMessage.bind(this);
        this.close = this.close.bind(this);
        this.setError = this.setError.bind(this);
        this.clearError = this.clearError.bind(this);
        this.newChat = this.newChat.bind(this);
        this.setChatMode = this.setChatMode.bind(this);
        this.sendRequest = this.sendRequest.bind(this);
        this.sendResponse = this.sendResponse.bind(this);
        this.setDefaultLoadingMessages = this.setDefaultLoadingMessages.bind(this);
        this.getDefaultLoadingMessages = this.getDefaultLoadingMessages.bind(this);
        this.getLoadingMessageDuration = this.getLoadingMessageDuration.bind(this);
        this.setLoadingMessage = this.setLoadingMessage.bind(this);
        this.getLoadingMessage = this.getLoadingMessage.bind(this);
        this.setPrompt = this.setPrompt.bind(this);
        this.intercept = this.intercept.bind(this);
        this.setFirstRunExperience = this.setFirstRunExperience.bind(this);
        this.setConversation = this.setConversation.bind(this);
        this.getConversation = this.getConversation.bind(this);
        this.getPrompt = this.getPrompt.bind(this);
        this.getError = this.getError.bind(this);
        this.setHistory = this.setHistory.bind(this);
        this.getHistory = this.getHistory.bind(this);
        this.toggleHistory = this.toggleHistory.bind(this);
        this.toggleSettings = this.toggleSettings.bind(this);
        this.deleteConversation = this.deleteConversation.bind(this);
        this.openConversation = this.openConversation.bind(this);
        this.setAllowedAttachments = this.setAllowedAttachments.bind(this);
        this.toggleAutoScroll = this.toggleAutoScroll.bind(this);
        this.setModels = this.setModels.bind(this);
        this.setSelectedModel = this.setSelectedModel.bind(this);
        this.getSelectedModel = this.getSelectedModel.bind(this);
        this.setAgentModes = this.setAgentModes.bind(this);
        this.getAgentModes = this.getAgentModes.bind(this);
        this.setAgentMode = this.setAgentMode.bind(this);
        this.getAgentMode = this.getAgentMode.bind(this);
        this.getMessagesInGroup = this.getMessagesInGroup.bind(this);
        this.setPreHook = this.setPreHook.bind(this);
        this.getPreHook = this.getPreHook.bind(this);
        this.prependOlderMessages = this.prependOlderMessages.bind(this);
        this.appendOlderHistoryItems = this.appendOlderHistoryItems.bind(this);
        this.setSuggestions = this.setSuggestions.bind(this);
        this.sendInputStreamEvent = this.sendInputStreamEvent.bind(this);
        this.sendOutputStreamEvent = this.sendOutputStreamEvent.bind(this);
        this.setShowLoading = this.setShowLoading.bind(this);
        this.setWaiting = this.setWaiting.bind(this);
        this.setAttachmentsLoading = this.setAttachmentsLoading.bind(this);
        this.setCustomHeaderActions = this.setCustomHeaderActions.bind(this);
        this.getCustomHeaderActions = this.getCustomHeaderActions.bind(this);
        this.setGetChatModeKey = this.setGetChatModeKey.bind(this);
        this.setLocale = this.setLocale.bind(this);
        this.getLocale = this.getLocale.bind(this);
        this.setTheme = this.setTheme.bind(this);
        this.getTheme = this.getTheme.bind(this);
        this.setResourceManager = this.setResourceManager.bind(this);
        this.getResourceManager = this.getResourceManager.bind(this);
        this.setReadOnly = this.setReadOnly.bind(this);
        this.getReadOnly = this.getReadOnly.bind(this);
    }
    static Instantiate({ instanceName = external_ChatConstants_cjs_namespaceObject.CHAT_INSTANCE_DEFAULT_NAME, config, messageRenderers = [] }) {
        if (!AutopilotChatService._instances[instanceName]) AutopilotChatService._instances[instanceName] = new AutopilotChatService(instanceName);
        if (config) AutopilotChatService._instances[instanceName].initialize(config);
        messageRenderers.forEach((renderer)=>AutopilotChatService._instances[instanceName].injectMessageRenderer(renderer));
        return AutopilotChatService._instances[instanceName];
    }
    static getInstance(instanceName = external_ChatConstants_cjs_namespaceObject.CHAT_INSTANCE_DEFAULT_NAME) {
        return AutopilotChatService._instances[instanceName];
    }
    get activeConversationId() {
        return this._activeConversationId;
    }
    getConfig() {
        return this._config;
    }
    setGetChatModeKey(getChatModeKey) {
        this._getChatModeKey = getChatModeKey;
    }
    initialize(config, messageRenderers = []) {
        this._eventUnsubscribers.forEach((unsubscribe)=>unsubscribe());
        this._eventUnsubscribers = [];
        this._config = {
            ...this._initialConfig,
            ...config
        };
        if (config.mode) this.setChatMode(config.mode);
        if (config.disabledFeatures) this.setDisabledFeatures(config.disabledFeatures);
        if (config.overrideLabels) this.setOverrideLabels(config.overrideLabels);
        if (config.firstRunExperience) this.setFirstRunExperience(config.firstRunExperience);
        if (void 0 !== config.useLocalHistory) this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.UseLocalHistory, config.useLocalHistory);
        if (config.allowedAttachments) this.setAllowedAttachments({
            ...this._config.allowedAttachments,
            ...config.allowedAttachments
        });
        if (config.models) this.setModels(config.models);
        if (config.selectedModel) this.setSelectedModel(config.selectedModel.id);
        if (config.agentModes) this.setAgentModes(config.agentModes);
        if (config.selectedAgentMode) this.setAgentMode(config.selectedAgentMode.id);
        if (config.spacing) this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetSpacing, config.spacing);
        if (config.theming) this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetTheming, config.theming);
        if (config.locale) this.setLocale(config.locale);
        if (config.theme) this.setTheme(config.theme);
        if (config.resourceManager) this.setResourceManager(config.resourceManager);
        if (void 0 !== config.readOnly) this.setReadOnly(config.readOnly);
        messageRenderers.forEach((renderer)=>this.injectMessageRenderer(renderer));
    }
    patchConfig(config, messageRenderers = []) {
        this._config = {
            ...this._config,
            ...config
        };
        this.initialize(this._config, messageRenderers);
    }
    injectMessageRenderer(renderer) {
        const existingRenderer = this._messageRenderers.find((r)=>r.name === renderer.name);
        if (existingRenderer) {
            const index = this._messageRenderers.indexOf(existingRenderer);
            this._messageRenderers[index] = renderer;
        } else this._messageRenderers.push(renderer);
    }
    getMessageRenderer(name) {
        return this._messageRenderers.find((r)=>r.name === name);
    }
    renderMessage(container, message) {
        const renderer = this._messageRenderers.find((r)=>r.name === message.widget);
        if (!renderer?.render) return;
        return renderer.render(container, message);
    }
    open(config, messageRenderers = []) {
        this.patchConfig({
            mode: external_ChatModel_cjs_namespaceObject.AutopilotChatMode.SideBySide,
            ...config ?? {}
        }, messageRenderers);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Open);
    }
    close() {
        this.setChatMode(external_ChatModel_cjs_namespaceObject.AutopilotChatMode.Closed);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Close);
    }
    setChatMode(mode, persist = true) {
        const key = this._getChatModeKey(this._instanceName);
        const storedMode = external_StorageService_cjs_namespaceObject.StorageService.Instance.get(key);
        if (storedMode && mode === external_ChatModel_cjs_namespaceObject.AutopilotChatMode.Closed) external_StorageService_cjs_namespaceObject.StorageService.Instance.remove(key);
        if (persist && mode !== external_ChatModel_cjs_namespaceObject.AutopilotChatMode.Closed) external_StorageService_cjs_namespaceObject.StorageService.Instance.set(key, mode);
        this._config.mode = mode;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.ModeChange, mode);
    }
    setDisabledFeatures(features) {
        this._config.disabledFeatures = {
            ...this._initialConfig.disabledFeatures,
            ...this._config.disabledFeatures,
            ...features
        };
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetDisabledFeatures, this._config.disabledFeatures);
    }
    setOverrideLabels(labels) {
        this._config.overrideLabels = {
            ...this._config.overrideLabels,
            ...labels
        };
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetOverrideLabels, this._config.overrideLabels);
    }
    setFirstRunExperience(config) {
        this._config.firstRunExperience = config;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetFirstRunExperience, config);
    }
    setPrompt(prompt) {
        this._prompt = prompt;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetPrompt, prompt);
    }
    setShowLoading(showLoading) {
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetShowLoading, showLoading);
    }
    setWaiting(waiting) {
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetWaiting, waiting);
    }
    getPrompt() {
        return this._prompt;
    }
    setSuggestions(suggestions, sendOnClick) {
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetSuggestions, {
            suggestions,
            sendOnClick
        });
    }
    setConversation(messages) {
        this._conversation = messages;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShowLoadingState, false);
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetIsLoadingMoreMessages, false);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetConversation, messages);
    }
    prependOlderMessages(messages = [], done) {
        if (done) requestAnimationFrame(()=>{
            this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreMessages, false);
        });
        if (0 === messages.length) return;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.PrependOlderMessages);
        this.setConversation([
            ...messages,
            ...this._conversation
        ]);
    }
    appendOlderHistoryItems(items = [], done) {
        if (done) this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreHistory, false);
        if (0 === items.length) return;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.AppendOlderHistory);
        this.setHistory([
            ...this._history,
            ...items
        ]);
    }
    getConversation() {
        return this._conversation;
    }
    sendRequest(request) {
        this._groupId = crypto.randomUUID();
        const userMessage = {
            id: crypto.randomUUID(),
            created_at: new Date().toISOString(),
            ...request,
            role: external_ChatModel_cjs_namespaceObject.AutopilotChatRole.User,
            widget: external_ChatConstants_cjs_namespaceObject.DEFAULT_MESSAGE_RENDERER
        };
        this.setPrompt('');
        this.setSuggestions([]);
        this._conversation.push(userMessage);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Request, userMessage);
    }
    stopResponse() {
        const lastMessage = this._conversation[this._conversation.length - 1];
        if (lastMessage?.stream && !lastMessage.done) this.sendResponse({
            ...lastMessage,
            done: true,
            content: ''
        });
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.StopResponse);
    }
    sendResponse(response) {
        const lastMessage = this._conversation[this._conversation.length - 1];
        const messageId = response.id || crypto.randomUUID();
        if (lastMessage?.groupId && lastMessage.role === external_ChatModel_cjs_namespaceObject.AutopilotChatRole.Assistant && this._groupId !== lastMessage.groupId) this._groupId = lastMessage.groupId;
        if (response.contentPartChunk) return void this._processContentPartChunk(messageId, response);
        const { contentPartChunk: _contentPartChunk, ...rest } = response;
        const assistantMessage = {
            id: messageId,
            ...rest,
            content: response.content ?? response.contentParts?.map((part)=>part.text).join('') ?? '',
            groupId: response.groupId ?? this._groupId,
            created_at: response.created_at ?? new Date().toISOString(),
            role: external_ChatModel_cjs_namespaceObject.AutopilotChatRole.Assistant
        };
        const existingIndex = this._conversation.findIndex((message)=>message.id === assistantMessage.id);
        if (-1 !== existingIndex) if (response.stream) {
            if (this._conversation[existingIndex]) {
                this._conversation[existingIndex].content += assistantMessage.content;
                this._conversation[existingIndex].done = !!response.done;
                this._conversation[existingIndex].meta = response.meta ?? this._conversation[existingIndex].meta;
                this._conversation[existingIndex].groupId = assistantMessage.groupId ?? this._conversation[existingIndex].groupId;
            }
            this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SendChunk, assistantMessage);
        } else {
            this._conversation[existingIndex] = {
                ...this._conversation[existingIndex],
                ...assistantMessage
            };
            this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Response, assistantMessage);
        }
        else {
            this._conversation.push(assistantMessage);
            this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Response, assistantMessage);
        }
    }
    setDefaultLoadingMessages(messages, duration) {
        this._defaultLoadingMessages = messages;
        if (duration) this._loadingMessageDuration = duration;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetDefaultLoadingMessages, {
            messages,
            duration
        });
    }
    getDefaultLoadingMessages() {
        return this._defaultLoadingMessages;
    }
    getLoadingMessageDuration() {
        return this._loadingMessageDuration;
    }
    setLoadingMessage(message) {
        this._loadingMessage = message;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetLoadingMessage, message);
    }
    getLoadingMessage() {
        return this._loadingMessage;
    }
    newChat(config) {
        if (config) this.patchConfig(config);
        this._groupId = void 0;
        this._conversation = [];
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.NewChat);
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreMessages, true);
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetIsLoadingMoreMessages, false);
    }
    setError(message, level = 'error') {
        if (void 0 === message) return void this.clearError();
        this._error = {
            message,
            level
        };
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Error, this._error);
    }
    clearError() {
        this._error = void 0;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Error, void 0);
    }
    getError() {
        return this._error;
    }
    on(event, handler) {
        return this._eventBus.subscribe(event, handler);
    }
    intercept(event, interceptor) {
        if (Object.values(external_ChatModel_cjs_namespaceObject.AutopilotChatInterceptableEvent).includes(event)) return this._eventBus.intercept(event, interceptor);
        return ()=>{};
    }
    setHistory(history, done) {
        this._history = history;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetIsLoadingMoreHistory, false);
        if (void 0 !== done) this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreHistory, !done);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetHistory, history);
    }
    getHistory() {
        return this._history;
    }
    get historyOpen() {
        return this._historyOpen;
    }
    get settingsOpen() {
        return this._settingsOpen;
    }
    toggleHistory(open) {
        this._historyOpen = open ?? !this._historyOpen;
        if (this._historyOpen) {
            this._settingsOpen = false;
            this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ToggleSettings, false);
        }
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ToggleHistory, this._historyOpen);
    }
    toggleSettings(open) {
        this._settingsOpen = open ?? !this._settingsOpen;
        if (this._settingsOpen) {
            this._historyOpen = false;
            this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ToggleHistory, false);
        }
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ToggleSettings, this._settingsOpen);
    }
    deleteConversation(conversationId) {
        if (conversationId === this._activeConversationId) {
            this._activeConversationId = null;
            this._groupId = void 0;
        }
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.DeleteConversation, conversationId);
    }
    openConversation(conversationId, showLoadingState = true) {
        this._activeConversationId = conversationId;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.OpenConversation, conversationId);
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShowLoadingState, showLoadingState);
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreMessages, true);
    }
    setAllowedAttachments(allowedAttachments) {
        this._config.allowedAttachments = allowedAttachments;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetAllowedAttachments, allowedAttachments);
    }
    toggleAutoScroll(autoScroll) {
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.ToggleAutoScroll, autoScroll);
    }
    setModels(models) {
        this._config.models = models;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetModels, models);
    }
    getModels() {
        return this._config.models;
    }
    setSelectedModel(modelId) {
        if (!this._config.models) return;
        const newSelectedModel = this._config.models.find((model)=>model.id === modelId);
        if (!newSelectedModel) return;
        this._config.selectedModel = newSelectedModel;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetSelectedModel, newSelectedModel);
    }
    getSelectedModel() {
        return this._config.selectedModel;
    }
    setAgentModes(agentModes) {
        this._config.agentModes = agentModes;
        if (!this._config.selectedAgentMode && agentModes[0]) this.setAgentMode(agentModes[0].id);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetAgentModes, agentModes);
    }
    getAgentModes() {
        return this._config.agentModes;
    }
    setAgentMode(modeId) {
        if (!this._config.agentModes) return;
        const newSelectedMode = this._config.agentModes.find((mode)=>mode.id === modeId);
        if (!newSelectedMode) return;
        this._config.selectedAgentMode = newSelectedMode;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetSelectedAgentMode, newSelectedMode);
    }
    getAgentMode() {
        return this._config.selectedAgentMode;
    }
    setCustomHeaderActions(actions) {
        this._customHeaderActions = actions;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetCustomHeaderActions, actions);
    }
    getCustomHeaderActions() {
        return this._customHeaderActions;
    }
    setLocale(locale) {
        this._locale = locale;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetLocale, locale);
    }
    getLocale() {
        return this._locale;
    }
    setTheme(theme) {
        this._theme = theme;
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetTheme, theme);
    }
    getTheme() {
        return this._theme;
    }
    setResourceManager(resourceManager) {
        this._resourceManager = resourceManager;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetResourceManager, resourceManager);
    }
    getResourceManager() {
        return this._resourceManager;
    }
    setReadOnly(readOnly) {
        this._config.readOnly = readOnly;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetReadOnly, readOnly);
    }
    getReadOnly() {
        return this._config.readOnly ?? false;
    }
    get __internalService__() {
        return this._internalService;
    }
    getMessagesInGroup(groupId) {
        return this._conversation.filter((message)=>message.groupId === groupId);
    }
    setPreHook(action, hook) {
        if (!this._config.preHooks) this._config.preHooks = {};
        this._config.preHooks[action] = hook;
    }
    getPreHook(action) {
        return this._config.preHooks?.[action] ?? (()=>Promise.resolve(true));
    }
    sendInputStreamEvent(event) {
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.InputStream, event);
    }
    sendOutputStreamEvent(event) {
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.OutputStream, event);
    }
    publishSpeechToTextToggle() {
        this._isSpeechToTextActive = !this._isSpeechToTextActive;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetSpeechToTextState, this._isSpeechToTextActive);
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SpeechToTextToggle, this._isSpeechToTextActive);
    }
    setSpeechToTextState(isActive) {
        this._isSpeechToTextActive = isActive;
        this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SetSpeechToTextState, isActive);
    }
    get isSpeechToTextActive() {
        return this._isSpeechToTextActive;
    }
    setAttachmentsLoading(attachments) {
        this._internalService.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatInternalEvent.SetAttachmentsLoading, attachments);
    }
    _accumulateContentPart(messageId, chunk) {
        if (!this._contentPartBuilders.has(messageId)) this._contentPartBuilders.set(messageId, new external_ContentPartBuilder_cjs_namespaceObject.ContentPartBuilder());
        const builder = this._contentPartBuilders.get(messageId);
        builder.addChunk(chunk);
    }
    _getAccumulatedContent(messageId) {
        const builder = this._contentPartBuilders.get(messageId);
        if (!builder) return [];
        return builder.getContentParts();
    }
    _clearAccumulatedContent(messageId) {
        this._contentPartBuilders.delete(messageId);
    }
    _processContentPartChunk(messageId, response) {
        const { contentPartChunk, ...rest } = response;
        this._accumulateContentPart(messageId, contentPartChunk);
        const accumulatedContent = this._getAccumulatedContent(messageId);
        const assistantMessage = {
            id: messageId,
            ...rest,
            content: accumulatedContent.map((part)=>part.text).join(''),
            contentParts: accumulatedContent,
            groupId: response.groupId ?? this._groupId,
            created_at: response.created_at ?? new Date().toISOString(),
            role: external_ChatModel_cjs_namespaceObject.AutopilotChatRole.Assistant,
            stream: true,
            done: response.done ?? false
        };
        const existingIndex = this._conversation.findIndex((message)=>message.id === messageId);
        if (-1 !== existingIndex) {
            this._conversation[existingIndex] = {
                ...this._conversation[existingIndex],
                ...assistantMessage
            };
            this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.SendChunk, assistantMessage);
        } else {
            this._conversation.push(assistantMessage);
            this._eventBus.publish(external_ChatModel_cjs_namespaceObject.AutopilotChatEvent.Response, assistantMessage);
        }
        if (response.done) this._clearAccumulatedContent(messageId);
    }
}
exports.AutopilotChatService = __webpack_exports__.AutopilotChatService;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatService"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
