import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { Action, Separator } from '../../../base/common/actions.js';
import { localizeWithPath } from '../../../nls.js';
import { IWorkbenchLayoutService } from '../../services/layout/browser/layoutService.service.js';
import { IContextMenuService } from '../../../platform/contextview/browser/contextView.service.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { getActiveDocument, isHTMLTextAreaElement, isHTMLInputElement, addDisposableListener, isHTMLElement, EventHelper, getWindow } from '../../../base/browser/dom.js';
import { registerWorkbenchContribution2, WorkbenchPhase } from '../../common/contributions.js';
import { isNative } from '../../../base/common/platform.js';
import { IClipboardService } from '../../../platform/clipboard/common/clipboardService.service.js';
import { StandardMouseEvent } from '../../../base/browser/mouseEvent.js';
import { Event as Event$1 } from '../../../base/common/event.js';
import { Lazy } from '../../../base/common/lazy.js';
const _moduleId = "vs/workbench/browser/actions/textInputActions";
let TextInputActionsProvider = class TextInputActionsProvider extends Disposable {
    static { this.ID = 'workbench.contrib.textInputActionsProvider'; }
    constructor(layoutService, contextMenuService, clipboardService) {
        super();
        this.layoutService = layoutService;
        this.contextMenuService = contextMenuService;
        this.clipboardService = clipboardService;
        this.textInputActions = ( (new Lazy(() => this.createActions())));
        this.registerListeners();
    }
    createActions() {
        return [
            (
            (new Action('undo', ( localizeWithPath(_moduleId, 0, "Undo")), undefined, true, async () => getActiveDocument().execCommand('undo')))),
            ( (new Action('redo', ( localizeWithPath(_moduleId, 1, "Redo")), undefined, true, async () => getActiveDocument().execCommand('redo')))),
            ( (new Separator())),
            (
            (new Action('editor.action.clipboardCutAction', ( localizeWithPath(_moduleId, 2, "Cut")), undefined, true, async () => getActiveDocument().execCommand('cut')))),
            ( (new Action('editor.action.clipboardCopyAction', ( localizeWithPath(_moduleId, 3, "Copy")), undefined, true, async () => getActiveDocument().execCommand('copy')))),
            ( (new Action('editor.action.clipboardPasteAction', ( localizeWithPath(_moduleId, 4, "Paste")), undefined, true, async (element) => {
            if (isNative) {
                getActiveDocument().execCommand('paste');
            }
            else {
                const clipboardText = await this.clipboardService.readText();
                if (isHTMLTextAreaElement(element) ||
                    isHTMLInputElement(element)) {
                    const selectionStart = element.selectionStart || 0;
                    const selectionEnd = element.selectionEnd || 0;
                    element.value = `${element.value.substring(0, selectionStart)}${clipboardText}${element.value.substring(selectionEnd, element.value.length)}`;
                    element.selectionStart = selectionStart + clipboardText.length;
                    element.selectionEnd = element.selectionStart;
                    element.dispatchEvent(( (new Event('input', { bubbles: true, cancelable: true }))));
                }
            }
        }))),
            ( (new Separator())),
            (
            (new Action('editor.action.selectAll', ( localizeWithPath(_moduleId, 5, "Select All")), undefined, true, async () => getActiveDocument().execCommand('selectAll'))))
        ];
    }
    registerListeners() {
        this._register(Event$1.runAndSubscribe(this.layoutService.onDidAddContainer, ({ container, disposables }) => {
            disposables.add(addDisposableListener(container, 'contextmenu', e => this.onContextMenu(getWindow(container), e)));
        }, { container: this.layoutService.mainContainer, disposables: this._store }));
    }
    onContextMenu(targetWindow, e) {
        if (e.defaultPrevented) {
            return;
        }
        const target = e.target;
        if (!(isHTMLElement(target)) || (target.nodeName.toLowerCase() !== 'input' && target.nodeName.toLowerCase() !== 'textarea')) {
            return;
        }
        EventHelper.stop(e, true);
        const event = ( (new StandardMouseEvent(targetWindow, e)));
        this.contextMenuService.showContextMenu({
            getAnchor: () => event,
            getActions: () => this.textInputActions.value,
            getActionsContext: () => target,
        });
    }
};
TextInputActionsProvider = ( (__decorate([
    ( (__param(0, IWorkbenchLayoutService))),
    ( (__param(1, IContextMenuService))),
    ( (__param(2, IClipboardService)))
], TextInputActionsProvider)));
registerWorkbenchContribution2(TextInputActionsProvider.ID, TextInputActionsProvider, WorkbenchPhase.BlockRestore
);
export { TextInputActionsProvider };
