import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import './media/inlineChatContentWidget.css.js';
import { Dimension, reset, 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 { renderLabelWithIcons } from '../../../../base/browser/ui/iconLabel/iconLabels.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { inlineChatBackground } 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';
let InlineChatContentWidget = class InlineChatContentWidget {
    constructor(_editor, instaService, contextKeyService) {
        this._editor = _editor;
        this.suppressMouseDown = false;
        this.allowEditorOverflow = true;
        this._store = ( new DisposableStore());
        this._domNode = document.createElement('div');
        this._inputContainer = document.createElement('div');
        this._messageContainer = 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, `inlineChatDefaultModel/editorContentWidgetPlaceholder`, undefined));
        const scopedInstaService = instaService.createChild(( new ServiceCollection([
            IContextKeyService,
            this._store.add(contextKeyService.createScoped(this._domNode))
        ])));
        this._widget = scopedInstaService.createInstance(ChatWidget, ChatAgentLocation.Editor, { resource: true }, {
            defaultElementHeight: 32,
            editorOverflowWidgetsDomNode: _editor.getOverflowWidgetsDomNode(),
            renderStyle: 'compact',
            renderInputOnTop: true,
            supportsFileReferences: false,
            menus: {
                telemetrySource: 'inlineChat-content'
            },
            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.inputEditor.onDidContentSizeChange(() => _editor.layoutContentWidget(this)));
        this._domNode.tabIndex = -1;
        this._domNode.className = 'inline-chat-content-widget interactive-session';
        this._domNode.appendChild(this._inputContainer);
        this._messageContainer.classList.add('hidden', 'message');
        this._domNode.appendChild(this._messageContainer);
        const tracker = trackFocus(this._domNode);
        this._store.add(tracker.onDidBlur(() => {
            if (this._visible
            ) {
                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: [1 ]
        };
    }
    beforeRender() {
        const maxHeight = this._widget.input.inputEditor.getOption(67 ) * 5;
        const inputEditorHeight = this._widget.inputEditor.getContentHeight();
        this._widget.inputEditor.layout(new Dimension(360, Math.min(maxHeight, inputEditorHeight)));
        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));
            this._widget.inputEditor.setValue('');
            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.saveState();
            this._widget.setVisible(false);
        }
    }
    setSession(session) {
        this._widget.setModel(session.chatModel, {});
        this._widget.setInputPlaceholder(session.session.placeholder ?? '');
        this._updateMessage(session.session.message ?? '');
    }
    _updateMessage(message) {
        this._messageContainer.classList.toggle('hidden', !message);
        const renderedMessage = renderLabelWithIcons(message);
        reset(this._messageContainer, ...renderedMessage);
        this._editor.layoutContentWidget(this);
    }
};
InlineChatContentWidget = ( __decorate([
    ( __param(1, IInstantiationService)),
    ( __param(2, IContextKeyService))
], InlineChatContentWidget));
export { InlineChatContentWidget };
