import { getActiveElement } from '../../../../base/browser/dom.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { EditorAction2 } from '../../editorExtensions.js';
import { ICodeEditorService } from '../../services/codeEditorService.js';
import { DiffEditorWidget } from './diffEditorWidget.js';
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
import { localize2WithPath } from '../../../../nls.js';
import { Action2, MenuId } from '../../../../platform/actions/common/actions.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import './registrations.contribution.js';
class ToggleCollapseUnchangedRegions extends Action2 {
    constructor() {
        super({
            id: 'diffEditor.toggleCollapseUnchangedRegions',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'toggleCollapseUnchangedRegions',
                'Toggle Collapse Unchanged Regions'
            )),
            icon: Codicon.map,
            toggled: ( ContextKeyExpr.has('config.diffEditor.hideUnchangedRegions.enabled')),
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            menu: {
                when: ( ContextKeyExpr.has('isInDiffEditor')),
                id: MenuId.EditorTitle,
                order: 22,
                group: 'navigation',
            },
        });
    }
    run(accessor, ...args) {
        const configurationService = accessor.get(IConfigurationService);
        const newValue = !configurationService.getValue('diffEditor.hideUnchangedRegions.enabled');
        configurationService.updateValue('diffEditor.hideUnchangedRegions.enabled', newValue);
    }
}
class ToggleShowMovedCodeBlocks extends Action2 {
    constructor() {
        super({
            id: 'diffEditor.toggleShowMovedCodeBlocks',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'toggleShowMovedCodeBlocks',
                'Toggle Show Moved Code Blocks'
            )),
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
        });
    }
    run(accessor, ...args) {
        const configurationService = accessor.get(IConfigurationService);
        const newValue = !configurationService.getValue('diffEditor.experimental.showMoves');
        configurationService.updateValue('diffEditor.experimental.showMoves', newValue);
    }
}
class ToggleUseInlineViewWhenSpaceIsLimited extends Action2 {
    constructor() {
        super({
            id: 'diffEditor.toggleUseInlineViewWhenSpaceIsLimited',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'toggleUseInlineViewWhenSpaceIsLimited',
                'Toggle Use Inline View When Space Is Limited'
            )),
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
        });
    }
    run(accessor, ...args) {
        const configurationService = accessor.get(IConfigurationService);
        const newValue = !configurationService.getValue('diffEditor.useInlineViewWhenSpaceIsLimited');
        configurationService.updateValue('diffEditor.useInlineViewWhenSpaceIsLimited', newValue);
    }
}
const diffEditorCategory = ( localize2WithPath(
    'vs/editor/browser/widget/diffEditor/commands',
    'diffEditor',
    "Diff Editor"
));
class SwitchSide extends EditorAction2 {
    constructor() {
        super({
            id: 'diffEditor.switchSide',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'switchSide',
                'Switch Side'
            )),
            icon: Codicon.arrowSwap,
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            f1: true,
            category: diffEditorCategory,
        });
    }
    runEditorCommand(accessor, editor, arg) {
        const diffEditor = findFocusedDiffEditor(accessor);
        if (diffEditor instanceof DiffEditorWidget) {
            if (arg && arg.dryRun) {
                return { destinationSelection: diffEditor.mapToOtherSide().destinationSelection };
            }
            else {
                diffEditor.switchSide();
            }
        }
        return undefined;
    }
}
class ExitCompareMove extends EditorAction2 {
    constructor() {
        super({
            id: 'diffEditor.exitCompareMove',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'exitCompareMove',
                'Exit Compare Move'
            )),
            icon: Codicon.close,
            precondition: EditorContextKeys.comparingMovedCode,
            f1: false,
            category: diffEditorCategory,
            keybinding: {
                weight: 10000,
                primary: 9 ,
            }
        });
    }
    runEditorCommand(accessor, editor, ...args) {
        const diffEditor = findFocusedDiffEditor(accessor);
        if (diffEditor instanceof DiffEditorWidget) {
            diffEditor.exitCompareMove();
        }
    }
}
class CollapseAllUnchangedRegions extends EditorAction2 {
    constructor() {
        super({
            id: 'diffEditor.collapseAllUnchangedRegions',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'collapseAllUnchangedRegions',
                'Collapse All Unchanged Regions'
            )),
            icon: Codicon.fold,
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            f1: true,
            category: diffEditorCategory,
        });
    }
    runEditorCommand(accessor, editor, ...args) {
        const diffEditor = findFocusedDiffEditor(accessor);
        if (diffEditor instanceof DiffEditorWidget) {
            diffEditor.collapseAllUnchangedRegions();
        }
    }
}
class ShowAllUnchangedRegions extends EditorAction2 {
    constructor() {
        super({
            id: 'diffEditor.showAllUnchangedRegions',
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'showAllUnchangedRegions',
                'Show All Unchanged Regions'
            )),
            icon: Codicon.unfold,
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            f1: true,
            category: diffEditorCategory,
        });
    }
    runEditorCommand(accessor, editor, ...args) {
        const diffEditor = findFocusedDiffEditor(accessor);
        if (diffEditor instanceof DiffEditorWidget) {
            diffEditor.showAllUnchangedRegions();
        }
    }
}
class RevertHunkOrSelection extends Action2 {
    constructor() {
        super({
            id: 'diffEditor.revert',
            title: ( localize2WithPath('vs/editor/browser/widget/diffEditor/commands', 'revert', 'Revert')),
            f1: false,
            category: diffEditorCategory,
        });
    }
    run(accessor, arg) {
        const diffEditor = findDiffEditor(accessor, arg.originalUri, arg.modifiedUri);
        if (diffEditor instanceof DiffEditorWidget) {
            diffEditor.revertRangeMappings(arg.mapping.innerChanges ?? []);
        }
        return undefined;
    }
}
const accessibleDiffViewerCategory = ( localize2WithPath(
    'vs/editor/browser/widget/diffEditor/commands',
    'accessibleDiffViewer',
    "Accessible Diff Viewer"
));
class AccessibleDiffViewerNext extends Action2 {
    static { this.id = 'editor.action.accessibleDiffViewer.next'; }
    constructor() {
        super({
            id: AccessibleDiffViewerNext.id,
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'editor.action.accessibleDiffViewer.next',
                'Go to Next Difference'
            )),
            category: accessibleDiffViewerCategory,
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            keybinding: {
                primary: 65 ,
                weight: 100
            },
            f1: true,
        });
    }
    run(accessor) {
        const diffEditor = findFocusedDiffEditor(accessor);
        diffEditor?.accessibleDiffViewerNext();
    }
}
class AccessibleDiffViewerPrev extends Action2 {
    static { this.id = 'editor.action.accessibleDiffViewer.prev'; }
    constructor() {
        super({
            id: AccessibleDiffViewerPrev.id,
            title: ( localize2WithPath(
                'vs/editor/browser/widget/diffEditor/commands',
                'editor.action.accessibleDiffViewer.prev',
                'Go to Previous Difference'
            )),
            category: accessibleDiffViewerCategory,
            precondition: ( ContextKeyExpr.has('isInDiffEditor')),
            keybinding: {
                primary: 1024  | 65 ,
                weight: 100
            },
            f1: true,
        });
    }
    run(accessor) {
        const diffEditor = findFocusedDiffEditor(accessor);
        diffEditor?.accessibleDiffViewerPrev();
    }
}
function findDiffEditor(accessor, originalUri, modifiedUri) {
    const codeEditorService = accessor.get(ICodeEditorService);
    const diffEditors = codeEditorService.listDiffEditors();
    return diffEditors.find(diffEditor => {
        const modified = diffEditor.getModifiedEditor();
        const original = diffEditor.getOriginalEditor();
        return modified && modified.getModel()?.uri.toString() === ( modifiedUri.toString())
            && original && original.getModel()?.uri.toString() === ( originalUri.toString());
    }) || null;
}
function findFocusedDiffEditor(accessor) {
    const codeEditorService = accessor.get(ICodeEditorService);
    const diffEditors = codeEditorService.listDiffEditors();
    const activeElement = getActiveElement();
    if (activeElement) {
        for (const d of diffEditors) {
            const container = d.getContainerDomNode();
            if (isElementOrParentOf(container, activeElement)) {
                return d;
            }
        }
    }
    return null;
}
function isElementOrParentOf(elementOrParent, element) {
    let e = element;
    while (e) {
        if (e === elementOrParent) {
            return true;
        }
        e = e.parentElement;
    }
    return false;
}
export { AccessibleDiffViewerNext, AccessibleDiffViewerPrev, CollapseAllUnchangedRegions, ExitCompareMove, RevertHunkOrSelection, ShowAllUnchangedRegions, SwitchSide, ToggleCollapseUnchangedRegions, ToggleShowMovedCodeBlocks, ToggleUseInlineViewWhenSpaceIsLimited, findDiffEditor, findFocusedDiffEditor };
