import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import '../../../../base/common/observableInternal/derived.js';
import { autorunWithStore } from '../../../../base/common/observableInternal/autorun.js';
import { observableFromEvent } from '../../../../base/common/observableInternal/utils.js';
import '../../../../base/common/cancellation.js';
import { registerDiffEditorContribution } from '../../../../editor/browser/editorExtensions.js';
import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js';
import { AccessibleDiffViewerNext, AccessibleDiffViewerPrev } from '../../../../editor/browser/widget/diffEditor/commands.js';
import { DiffEditorWidget } from '../../../../editor/browser/widget/diffEditor/diffEditorWidget.js';
import { EmbeddedDiffEditorWidget } from '../../../../editor/browser/widget/diffEditor/embeddedDiffEditorWidget.js';
import { localizeWithPath } from '../../../../nls.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyEqualsExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import '../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { Registry } from '../../../../platform/registry/common/platform.js';
import { FloatingEditorClickWidget } from '../../../browser/codeeditor.js';
import { Extensions } from '../../../common/configuration.js';
import { IAccessibleViewService } from '../../accessibility/browser/accessibleView.service.js';
import { AccessibilityHelpAction } from '../../accessibility/browser/accessibleViewActions.js';
import { getCommentCommandInfo } from '../../accessibility/browser/editorAccessibilityHelp.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import Severity$1 from '../../../../base/common/severity.js';
const _moduleId = "vs/workbench/contrib/codeEditor/browser/diffEditorHelper";
let DiffEditorHelperContribution = class DiffEditorHelperContribution extends Disposable {
    static { this.ID = 'editor.contrib.diffEditorHelper'; }
    constructor(_diffEditor, _instantiationService, _configurationService, _notificationService) {
        super();
        this._diffEditor = _diffEditor;
        this._instantiationService = _instantiationService;
        this._configurationService = _configurationService;
        this._notificationService = _notificationService;
        this._register(createScreenReaderHelp());
        const isEmbeddedDiffEditor = this._diffEditor instanceof EmbeddedDiffEditorWidget;
        if (!isEmbeddedDiffEditor) {
            const computationResult = observableFromEvent(e => this._diffEditor.onDidUpdateDiff(e), () =>  this._diffEditor.getDiffComputationResult());
            const onlyWhiteSpaceChange = ( (computationResult.map(r => r && !r.identical && r.changes2.length === 0)));
            this._register(autorunWithStore((reader, store) => {
                if (onlyWhiteSpaceChange.read(reader)) {
                    const helperWidget = store.add(this._instantiationService.createInstance(FloatingEditorClickWidget, this._diffEditor.getModifiedEditor(), ( localizeWithPath(_moduleId, 0, "Show Whitespace Differences")), null));
                    store.add(helperWidget.onClick(() => {
                        this._configurationService.updateValue('diffEditor.ignoreTrimWhitespace', false);
                    }));
                    helperWidget.render();
                }
            }));
            this._register(this._diffEditor.onDidUpdateDiff(() => {
                const diffComputationResult = this._diffEditor.getDiffComputationResult();
                if (diffComputationResult && diffComputationResult.quitEarly) {
                    this._notificationService.prompt(Severity$1.Warning, ( localizeWithPath(
                        _moduleId,
                        1,
                        "The diff algorithm was stopped early (after {0} ms.)",
                        this._diffEditor.maxComputationTime
                    )), [{
                            label: ( localizeWithPath(_moduleId, 2, "Remove Limit")),
                            run: () => {
                                this._configurationService.updateValue('diffEditor.maxComputationTime', 0);
                            }
                        }], {});
                }
            }));
        }
    }
};
DiffEditorHelperContribution = ( (__decorate([
    ( (__param(1, IInstantiationService))),
    ( (__param(2, IConfigurationService))),
    ( (__param(3, INotificationService)))
], DiffEditorHelperContribution)));
function createScreenReaderHelp() {
    return AccessibilityHelpAction.addImplementation(105, 'diff-editor', async (accessor) => {
        const accessibleViewService = accessor.get(IAccessibleViewService);
        const editorService = accessor.get(IEditorService);
        const codeEditorService = accessor.get(ICodeEditorService);
        const keybindingService = accessor.get(IKeybindingService);
        const contextKeyService = accessor.get(IContextKeyService);
        if (!(editorService.activeTextEditorControl instanceof DiffEditorWidget)) {
            return;
        }
        const codeEditor = codeEditorService.getActiveCodeEditor() || codeEditorService.getFocusedCodeEditor();
        if (!codeEditor) {
            return;
        }
        const next = keybindingService.lookupKeybinding(AccessibleDiffViewerNext.id)?.getAriaLabel();
        const previous = keybindingService.lookupKeybinding(AccessibleDiffViewerPrev.id)?.getAriaLabel();
        let switchSides;
        const switchSidesKb = keybindingService.lookupKeybinding('diffEditor.switchSide')?.getAriaLabel();
        if (switchSidesKb) {
            switchSides = ( localizeWithPath(
                _moduleId,
                3,
                "Run the command Diff Editor: Switch Side ({0}) to toggle between the original and modified editors.",
                switchSidesKb
            ));
        }
        else {
            switchSides = ( localizeWithPath(
                _moduleId,
                4,
                "Run the command Diff Editor: Switch Side, which is currently not triggerable via keybinding, to toggle between the original and modified editors."
            ));
        }
        const diffEditorActiveAnnouncement = ( localizeWithPath(
            _moduleId,
            5,
            "The setting, accessibility.verbosity.diffEditorActive, controls if a diff editor announcement is made when it becomes the active editor."
        ));
        const keys = ['accessibility.signals.diffLineDeleted', 'accessibility.signals.diffLineInserted', 'accessibility.signals.diffLineModified'];
        const content = [
            ( localizeWithPath(_moduleId, 6, "You are in a diff editor.")),
            ( localizeWithPath(
                _moduleId,
                7,
                "View the next ({0}) or previous ({1}) diff in diff review mode, which is optimized for screen readers.",
                next,
                previous
            )),
            switchSides,
            diffEditorActiveAnnouncement,
            ( localizeWithPath(
                _moduleId,
                8,
                "To control which accessibility signals should be played, the following settings can be configured: {0}.",
                keys.join(', ')
            )),
        ];
        const commentCommandInfo = getCommentCommandInfo(keybindingService, contextKeyService, codeEditor);
        if (commentCommandInfo) {
            content.push(commentCommandInfo);
        }
        accessibleViewService.show({
            id: "diffEditor" ,
            verbositySettingKey: "accessibility.verbosity.diffEditor" ,
            provideContent: () => content.join('\n\n'),
            onClose: () => {
                codeEditor.focus();
            },
            options: { type: "help"  }
        });
    }, ContextKeyEqualsExpr.create('isInDiffEditor', true));
}
registerDiffEditorContribution(DiffEditorHelperContribution.ID, DiffEditorHelperContribution);
( (Registry.as(Extensions.ConfigurationMigration)))
    .registerConfigurationMigrations([{
        key: 'diffEditor.experimental.collapseUnchangedRegions',
        migrateFn: (value, accessor) => {
            return [
                ['diffEditor.hideUnchangedRegions.enabled', { value }],
                ['diffEditor.experimental.collapseUnchangedRegions', { value: undefined }]
            ];
        }
    }]);
