"use strict";

// TODO: Can probably be made more performant by precomputing a key -> item mapping, at the cost of multiple items with the same key becoming impossible

module.exports = function diffLists(oldArray, newArray, keyFunction = (item) => item) {
	// NOTE: This only detects additions and removals, *not* order changes! The order is not relevant for our usecase.
	let oldSet = new Set(oldArray.map((item) => keyFunction(item)));
	let newSet = new Set(newArray.map((item) => keyFunction(item)));

	let removed = oldArray.filter((item) => !newSet.has(keyFunction(item)));
	let added = newArray.filter((item) => !oldSet.has(keyFunction(item)));

	return { removed, added };
};
