/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import * as dom from '../../../../base/browser/dom.js';
import { Button } from '../../../../base/browser/ui/button/button.js';
import { toAction } from '../../../../base/common/actions.js';
import { Disposable, MutableDisposable, toDisposable } from '../../../../base/common/lifecycle.js';
import './postDropWidget.css';
import { localize } from '../../../../nls.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { defaultButtonStyles } from '../../../../platform/theme/browser/defaultStyles.js';
let PostDropWidget = class PostDropWidget extends Disposable {
    constructor(editor, range, edits, onSelectNewEdit, _contextMenuService) {
        super();
        this.editor = editor;
        this.range = range;
        this.edits = edits;
        this.onSelectNewEdit = onSelectNewEdit;
        this._contextMenuService = _contextMenuService;
        this.allowEditorOverflow = false;
        this.suppressMouseDown = true;
        this.create();
        this.editor.addContentWidget(this);
        this.editor.layoutContentWidget(this);
        this._register(toDisposable((() => this.editor.removeContentWidget(this))));
        this._register(this.editor.onDidChangeCursorPosition(e => {
            if (!range.containsPosition(e.position)) {
                this.dispose();
            }
        }));
    }
    create() {
        this.domNode = dom.$('.post-drop-widget');
        const button = this._register(new Button(this.domNode, Object.assign({ title: localize('postDropWidgetTile', "Drop options..."), supportIcons: true }, defaultButtonStyles)));
        button.label = '$(clippy)';
        this._register(dom.addDisposableListener(this.domNode, dom.EventType.CLICK, e => {
            this._contextMenuService.showContextMenu({
                getAnchor: () => {
                    const pos = dom.getDomNodePagePosition(button.element);
                    return { x: pos.left + pos.width, y: pos.top + pos.height };
                },
                getActions: () => {
                    return this.edits.allEdits.map((edit, i) => toAction({
                        id: '',
                        label: edit.label,
                        checked: i === this.edits.activeEditIndex,
                        run: () => this.onSelectNewEdit(i),
                    }));
                }
            });
        }));
    }
    getId() {
        return PostDropWidget.ID;
    }
    getDomNode() {
        return this.domNode;
    }
    getPosition() {
        return {
            position: this.range.getEndPosition(),
            preference: [2 /* ContentWidgetPositionPreference.BELOW */]
        };
    }
};
PostDropWidget.ID = 'editor.widget.postDropWidget';
PostDropWidget = __decorate([
    __param(4, IContextMenuService)
], PostDropWidget);
let PostDropWidgetManager = class PostDropWidgetManager extends Disposable {
    constructor(_editor, _instantiationService) {
        super();
        this._editor = _editor;
        this._instantiationService = _instantiationService;
        this._currentWidget = this._register(new MutableDisposable());
        this._register(_editor.onDidChangeModelContent(() => this.clear()));
    }
    show(range, edits, onDidSelectEdit) {
        this.clear();
        if (this._editor.hasModel()) {
            this._currentWidget.value = this._instantiationService.createInstance(PostDropWidget, this._editor, range, edits, onDidSelectEdit);
        }
    }
    clear() {
        var _a;
        (_a = this._currentWidget) === null || _a === void 0 ? void 0 : _a.clear();
    }
};
PostDropWidgetManager = __decorate([
    __param(1, IInstantiationService)
], PostDropWidgetManager);
export { PostDropWidgetManager };
