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


CrossServerSearchReq? buildEpisodeCrossServerReq({
  required EmbyRawItem episode,
  EmbyRawItem? seriesItem,
  int? seasonNumberOverride,
}) {
  final seasonNumber =
      seasonNumberOverride ??
      (episode.extra['ParentIndexNumber'] as num?)?.toInt();
  final episodeNumber = episode.IndexNumber;
  if (seasonNumber == null || episodeNumber == null) return null;

  final providerIds = providerIdsOfItem(episode);
  final seriesProviderIds = seriesItem != null
      ? providerIdsOfItem(seriesItem)
      : const <String, String>{};
  if (providerIds.isEmpty && seriesProviderIds.isEmpty) return null;

  return CrossServerSearchReq(
    anchorType: 'Episode',
    itemName: episode.Name,
    providerIds: providerIds,
    seriesProviderIds: seriesProviderIds,
    parentIndexNumber: seasonNumber,
    indexNumber: episodeNumber,
  );
}


String encodeProviderIdsForKey(Map<String, String> ids) {
  if (ids.isEmpty) return '';
  final entries = ids.entries.toList()..sort((a, b) => a.key.compareTo(b.key));
  return entries
      .where((e) => e.value.isNotEmpty)
      .map((e) => '${e.key}.${e.value}')
      .join(',');
}
