
import { __decorate, __param } from '../../../../../../../../../external/tslib/tslib.es6.js';
import { ReferenceCollection, AsyncReferenceCollection } from '../../../../../../base/common/lifecycle.js';
import { INotebookService } from '../../../common/notebookService.service.js';
import { VSBuffer, bufferToStream } from '../../../../../../base/common/buffer.js';
import { IInstantiationService } from '../../../../../../platform/instantiation/common/instantiation.js';
import { ITextModelService } from '../../../../../../editor/common/services/resolverService.service.js';

let OriginalNotebookModelReferenceCollection = class OriginalNotebookModelReferenceCollection extends ReferenceCollection {
    constructor(notebookService, modelService) {
        super();
        this.notebookService = notebookService;
        this.modelService = modelService;
        this.modelsToDispose = ( new Set());
    }
    async createReferencedObject(key, fileEntry, viewType) {
        this.modelsToDispose.delete(key);
        const uri = fileEntry.originalURI;
        const model = this.notebookService.getNotebookTextModel(uri);
        if (model) {
            return model;
        }
        const modelRef = await this.modelService.createModelReference(uri);
        const bytes = VSBuffer.fromString(modelRef.object.textEditorModel.getValue());
        const stream = bufferToStream(bytes);
        modelRef.dispose();
        return this.notebookService.createNotebookTextModel(viewType, uri, stream);
    }
    destroyReferencedObject(key, modelPromise) {
        this.modelsToDispose.add(key);
        (async () => {
            try {
                const model = await modelPromise;
                if (!( this.modelsToDispose.has(key))) {
                    return;
                }
                model.dispose();
            } catch (error) {} finally {
                this.modelsToDispose.delete(key);
            }
        })();
    }
};
OriginalNotebookModelReferenceCollection = ( __decorate([( __param(0, INotebookService)), ( __param(1, ITextModelService))], OriginalNotebookModelReferenceCollection));
let NotebookOriginalModelReferenceFactory = class NotebookOriginalModelReferenceFactory {
    get resourceModelCollection() {
        if (!this._resourceModelCollection) {
            this._resourceModelCollection = this.instantiationService.createInstance(OriginalNotebookModelReferenceCollection);
        }
        return this._resourceModelCollection;
    }
    get asyncModelCollection() {
        if (!this._asyncModelCollection) {
            this._asyncModelCollection = ( new AsyncReferenceCollection(this.resourceModelCollection));
        }
        return this._asyncModelCollection;
    }
    constructor(instantiationService) {
        this.instantiationService = instantiationService;
        this._resourceModelCollection = undefined;
        this._asyncModelCollection = undefined;
    }
    getOrCreate(fileEntry, viewType) {
        return this.asyncModelCollection.acquire(( fileEntry.originalURI.toString()), fileEntry, viewType);
    }
};
NotebookOriginalModelReferenceFactory = ( __decorate([( __param(0, IInstantiationService))], NotebookOriginalModelReferenceFactory));

export { NotebookOriginalModelReferenceFactory, OriginalNotebookModelReferenceCollection };
