import { __decorate, __param } from '../../../../../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../../../../../base/common/lifecycle.js';
import { IMarkerService } from '../../../../../../platform/markers/common/markers.js';
import { NotebookExecutionType, INotebookExecutionStateService } from '../../../common/notebookExecutionStateService.js';
import { IInlineChatService } from '../../../../inlineChat/common/inlineChat.js';
import { Iterable } from '../../../../../../base/common/iterator.js';
import { IConfigurationService } from '../../../../../../platform/configuration/common/configuration.js';
import { NotebookSetting } from '../../../common/notebookCommon.js';
import { Emitter } from '../../../../../../base/common/event.js';
var CellDiagnostics_1;
let CellDiagnostics = class CellDiagnostics extends Disposable {
    static { CellDiagnostics_1 = this; }
    static { this.ID = 'workbench.notebook.cellDiagnostics'; }
    get ErrorDetails() {
        return this.errorDetails;
    }
    constructor(cell, notebookExecutionStateService, markerService, inlineChatService, configurationService) {
        super();
        this.cell = cell;
        this.notebookExecutionStateService = notebookExecutionStateService;
        this.markerService = markerService;
        this.inlineChatService = inlineChatService;
        this.configurationService = configurationService;
        this._onDidDiagnosticsChange = ( new Emitter());
        this.onDidDiagnosticsChange = this._onDidDiagnosticsChange.event;
        this.enabled = false;
        this.listening = false;
        this.errorDetails = undefined;
        if (cell.viewType !== 'interactive') {
            this.updateEnabled();
            this._register(inlineChatService.onDidChangeProviders(() => this.updateEnabled()));
            this._register(configurationService.onDidChangeConfiguration((e) => {
                if (e.affectsConfiguration(NotebookSetting.cellFailureDiagnostics)) {
                    this.updateEnabled();
                }
            }));
        }
    }
    updateEnabled() {
        const settingEnabled = this.configurationService.getValue(NotebookSetting.cellFailureDiagnostics);
        if (this.enabled && (!settingEnabled || Iterable.isEmpty(this.inlineChatService.getAllProvider()))) {
            this.enabled = false;
            this.clear();
        }
        else if (!this.enabled && settingEnabled && !Iterable.isEmpty(this.inlineChatService.getAllProvider())) {
            this.enabled = true;
            if (!this.listening) {
                this.listening = true;
                this._register(this.notebookExecutionStateService.onDidChangeExecution((e) => this.handleChangeExecutionState(e)));
            }
        }
    }
    handleChangeExecutionState(e) {
        if (this.enabled && e.type === NotebookExecutionType.cell && e.affectsCell(this.cell.uri)) {
            if (!!e.changed) {
                this.clear();
            }
            else {
                this.setDiagnostics();
            }
        }
    }
    clear() {
        if (this.ErrorDetails) {
            this.markerService.changeOne(CellDiagnostics_1.ID, this.cell.uri, []);
            this.errorDetails = undefined;
            this._onDidDiagnosticsChange.fire();
        }
    }
    setDiagnostics() {
        const metadata = this.cell.model.internalMetadata;
        if (!metadata.lastRunSuccess && metadata?.error?.location) {
            const marker = this.createMarkerData(metadata.error.message, metadata.error.location);
            this.markerService.changeOne(CellDiagnostics_1.ID, this.cell.uri, [marker]);
            this.errorDetails = metadata.error;
            this._onDidDiagnosticsChange.fire();
        }
    }
    createMarkerData(message, location) {
        return {
            severity: 8,
            message: message,
            startLineNumber: location.startLineNumber + 1,
            startColumn: location.startColumn + 1,
            endLineNumber: location.endLineNumber + 1,
            endColumn: location.endColumn + 1,
            source: 'Cell Execution Error'
        };
    }
    dispose() {
        super.dispose();
        this.clear();
    }
};
CellDiagnostics = CellDiagnostics_1 = ( __decorate([
    ( __param(1, INotebookExecutionStateService)),
    ( __param(2, IMarkerService)),
    ( __param(3, IInlineChatService)),
    ( __param(4, IConfigurationService))
], CellDiagnostics));
export { CellDiagnostics };
