
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import '../../../../base/common/observableInternal/index.js';
import { localize } from '../../../../nls.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { AccessibilityVerbositySettingId } from '../../accessibility/browser/accessibilityConfiguration.js';
import { AccessibilityCommandId } from '../../accessibility/common/accessibilityCommands.js';
import { CellKind, NotebookCellExecutionState } from '../common/notebookCommon.js';
import { NotebookExecutionType } from '../common/notebookExecutionStateService.js';
import { INotebookExecutionStateService } from '../common/notebookExecutionStateService.service.js';
import { getAllOutputsText } from './viewModel/cellOutputTextHelper.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.service.js';
import { alert } from '../../../../base/browser/ui/aria/aria.js';
import { observableFromEvent } from '../../../../base/common/observableInternal/observables/observableFromEvent.js';

let NotebookAccessibilityProvider = class NotebookAccessibilityProvider extends Disposable {
    constructor(
        viewModel,
        isReplHistory,
        notebookExecutionStateService,
        keybindingService,
        configurationService,
        accessibilityService
    ) {
        super();
        this.viewModel = viewModel;
        this.isReplHistory = isReplHistory;
        this.notebookExecutionStateService = notebookExecutionStateService;
        this.keybindingService = keybindingService;
        this.configurationService = configurationService;
        this.accessibilityService = accessibilityService;
        this._onDidAriaLabelChange = this._register(( new Emitter()));
        this.onDidAriaLabelChange = this._onDidAriaLabelChange.event;
        this._register(Event.debounce(
            this.notebookExecutionStateService.onDidChangeExecution,
            (last, e) => this.mergeEvents(last, e),
            100
        )(updates => {
            if (!updates.length) {
                return;
            }
            const viewModel = this.viewModel();
            if (viewModel) {
                for (const update of updates) {
                    const cellModel = viewModel.getCellByHandle(update.cellHandle);
                    if (cellModel) {
                        this._onDidAriaLabelChange.fire(cellModel);
                    }
                }
                const lastUpdate = updates[updates.length - 1];
                if (this.shouldReadCellOutputs(lastUpdate.state)) {
                    const cell = viewModel.getCellByHandle(lastUpdate.cellHandle);
                    if (cell && cell.outputsViewModels.length) {
                        const text = getAllOutputsText(viewModel.notebookDocument, cell, true);
                        alert(text);
                    }
                }
            }
        }, this));
    }
    shouldReadCellOutputs(state) {
        return (
            state === undefined && this.isReplHistory && this.accessibilityService.isScreenReaderOptimized() && this.configurationService.getValue("accessibility.replEditor.readLastExecutionOutput")
        );
    }
    get verbositySettingId() {
        return this.isReplHistory ? AccessibilityVerbositySettingId.ReplEditor : AccessibilityVerbositySettingId.Notebook;
    }
    getAriaLabel(element) {
        const event = Event.filter(this.onDidAriaLabelChange, e => e === element);
        return observableFromEvent(this, event, () => {
            const viewModel = this.viewModel();
            if (!viewModel) {
                return "";
            }
            const index = viewModel.getCellIndex(element);
            if (index >= 0) {
                return this.getLabel(element);
            }
            return "";
        });
    }
    createItemLabel(executionLabel, cellKind) {
        return this.isReplHistory ? `cell${executionLabel}` : `${cellKind === CellKind.Markup ? "markdown" : "code"} cell${executionLabel}`;
    }
    getLabel(element) {
        const executionState = this.notebookExecutionStateService.getCellExecution(element.uri)?.state;
        const executionLabel = executionState === NotebookCellExecutionState.Executing ? ", executing" : executionState === NotebookCellExecutionState.Pending ? ", pending" : "";
        return this.createItemLabel(executionLabel, element.cellKind);
    }
    get widgetAriaLabelName() {
        return this.isReplHistory ? ( localize(11256, "REPL Editor History")) : ( localize(11257, "Notebook"));
    }
    getWidgetAriaLabel() {
        const keybinding = this.keybindingService.lookupKeybinding(AccessibilityCommandId.OpenAccessibilityHelp)?.getLabel();
        if (this.configurationService.getValue(this.verbositySettingId)) {
            return keybinding ? ( localize(
                11258,
                "{0}\nUse {1} for accessibility help",
                this.widgetAriaLabelName,
                keybinding
            )) : ( localize(
                11259,
                "{0}\nRun the Open Accessibility Help command for more information",
                this.widgetAriaLabelName
            ));
        }
        return this.widgetAriaLabelName;
    }
    mergeEvents(last, e) {
        const viewModel = this.viewModel();
        const result = last || [];
        if (viewModel && e.type === NotebookExecutionType.cell && e.affectsNotebook(viewModel.uri)) {
            const index = result.findIndex(update => update.cellHandle === e.cellHandle);
            if (index >= 0) {
                result.splice(index, 1);
            }
            result.push({
                cellHandle: e.cellHandle,
                state: e.changed?.state
            });
        }
        return result;
    }
};
NotebookAccessibilityProvider = ( __decorate([( __param(2, INotebookExecutionStateService)), ( __param(3, IKeybindingService)), ( __param(4, IConfigurationService)), ( __param(5, IAccessibilityService))], NotebookAccessibilityProvider));

export { NotebookAccessibilityProvider };
