From a4eafd0307fb8c640455f4fcfa7adffbf4d9bb9c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= <adomas.ven@gmail.com>
Date: Tue, 25 Oct 2022 13:51:53 +0300
Subject: [PATCH] Fix wrong given name disambiguation after editing author for
 citeproc-js

Closes #2870
---
 chrome/content/zotero/xpcom/integration.js | 10 ++++++++++
 test/tests/integrationTest.js              | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js
index 5728691e70..73ff3364c1 100644
--- a/chrome/content/zotero/xpcom/integration.js
+++ b/chrome/content/zotero/xpcom/integration.js
@@ -1038,6 +1038,10 @@ Zotero.Integration.Session.prototype.updateFromDocument = Zotero.Promise.corouti
 	this.progressBar.start();
 	if (forceCitations) {
 		this.regenAll = true;
+		// See Session.restoreProcessorState() for a comment
+		if (!Zotero.Prefs.get('cite.useCiteprocRs')) {
+			this.reload = true;
+		}
 	}
 	yield this._processFields();
 	try {
@@ -2175,6 +2179,12 @@ Zotero.Integration.Session.prototype.restoreProcessorState = function() {
 			citations.push(this.citationsByIndex[i]);
 		}
 	}
+	if (!Zotero.Prefs.get('cite.useCiteprocRs')) {
+		// Due to a bug in citeproc-js there are disambiguation issues after changing items in Zotero library
+		// and rebuilding the processor state, so we reinitialize the processor altogether
+		let style = Zotero.Styles.get(this.data.style.styleID);
+		this.style = style.getCiteProc(this.data.style.locale, this.outputFormat, this.data.prefs.automaticJournalAbbreviations);
+	}
 	this.style.rebuildProcessorState(citations, this.outputFormat, uncited);
 }
 
diff --git a/test/tests/integrationTest.js b/test/tests/integrationTest.js
index 91780c32ee..b87e85a635 100644
--- a/test/tests/integrationTest.js
+++ b/test/tests/integrationTest.js
@@ -938,6 +938,27 @@ describe("Zotero.Integration", function () {
 				assert.isTrue(displayDialogStub.lastCall.args[0].includes('editBibliographyDialog'));
 			});
 		});
+		
+		describe('#refresh', function() {
+			var docID = this.fullTitle();
+			it ('should properly disambiguate author after editing in the database', async function () {
+				let testItem1 = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});
+				testItem1.setField('title', `title1`);
+				testItem1.setCreator(0, {creatorType: 'author', firstName: "Foo", lastName: "Bar"});
+				testItem1.setField('date', '2022-01-01');
+				let testItem2 = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});
+				testItem2.setField('title', `title2`);
+				testItem2.setCreator(0, {creatorType: 'author', firstName: "Foo", lastName: "Bar"});
+				testItem2.setField('date', '2022-01-01');
+				setAddEditItems([testItem1, testItem2]);
+				await initDoc(docID);
+				await execCommand('addEditCitation', docID);
+				assert.equal(applications[docID].doc.fields[0].text, '(Bar, 2022a, 2022b)');
+				testItem2.setCreator(0, {creatorType: 'author', firstName: "Foo F", lastName: "Bar"});
+				await execCommand('refresh', docID);
+				assert.equal(applications[docID].doc.fields[0].text, '(F. Bar, 2022; F. F. Bar, 2022)');
+			});
+		});
 	});
 	
 	describe("DocumentData", function() {