<?php

/** Deduplicates semantic and lexical collision candidates by unordered post pair. */
class Content_Miner_Candidate_Union {
	/** @return array<int,array<string,mixed>> */
	public static function merge( array $lexical, array $semantic ) {
		$pairs = array();
		foreach ( array_merge( $lexical, $semantic ) as $pair ) {
			$left_id  = (int) $pair['left_id'];
			$right_id = (int) $pair['right_id'];
			$key = min( $left_id, $right_id ) . ':' . max( $left_id, $right_id );
			if ( ! isset( $pairs[ $key ] ) || (float) $pair['score'] > (float) $pairs[ $key ]['score'] ) {
				$pairs[ $key ] = $pair;
			}
		}
		$pairs = array_values( $pairs );
		usort( $pairs, function ( $a, $b ) { return $b['score'] <=> $a['score']; } );
		return $pairs;
	}
}
