export default function unionIntersect(...arrays) {
  if (arrays.length === 0) return [];
  if (arrays.length === 1) return arrays[0];

  const result = [];
  const sets = arrays.slice(1).map(array => new Set(array));

  // TODO: Use a priority queue
  arrays[0].forEach((value) => {
    if (sets.every(set => set.has(value))) {
      result.push(value);
    }
  });

  return result;
}