const require_computeSimilarity = require('./computeSimilarity.cjs');

//#region ../@intlayer/cli/dist/esm/translation-alignment/alignBlocks.mjs
const alignEnglishAndFrenchBlocks = (defaultBlocks, secondaryBlocks) => {
	const defaultLength = defaultBlocks.length;
	const secondaryLength = secondaryBlocks.length;
	const scoreMatrix = Array.from({ length: defaultLength + 1 }, () => Array.from({ length: secondaryLength + 1 }, () => 0));
	const traceMatrix = Array.from({ length: defaultLength + 1 }, () => Array.from({ length: secondaryLength + 1 }, () => "diagonal"));
	const gapPenalty = -2;
	const computeMatchScore = (defaultIndex, secondaryIndex) => {
		const defaultBlock = defaultBlocks[defaultIndex];
		const secondaryBlock = secondaryBlocks[secondaryIndex];
		const typeBonus = defaultBlock.type === secondaryBlock.type ? 2 : 0;
		const anchorSimilarity = require_computeSimilarity.computeJaccardSimilarity(defaultBlock.anchorText, secondaryBlock.anchorText, 3);
		return typeBonus + (Math.min(defaultBlock.content.length, secondaryBlock.content.length) / Math.max(defaultBlock.content.length, secondaryBlock.content.length) > .75 ? 1 : 0) + anchorSimilarity * 8;
	};
	for (let i$1 = 1; i$1 <= defaultLength; i$1 += 1) {
		scoreMatrix[i$1][0] = scoreMatrix[i$1 - 1][0] + gapPenalty;
		traceMatrix[i$1][0] = "up";
	}
	for (let j$1 = 1; j$1 <= secondaryLength; j$1 += 1) {
		scoreMatrix[0][j$1] = scoreMatrix[0][j$1 - 1] + gapPenalty;
		traceMatrix[0][j$1] = "left";
	}
	for (let i$1 = 1; i$1 <= defaultLength; i$1 += 1) for (let j$1 = 1; j$1 <= secondaryLength; j$1 += 1) {
		const match = scoreMatrix[i$1 - 1][j$1 - 1] + computeMatchScore(i$1 - 1, j$1 - 1);
		const deleteGap = scoreMatrix[i$1 - 1][j$1] + gapPenalty;
		const insertGap = scoreMatrix[i$1][j$1 - 1] + gapPenalty;
		const best = Math.max(match, deleteGap, insertGap);
		scoreMatrix[i$1][j$1] = best;
		traceMatrix[i$1][j$1] = best === match ? "diagonal" : best === deleteGap ? "up" : "left";
	}
	const result = [];
	let i = defaultLength;
	let j = secondaryLength;
	while (i > 0 || j > 0) if (i > 0 && j > 0 && traceMatrix[i][j] === "diagonal") {
		const englishIndex = i - 1;
		const frenchIndex = j - 1;
		const similarityScore = require_computeSimilarity.computeJaccardSimilarity(defaultBlocks[englishIndex].anchorText, secondaryBlocks[frenchIndex].anchorText, 3);
		result.unshift({
			englishIndex,
			frenchIndex,
			similarityScore
		});
		i -= 1;
		j -= 1;
	} else if (i > 0 && (j === 0 || traceMatrix[i][j] === "up")) {
		result.unshift({
			englishIndex: i - 1,
			frenchIndex: null,
			similarityScore: 0
		});
		i -= 1;
	} else if (j > 0 && (i === 0 || traceMatrix[i][j] === "left")) {
		result.unshift({
			englishIndex: -1,
			frenchIndex: j - 1,
			similarityScore: 0
		});
		j -= 1;
	}
	return result;
};

//#endregion
exports.alignEnglishAndFrenchBlocks = alignEnglishAndFrenchBlocks;
//# sourceMappingURL=alignBlocks.cjs.map