
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../../../../base/common/lifecycle.js';
import '../../../../../base/common/observableInternal/index.js';
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
import { CodeEditorWidget } from '../../../../browser/widget/codeEditor/codeEditorWidget.js';
import { StructuredLogger } from '../structuredLogger.js';
import { autorunWithStore } from '../../../../../base/common/observableInternal/autorun.js';

var TextModelChangeRecorder_1;
let TextModelChangeRecorder = class TextModelChangeRecorder extends Disposable {
    static { TextModelChangeRecorder_1 = this; }
    static { this._nextMetadataId = 0; }
    static { this._metaDataMap = ( new Map()); }
    static editWithMetadata(metadata, cb) {
        const id = this._nextMetadataId++;
        this._metaDataMap.set(id, metadata);
        try {
            const result = cb();
            return result;
        }
        finally {
            this._metaDataMap.delete(id);
        }
    }
    static _getCurrentMetadata() {
        const result = {};
        for (const metadata of ( this._metaDataMap.values())) {
            Object.assign(result, metadata);
        }
        return result;
    }
    constructor(_editor, _instantiationService) {
        super();
        this._editor = _editor;
        this._instantiationService = _instantiationService;
        this._structuredLogger = this._register(this._instantiationService.createInstance(StructuredLogger.cast(), 'editor.inlineSuggest.logChangeReason.commandId'));
        this._register(autorunWithStore((reader, store) => {
            if (!(this._editor instanceof CodeEditorWidget)) {
                return;
            }
            if (!this._structuredLogger.isEnabled.read(reader)) {
                return;
            }
            const sources = [];
            store.add(this._editor.onBeforeExecuteEdit(({ source }) => {
                if (source) {
                    sources.push(source);
                }
            }));
            store.add(this._editor.onDidChangeModelContent(e => {
                const tm = this._editor.getModel();
                if (!tm) {
                    return;
                }
                const metadata = TextModelChangeRecorder_1._getCurrentMetadata();
                if (sources.length === 0 && metadata.source) {
                    sources.push(metadata.source);
                }
                for (const source of sources) {
                    const data = {
                        ...metadata,
                        sourceId: 'TextModel.setChangeReason',
                        source: source,
                        time: Date.now(),
                        modelUri: ( tm.uri.toString()),
                        modelVersion: tm.getVersionId(),
                    };
                    setTimeout(() => {
                        this._structuredLogger.log(data);
                    }, 0);
                }
                sources.length = 0;
            }));
        }));
    }
};
TextModelChangeRecorder = TextModelChangeRecorder_1 = ( __decorate([
    ( __param(1, IInstantiationService))
], TextModelChangeRecorder));

export { TextModelChangeRecorder };
