import '../../core/contracts/library.dart';


TmdbLibraryHit? selectPrimaryTmdbHit(
  List<TmdbLibraryHit> hits, {
  String? activeServerId,
}) {
  if (hits.isEmpty) return null;

  int score(TmdbLibraryHit h) {
    var s = 0;
    if ((h.playbackPositionTicks ?? 0) > 0) s += 2;
    if (activeServerId != null && h.serverId == activeServerId) s += 1;
    return s;
  }

  var best = hits.first;
  var bestScore = score(best);
  for (final h in hits.skip(1)) {
    final s = score(h);
    if (s > bestScore) {
      best = h;
      bestScore = s;
    }
  }
  return best;
}
