
import { getActiveElement } from '../../../../base/browser/dom.js';
import { UndoCommand, RedoCommand, SelectAllCommand } from '../../../../editor/browser/editorExtensions.js';
import { CopyAction, PasteAction, CutAction } from '../../../../editor/contrib/clipboard/browser/clipboard.js';
import { localize } from '../../../../nls.js';
import { MenuRegistry, MenuId } from '../../../../platform/actions/common/actions.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { AccessibleViewRegistry } from '../../../../platform/accessibility/browser/accessibleViewRegistry.js';
import { IWebviewService } from './webview.service.js';
import { WebviewInput } from '../../webviewPanel/browser/webviewEditorInput.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { WebviewFindAccessibilityHelp } from './webviewFindAccessibilityHelp.js';

const PRIORITY = 100;
function overrideCommandForWebview(command, f) {
    command?.addImplementation(PRIORITY, "webview", accessor => {
        const webviewService = accessor.get(IWebviewService);
        const webview = webviewService.activeWebview;
        if (webview?.isFocused) {
            f(webview);
            return true;
        }
        if (getActiveElement()?.classList.contains("action-menu-item")) {
            const editorService = accessor.get(IEditorService);
            if (editorService.activeEditor instanceof WebviewInput) {
                f(editorService.activeEditor.webview);
                return true;
            }
        }
        return false;
    });
}
overrideCommandForWebview(UndoCommand, webview => webview.undo());
overrideCommandForWebview(RedoCommand, webview => webview.redo());
overrideCommandForWebview(SelectAllCommand, webview => webview.selectAll());
overrideCommandForWebview(CopyAction, webview => webview.copy());
overrideCommandForWebview(PasteAction, webview => webview.paste());
overrideCommandForWebview(CutAction, webview => webview.cut());
const PreventDefaultContextMenuItemsContextKeyName = "preventDefaultContextMenuItems";
if (CutAction) {
    MenuRegistry.appendMenuItem(MenuId.WebviewContext, {
        command: {
            id: CutAction.id,
            title: ( localize(15983, "Cut"))
        },
        group: "5_cutcopypaste",
        order: 1,
        when: ContextKeyExpr.not(PreventDefaultContextMenuItemsContextKeyName)
    });
}
if (CopyAction) {
    MenuRegistry.appendMenuItem(MenuId.WebviewContext, {
        command: {
            id: CopyAction.id,
            title: ( localize(15984, "Copy"))
        },
        group: "5_cutcopypaste",
        order: 2,
        when: ContextKeyExpr.not(PreventDefaultContextMenuItemsContextKeyName)
    });
}
if (PasteAction) {
    MenuRegistry.appendMenuItem(MenuId.WebviewContext, {
        command: {
            id: PasteAction.id,
            title: ( localize(15985, "Paste"))
        },
        group: "5_cutcopypaste",
        order: 3,
        when: ContextKeyExpr.not(PreventDefaultContextMenuItemsContextKeyName)
    });
}
AccessibleViewRegistry.register(( new WebviewFindAccessibilityHelp()));

export { PreventDefaultContextMenuItemsContextKeyName };
