import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import '../../../../base/common/arrays.js';
import '../../../../base/common/observableInternal/derived.js';
import '../../../../base/common/observableInternal/autorun.js';
import { observableFromEvent } from '../../../../base/common/observableInternal/utils.js';
import '../../../../base/common/cancellation.js';
import { localizeWithPath } from '../../../../nls.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';
const _moduleId = "vs/workbench/contrib/notebook/browser/notebookAccessibilityProvider";
class NotebookAccessibilityProvider extends Disposable {
    constructor(notebookExecutionStateService, viewModel, keybindingService, configurationService) {
        super();
        this.notebookExecutionStateService = notebookExecutionStateService;
        this.viewModel = viewModel;
        this.keybindingService = keybindingService;
        this.configurationService = configurationService;
        this._onDidAriaLabelChange = ( (new Emitter()));
        this.onDidAriaLabelChange = this._onDidAriaLabelChange.event;
        this._register(Event.debounce(this.notebookExecutionStateService.onDidChangeExecution, (last, e) => this.mergeEvents(last, e), 100)((cellHandles) => {
            const viewModel = this.viewModel();
            if (viewModel) {
                for (const handle of cellHandles) {
                    const cellModel = viewModel.getCellByHandle(handle);
                    if (cellModel) {
                        this._onDidAriaLabelChange.fire(cellModel);
                    }
                }
            }
        }, this));
    }
    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(index, element);
            }
            return '';
        });
    }
    getLabel(index, element) {
        const executionState = this.notebookExecutionStateService.getCellExecution(element.uri)?.state;
        const executionLabel = executionState === NotebookCellExecutionState.Executing
            ? ', executing'
            : executionState === NotebookCellExecutionState.Pending
                ? ', pending'
                : '';
        return `Cell ${index}, ${element.cellKind === CellKind.Markup ? 'markdown' : 'code'} cell${executionLabel}`;
    }
    getWidgetAriaLabel() {
        const keybinding = this.keybindingService.lookupKeybinding(AccessibilityCommandId.OpenAccessibilityHelp)?.getLabel();
        if (this.configurationService.getValue(AccessibilityVerbositySettingId.Notebook)) {
            return keybinding
                ? ( localizeWithPath(_moduleId, 0, "Notebook\nUse {0} for accessibility help", keybinding))
                : ( localizeWithPath(
                _moduleId,
                1,
                "Notebook\nRun the Open Accessibility Help command for more information",
                keybinding
            ));
        }
        return ( localizeWithPath(_moduleId, 2, "Notebook"));
    }
    mergeEvents(last, e) {
        const viewModel = this.viewModel();
        const result = last || [];
        if (viewModel && e.type === NotebookExecutionType.cell && e.affectsNotebook(viewModel.uri)) {
            if (result.indexOf(e.cellHandle) < 0) {
                result.push(e.cellHandle);
            }
        }
        return result;
    }
}
export { NotebookAccessibilityProvider };
