"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionNotebookDocumentManagerImpl = void 0;
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
const ext_host_notebookdocument_1 = require("./ext.host.notebookdocument");
class ExtensionNotebookDocumentManagerImpl {
    constructor(rpcProtocol, extHostDoc) {
        this.extHostDoc = extHostDoc;
        this._documents = new ide_core_common_1.ResourceMap();
        this._onDidOpenNotebookDocument = new ide_core_common_1.Emitter();
        this._onDidCloseNotebookDocument = new ide_core_common_1.Emitter();
        this._onDidChangeNotebookDocument = new ide_core_common_1.Emitter();
        this._onDidSaveNotebookDocument = new ide_core_common_1.Emitter();
        this.onDidOpenNotebookDocument = this._onDidOpenNotebookDocument.event;
        this.onDidCloseNotebookDocument = this._onDidCloseNotebookDocument.event;
        this.onDidChangeNotebookDocument = this._onDidChangeNotebookDocument.event;
        this.onDidSaveNotebookDocument = this._onDidSaveNotebookDocument.event;
        this.rpcProtocol = rpcProtocol;
        this._proxy = this.rpcProtocol.getProxy(vscode_1.MainThreadAPIIdentifier.MainThreadNotebook);
    }
    get notebookDocuments() {
        return [...this._documents.values()].map((item) => item.apiNotebook);
    }
    getNotebookDocument(uri, relaxed) {
        const result = this._documents.get(uri);
        if (!result && !relaxed) {
            throw new Error(`NO notebook document for '${uri}'`);
        }
        return result;
    }
    $acceptDocumentAndEditorsDelta(delta) {
        var _a;
        if (delta.removedDocuments) {
            for (const uri of delta.removedDocuments) {
                const revivedUri = ide_core_common_1.Uri.revive(uri);
                const document = this._documents.get(revivedUri);
                if (document) {
                    document.dispose();
                    this._documents.delete(revivedUri);
                    // this.extHostDoc.$acceptDocumentsAndEditorsDelta({ removedDocuments: document.apiNotebook.getCells().map((cell) => cell.document.uri) });
                    const removedDocuments = document.apiNotebook.getCells().map((cell) => cell.document.uri);
                    removedDocuments.forEach((doc) => {
                        this.extHostDoc.$fireModelRemovedEvent({ uri: doc.toString() });
                    });
                    this._onDidCloseNotebookDocument.fire(document.apiNotebook);
                }
            }
        }
        if (delta.addedDocuments) {
            const addedCellDocuments = [];
            for (const modelData of delta.addedDocuments) {
                const uri = ide_core_common_1.Uri.revive(modelData.uri);
                if (this._documents.has(uri)) {
                    throw new Error(`adding EXISTING notebook ${uri} `);
                }
                const document = new ext_host_notebookdocument_1.ExtHostNotebookDocument(this._proxy, this.extHostDoc, uri, modelData);
                // add cell document as vscode.TextDocument
                addedCellDocuments.push(...modelData.cells.map((cell) => ext_host_notebookdocument_1.ExtHostCell.asModelAddData(cell)));
                (_a = this._documents.get(uri)) === null || _a === void 0 ? void 0 : _a.dispose();
                this._documents.set(uri, document);
                // this._textDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({ addedDocuments: addedCellDocuments });
                addedCellDocuments.forEach((doc) => {
                    this.extHostDoc.$fireModelOpenedEvent(doc);
                });
                this._onDidOpenNotebookDocument.fire(document.apiNotebook);
            }
        }
    }
    $acceptModelChanged(uri, event, isDirty, newMetadata) {
        const document = this.getNotebookDocument(ide_core_common_1.Uri.revive(uri));
        const e = document.acceptModelChanged(event, isDirty, newMetadata);
        this._onDidChangeNotebookDocument.fire(e);
    }
    $acceptDirtyStateChanged(uri, isDirty) {
        const document = this.getNotebookDocument(ide_core_common_1.Uri.revive(uri));
        document.acceptDirty(isDirty);
    }
    $acceptModelSaved(uri) {
        const document = this.getNotebookDocument(ide_core_common_1.Uri.revive(uri));
        this._onDidSaveNotebookDocument.fire(document.apiNotebook);
    }
}
exports.ExtensionNotebookDocumentManagerImpl = ExtensionNotebookDocumentManagerImpl;
//# sourceMappingURL=ext.host.notebook.js.map