import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import './media/inlineChatContentWidget.css.js';
import { ContentWidgetPositionPreference } from '../../../../editor/browser/editorBrowser.js';
import { trackFocus } from '../../../../base/browser/dom.js';
import { Emitter } from '../../../../base/common/event.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { Position } from '../../../../editor/common/core/position.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { MENU_INLINE_CHAT_EXECUTE, inlineChatBackground, InlineChatConfigKeys, MENU_INLINE_CHAT_CONTENT_STATUS } from '../common/inlineChat.js';
import { ChatWidget } from '../../chat/browser/chatWidget.js';
import { ChatAgentLocation } from '../../chat/common/chatAgents.js';
import '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import { editorForeground, editorBackground } from '../../../../platform/theme/common/colors/editorColors.js';
import { inputBackground } from '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { ChatModel } from '../../chat/common/chatModel.js';
import { Range } from '../../../../editor/common/core/range.js';
import { ServiceCollection } from '../../../../platform/instantiation/common/serviceCollection.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { EditorOption } from '../../../../editor/common/config/editorOptions.js';
import { ScrollType } from '../../../../editor/common/editorCommon.js';
import { MenuWorkbenchToolBar } from '../../../../platform/actions/browser/toolbar.js';
import { MenuItemAction } from '../../../../platform/actions/common/actions.js';
import { TextOnlyMenuEntryActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
let InlineChatContentWidget = class InlineChatContentWidget {
    constructor(location, _editor, instaService, contextKeyService, configurationService) {
        this._editor = _editor;
        this.suppressMouseDown = false;
        this.allowEditorOverflow = true;
        this._store = ( new DisposableStore());
        this._domNode = document.createElement('div');
        this._inputContainer = document.createElement('div');
        this._toolbarContainer = document.createElement('div');
        this._onDidBlur = this._store.add(( new Emitter()));
        this.onDidBlur = this._onDidBlur.event;
        this._visible = false;
        this._focusNext = false;
        this._defaultChatModel = this._store.add(instaService.createInstance(ChatModel, undefined, ChatAgentLocation.Editor));
        const scopedInstaService = instaService.createChild(( new ServiceCollection([
            IContextKeyService,
            this._store.add(contextKeyService.createScoped(this._domNode))
        ])), this._store);
        this._widget = scopedInstaService.createInstance(ChatWidget, location, { resource: true }, {
            defaultElementHeight: 32,
            editorOverflowWidgetsDomNode: _editor.getOverflowWidgetsDomNode(),
            renderStyle: 'minimal',
            renderInputOnTop: true,
            renderFollowups: true,
            supportsFileReferences: false,
            menus: {
                telemetrySource: 'inlineChat-content',
                executeToolbar: MENU_INLINE_CHAT_EXECUTE,
            },
            filter: _item => false
        }, {
            listForeground: editorForeground,
            listBackground: inlineChatBackground,
            inputEditorBackground: inputBackground,
            resultEditorBackground: editorBackground
        });
        this._store.add(this._widget);
        this._widget.render(this._inputContainer);
        this._widget.setModel(this._defaultChatModel, {});
        this._store.add(this._widget.onDidChangeContentHeight(() => _editor.layoutContentWidget(this)));
        this._domNode.tabIndex = -1;
        this._domNode.className = 'inline-chat-content-widget interactive-session';
        this._domNode.appendChild(this._inputContainer);
        this._toolbarContainer.classList.add('toolbar');
        if (configurationService.getValue(InlineChatConfigKeys.ExpTextButtons)) {
            this._toolbarContainer.style.display = 'inherit';
            this._domNode.style.paddingBottom = '4px';
        }
        this._domNode.appendChild(this._toolbarContainer);
        const toolbar = this._store.add(scopedInstaService.createInstance(MenuWorkbenchToolBar, this._toolbarContainer, MENU_INLINE_CHAT_CONTENT_STATUS, {
            actionViewItemProvider: action => action instanceof MenuItemAction ? instaService.createInstance(TextOnlyMenuEntryActionViewItem, action, { conversational: true }) : undefined,
            toolbarOptions: { primaryGroup: '0_main' },
            icon: false,
            label: true,
        }));
        this._store.add(toolbar.onDidChangeMenuItems(() => {
            this._domNode.classList.toggle('contents', toolbar.getItemsLength() > 1);
        }));
        const tracker = trackFocus(this._domNode);
        this._store.add(tracker.onDidBlur(() => {
            if (this._visible && this._widget.inputEditor.getModel()?.getValueLength() === 0) {
                this._onDidBlur.fire();
            }
        }));
        this._store.add(tracker);
    }
    dispose() {
        this._store.dispose();
    }
    getId() {
        return 'inline-chat-content-widget';
    }
    getDomNode() {
        return this._domNode;
    }
    getPosition() {
        if (!this._position) {
            return null;
        }
        return {
            position: this._position,
            preference: [ContentWidgetPositionPreference.ABOVE]
        };
    }
    beforeRender() {
        const maxHeight = this._widget.input.inputEditor.getOption(EditorOption.lineHeight) * 5;
        const inputEditorHeight = this._widget.contentHeight;
        this._widget.layout(Math.min(maxHeight, inputEditorHeight), 390);
        return null;
    }
    afterRender() {
        if (this._focusNext) {
            this._focusNext = false;
            this._widget.focusInput();
        }
    }
    get chatWidget() {
        return this._widget;
    }
    get isVisible() {
        return this._visible;
    }
    get value() {
        return this._widget.inputEditor.getValue();
    }
    show(position) {
        if (!this._visible) {
            this._visible = true;
            this._focusNext = true;
            this._editor.revealRangeNearTopIfOutsideViewport(Range.fromPositions(position), ScrollType.Immediate);
            const wordInfo = this._editor.getModel()?.getWordAtPosition(position);
            this._position = wordInfo ? ( new Position(position.lineNumber, wordInfo.startColumn)) : position;
            this._editor.addContentWidget(this);
            this._widget.setVisible(true);
        }
    }
    hide() {
        if (this._visible) {
            this._visible = false;
            this._editor.removeContentWidget(this);
            this._widget.inputEditor.setValue('');
            this._widget.saveState();
            this._widget.setVisible(false);
        }
    }
    setSession(session) {
        this._widget.setModel(session.chatModel, {});
        this._widget.setInputPlaceholder(session.agent.description ?? '');
    }
};
InlineChatContentWidget = ( __decorate([
    ( __param(2, IInstantiationService)),
    ( __param(3, IContextKeyService)),
    ( __param(4, IConfigurationService))
], InlineChatContentWidget));
export { InlineChatContentWidget };
