import { isFirefox } from '../../../../base/browser/browser.js';
import { getActiveDocument } from '../../../../base/browser/dom.js';
import { KeyMod as KeyMod$1, KeyCode } from '../../../../base/common/keyCodes.js';
import { isNative, isWeb } from '../../../../base/common/platform.js';
import { InMemoryClipboardMetadataManager, CopyOptions } from '../../../browser/controller/textAreaInput.js';
import { registerEditorAction, MultiCommand, EditorAction } from '../../../browser/editorExtensions.js';
import { ICodeEditorService } from '../../../browser/services/codeEditorService.js';
import { EditorOption } from '../../../common/config/editorOptions.js';
import { Handler } from '../../../common/editorCommon.js';
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
import { CopyPasteController } from '../../dropOrPasteInto/browser/copyPasteController.js';
import { localize2WithPath, localizeWithPath } from '../../../../nls.js';
import { MenuRegistry, MenuId } from '../../../../platform/actions/common/actions.js';
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.service.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
const _moduleId = "vs/editor/contrib/clipboard/browser/clipboard";
const CLIPBOARD_CONTEXT_MENU_GROUP = '9_cutcopypaste';
const supportsCut = (isNative || document.queryCommandSupported('cut'));
const supportsCopy = (isNative || document.queryCommandSupported('copy'));
const supportsPaste = (typeof navigator.clipboard === 'undefined' || isFirefox) ? document.queryCommandSupported('paste') : true;
function registerCommand(command) {
    command.register();
    return command;
}
const CutAction = supportsCut ? registerCommand(( (new MultiCommand({
    id: 'editor.action.clipboardCutAction',
    precondition: undefined,
    kbOpts: (
    (isNative ? {
        primary: KeyMod$1.CtrlCmd | KeyCode.KeyX,
        win: { primary: KeyMod$1.CtrlCmd | KeyCode.KeyX, secondary: [KeyMod$1.Shift | KeyCode.Delete] },
        weight: KeybindingWeight.EditorContrib
    } : undefined)),
    menuOpts: [{
            menuId: MenuId.MenubarEditMenu,
            group: '2_ccp',
            title: ( localizeWithPath(_moduleId, 0, "Cu&&t")),
            order: 1
        }, {
            menuId: MenuId.EditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 1, "Cut")),
            when: EditorContextKeys.writable,
            order: 1,
        }, {
            menuId: MenuId.CommandPalette,
            group: '',
            title: ( localizeWithPath(_moduleId, 1, "Cut")),
            order: 1
        }, {
            menuId: MenuId.SimpleEditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 1, "Cut")),
            when: EditorContextKeys.writable,
            order: 1,
        }]
})))) : undefined;
const CopyAction = supportsCopy ? registerCommand(( (new MultiCommand({
    id: 'editor.action.clipboardCopyAction',
    precondition: undefined,
    kbOpts: (
    (isNative ? {
        primary: KeyMod$1.CtrlCmd | KeyCode.KeyC,
        win: { primary: KeyMod$1.CtrlCmd | KeyCode.KeyC, secondary: [KeyMod$1.CtrlCmd | KeyCode.Insert] },
        weight: KeybindingWeight.EditorContrib
    } : undefined)),
    menuOpts: [{
            menuId: MenuId.MenubarEditMenu,
            group: '2_ccp',
            title: ( localizeWithPath(_moduleId, 2, "&&Copy")),
            order: 2
        }, {
            menuId: MenuId.EditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 3, "Copy")),
            order: 2,
        }, {
            menuId: MenuId.CommandPalette,
            group: '',
            title: ( localizeWithPath(_moduleId, 3, "Copy")),
            order: 1
        }, {
            menuId: MenuId.SimpleEditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 3, "Copy")),
            order: 2,
        }]
})))) : undefined;
MenuRegistry.appendMenuItem(MenuId.MenubarEditMenu, { submenu: MenuId.MenubarCopy, title: ( localize2WithPath(_moduleId, 4, "Copy As")), group: '2_ccp', order: 3 });
MenuRegistry.appendMenuItem(MenuId.EditorContext, { submenu: MenuId.EditorContextCopy, title: ( localize2WithPath(_moduleId, 4, "Copy As")), group: CLIPBOARD_CONTEXT_MENU_GROUP, order: 3 });
MenuRegistry.appendMenuItem(MenuId.EditorContext, { submenu: MenuId.EditorContextShare, title: ( localize2WithPath(_moduleId, 5, "Share")), group: '11_share', order: -1, when: ( (ContextKeyExpr.and(
     (ContextKeyExpr.notEquals('resourceScheme', 'output')),
    EditorContextKeys.editorTextFocus
))) });
MenuRegistry.appendMenuItem(MenuId.ExplorerContext, { submenu: MenuId.ExplorerContextShare, title: ( localize2WithPath(_moduleId, 5, "Share")), group: '11_share', order: -1 });
const PasteAction = supportsPaste ? registerCommand(( (new MultiCommand({
    id: 'editor.action.clipboardPasteAction',
    precondition: undefined,
    kbOpts: (
    (isNative ? {
        primary: KeyMod$1.CtrlCmd | KeyCode.KeyV,
        win: { primary: KeyMod$1.CtrlCmd | KeyCode.KeyV, secondary: [KeyMod$1.Shift | KeyCode.Insert] },
        linux: { primary: KeyMod$1.CtrlCmd | KeyCode.KeyV, secondary: [KeyMod$1.Shift | KeyCode.Insert] },
        weight: KeybindingWeight.EditorContrib
    } : undefined)),
    menuOpts: [{
            menuId: MenuId.MenubarEditMenu,
            group: '2_ccp',
            title: ( localizeWithPath(_moduleId, 6, "&&Paste")),
            order: 4
        }, {
            menuId: MenuId.EditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 7, "Paste")),
            when: EditorContextKeys.writable,
            order: 4,
        }, {
            menuId: MenuId.CommandPalette,
            group: '',
            title: ( localizeWithPath(_moduleId, 7, "Paste")),
            order: 1
        }, {
            menuId: MenuId.SimpleEditorContext,
            group: CLIPBOARD_CONTEXT_MENU_GROUP,
            title: ( localizeWithPath(_moduleId, 7, "Paste")),
            when: EditorContextKeys.writable,
            order: 4,
        }]
})))) : undefined;
class ExecCommandCopyWithSyntaxHighlightingAction extends EditorAction {
    constructor() {
        super({
            id: 'editor.action.clipboardCopyWithSyntaxHighlightingAction',
            label: ( localizeWithPath(_moduleId, 8, "Copy With Syntax Highlighting")),
            alias: 'Copy With Syntax Highlighting',
            precondition: undefined,
            kbOpts: {
                kbExpr: EditorContextKeys.textInputFocus,
                primary: 0,
                weight: KeybindingWeight.EditorContrib
            }
        });
    }
    run(accessor, editor) {
        if (!editor.hasModel()) {
            return;
        }
        const emptySelectionClipboard = editor.getOption(EditorOption.emptySelectionClipboard);
        if (!emptySelectionClipboard && editor.getSelection().isEmpty()) {
            return;
        }
        CopyOptions.forceCopyWithSyntaxHighlighting = true;
        editor.focus();
        editor.getContainerDomNode().ownerDocument.execCommand('copy');
        CopyOptions.forceCopyWithSyntaxHighlighting = false;
    }
}
function registerExecCommandImpl(target, browserCommand) {
    if (!target) {
        return;
    }
    target.addImplementation(10000, 'code-editor', (accessor, args) => {
        const focusedEditor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
        if (focusedEditor && focusedEditor.hasTextFocus()) {
            const emptySelectionClipboard = focusedEditor.getOption(EditorOption.emptySelectionClipboard);
            const selection = focusedEditor.getSelection();
            if (selection && selection.isEmpty() && !emptySelectionClipboard) {
                return true;
            }
            focusedEditor.getContainerDomNode().ownerDocument.execCommand(browserCommand);
            return true;
        }
        return false;
    });
    target.addImplementation(0, 'generic-dom', (accessor, args) => {
        getActiveDocument().execCommand(browserCommand);
        return true;
    });
}
registerExecCommandImpl(CutAction, 'cut');
registerExecCommandImpl(CopyAction, 'copy');
if (PasteAction) {
    PasteAction.addImplementation(10000, 'code-editor', (accessor, args) => {
        const codeEditorService = accessor.get(ICodeEditorService);
        const clipboardService = accessor.get(IClipboardService);
        const focusedEditor = codeEditorService.getFocusedCodeEditor();
        if (focusedEditor && focusedEditor.hasTextFocus()) {
            const result = focusedEditor.getContainerDomNode().ownerDocument.execCommand('paste');
            if (result) {
                return CopyPasteController.get(focusedEditor)?.finishedPaste() ?? Promise.resolve();
            }
            else if (isWeb) {
                return (async () => {
                    const clipboardText = await clipboardService.readText();
                    if (clipboardText !== '') {
                        const metadata = InMemoryClipboardMetadataManager.INSTANCE.get(clipboardText);
                        let pasteOnNewLine = false;
                        let multicursorText = null;
                        let mode = null;
                        if (metadata) {
                            pasteOnNewLine = (focusedEditor.getOption(EditorOption.emptySelectionClipboard) && !!metadata.isFromEmptySelection);
                            multicursorText = (typeof metadata.multicursorText !== 'undefined' ? metadata.multicursorText : null);
                            mode = metadata.mode;
                        }
                        focusedEditor.trigger('keyboard', Handler.Paste, {
                            text: clipboardText,
                            pasteOnNewLine,
                            multicursorText,
                            mode
                        });
                    }
                })();
            }
            return true;
        }
        return false;
    });
    PasteAction.addImplementation(0, 'generic-dom', (accessor, args) => {
        getActiveDocument().execCommand('paste');
        return true;
    });
}
if (supportsCopy) {
    registerEditorAction(ExecCommandCopyWithSyntaxHighlightingAction);
}
export { CopyAction, CutAction, PasteAction };
