import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { h, Dimension } from '../../../../base/browser/dom.js';
import { MutableDisposable, DisposableStore } from '../../../../base/common/lifecycle.js';
import { EmbeddedCodeEditorWidget } from '../../../../editor/browser/widget/codeEditor/embeddedCodeEditorWidget.js';
import { ZoneWidget } from '../../../../editor/contrib/zoneWidget/browser/zoneWidget.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { asCssVariable, asCssVariableName } from '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import { editorBackground } from '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../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 { editorGutter } from '../../../../editor/common/core/editorColorRegistry.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { INLINE_CHAT_ID, inlineChatRegionHighlight } from '../common/inlineChat.js';
import { EditorExtensionsRegistry } from '../../../../editor/browser/editorExtensions.js';
import { ResourceLabel } from '../../../browser/labels.js';
import { FileKind } from '../../../../platform/files/common/files.js';
import { ITextModelService } from '../../../../editor/common/services/resolverService.js';
import { ButtonBar } from '../../../../base/browser/ui/button/button.js';
import { defaultButtonStyles } from '../../../../platform/theme/browser/defaultStyles.js';
import { SideBySideEditor } from '../../../common/editor.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { toAction } from '../../../../base/common/actions.js';
import { renderIcon } from '../../../../base/browser/ui/iconLabel/iconLabels.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { TAB_ACTIVE_MODIFIED_BORDER } from '../../../common/theme.js';
import { localizeWithPath } from '../../../../nls.js';
import { Event } from '../../../../base/common/event.js';
var InlineChatFileCreatePreviewWidget_1;
const _moduleId = "vs/workbench/contrib/inlineChat/browser/inlineChatFileCreationWidget";
let InlineChatFileCreatePreviewWidget = class InlineChatFileCreatePreviewWidget extends ZoneWidget {
    static { InlineChatFileCreatePreviewWidget_1 = this; }
    static { this.TitleHeight = 35; }
    constructor(parentEditor, instaService, themeService, _textModelResolverService, _editorService) {
        super(parentEditor, {
            showArrow: false,
            showFrame: true,
            frameColor: asCssVariable(TAB_ACTIVE_MODIFIED_BORDER),
            frameWidth: 1,
            isResizeable: true,
            isAccessible: true,
            showInHiddenAreas: true,
            ordinal: 10000 + 2
        });
        this._textModelResolverService = _textModelResolverService;
        this._editorService = _editorService;
        this._elements = h('div.inline-chat-newfile-widget@domNode', [
            h('div.title@title', [
                h('span.name.show-file-icons@name'),
                h('span.detail@detail'),
            ]),
            h('div.editor@editor'),
        ]);
        this._previewStore = ( (new MutableDisposable()));
        super.create();
        this._name = instaService.createInstance(ResourceLabel, this._elements.name, { supportIcons: true });
        this._elements.detail.appendChild(renderIcon(Codicon.circleFilled));
        const contributions = EditorExtensionsRegistry
            .getEditorContributions()
            .filter(c => c.id !== INLINE_CHAT_ID);
        this._previewEditor = instaService.createInstance(EmbeddedCodeEditorWidget, this._elements.editor, {
            scrollBeyondLastLine: false,
            stickyScroll: { enabled: false },
            minimap: { enabled: false },
            scrollbar: { alwaysConsumeMouseWheel: false, useShadows: true, ignoreHorizontalScrollbarInContentHeight: true, },
        }, { isSimpleWidget: true, contributions }, parentEditor);
        const doStyle = () => {
            const theme = themeService.getColorTheme();
            const overrides = [
                [editorBackground, inlineChatRegionHighlight],
                [editorGutter, inlineChatRegionHighlight],
            ];
            for (const [target, source] of overrides) {
                const value = theme.getColor(source);
                if (value) {
                    this._elements.domNode.style.setProperty(asCssVariableName(target), String(value));
                }
            }
        };
        doStyle();
        this._disposables.add(themeService.onDidColorThemeChange(doStyle));
        this._buttonBar = instaService.createInstance(ButtonBarWidget);
        this._elements.title.appendChild(this._buttonBar.domNode);
    }
    dispose() {
        this._name.dispose();
        this._buttonBar.dispose();
        this._previewEditor.dispose();
        this._previewStore.dispose();
        super.dispose();
    }
    _fillContainer(container) {
        container.appendChild(this._elements.domNode);
    }
    show() {
        throw ( (new Error('Use showFileCreation')));
    }
    async showCreation(where, untitledTextModel) {
        const store = ( (new DisposableStore()));
        this._previewStore.value = store;
        this._name.element.setFile(untitledTextModel.resource, {
            fileKind: FileKind.FILE,
            fileDecorations: { badges: true, colors: true }
        });
        const actionSave = toAction({
            id: '1',
            label: ( localizeWithPath(_moduleId, 0, "Create")),
            run: () => untitledTextModel.save({ reason: 1  })
        });
        const actionSaveAs = toAction({
            id: '2',
            label: ( localizeWithPath(_moduleId, 1, "Create As")),
            run: async () => {
                const ids = this._editorService.findEditors(untitledTextModel.resource, { supportSideBySide: SideBySideEditor.ANY });
                await this._editorService.save(ids.slice(), { saveAs: true, reason: 1  });
            }
        });
        this._buttonBar.update([
            [actionSave, actionSaveAs],
            [(toAction({ id: '3', label: ( localizeWithPath(_moduleId, 2, "Discard")), run: () => untitledTextModel.revert() }))]
        ]);
        store.add(Event.any(untitledTextModel.onDidRevert, untitledTextModel.onDidSave, untitledTextModel.onDidChangeDirty, untitledTextModel.onWillDispose)(() => this.hide()));
        await untitledTextModel.resolve();
        const ref = await this._textModelResolverService.createModelReference(untitledTextModel.resource);
        store.add(ref);
        const model = ref.object.textEditorModel;
        this._previewEditor.setModel(model);
        const lineHeight = this.editor.getOption(67 );
        this._elements.title.style.height = `${InlineChatFileCreatePreviewWidget_1.TitleHeight}px`;
        const titleHightInLines = InlineChatFileCreatePreviewWidget_1.TitleHeight / lineHeight;
        const maxLines = Math.max(4, Math.floor((this.editor.getLayoutInfo().height / lineHeight) * .33));
        const lines = Math.min(maxLines, model.getLineCount());
        super.show(where, titleHightInLines + lines);
    }
    hide() {
        this._previewStore.clear();
        super.hide();
    }
    revealRange(range, isLastLine) {
    }
    _onWidth(widthInPixel) {
        if (this._dim) {
            this._doLayout(this._dim.height, widthInPixel);
        }
    }
    _doLayout(heightInPixel, widthInPixel) {
        const { lineNumbersLeft } = this.editor.getLayoutInfo();
        this._elements.title.style.marginLeft = `${lineNumbersLeft}px`;
        const newDim = ( (new Dimension(widthInPixel, heightInPixel)));
        if (!Dimension.equals(this._dim, newDim)) {
            this._dim = newDim;
            this._previewEditor.layout(this._dim.with(undefined, this._dim.height - InlineChatFileCreatePreviewWidget_1.TitleHeight));
        }
    }
};
InlineChatFileCreatePreviewWidget = InlineChatFileCreatePreviewWidget_1 = ( (__decorate([
    ( (__param(1, IInstantiationService))),
    ( (__param(2, IThemeService))),
    ( (__param(3, ITextModelService))),
    ( (__param(4, IEditorService)))
], InlineChatFileCreatePreviewWidget)));
let ButtonBarWidget = class ButtonBarWidget {
    constructor(_contextMenuService) {
        this._contextMenuService = _contextMenuService;
        this._domNode = h('div.buttonbar-widget');
        this._store = ( (new DisposableStore()));
        this._buttonBar = ( (new ButtonBar(this.domNode)));
    }
    update(allActions) {
        this._buttonBar.clear();
        let secondary = false;
        for (const actions of allActions) {
            let btn;
            const [first, ...rest] = actions;
            if (!first) {
                continue;
            }
            else if (rest.length === 0) {
                btn = this._buttonBar.addButton({ ...defaultButtonStyles, secondary });
            }
            else {
                btn = this._buttonBar.addButtonWithDropdown({
                    ...defaultButtonStyles,
                    addPrimaryActionToDropdown: false,
                    actions: rest,
                    contextMenuProvider: this._contextMenuService
                });
            }
            btn.label = first.label;
            this._store.add(btn.onDidClick(() => first.run()));
            secondary = true;
        }
    }
    dispose() {
        this._buttonBar.dispose();
        this._store.dispose();
    }
    get domNode() {
        return this._domNode.root;
    }
};
ButtonBarWidget = ( (__decorate([
    ( (__param(0, IContextMenuService)))
], ButtonBarWidget)));
export { InlineChatFileCreatePreviewWidget };
