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


EmbyRawItem mergeSelectedEpisodeDetail({
  required EmbyRawItem detail,
  required EmbyRawItem listItem,
}) {
  final mergedRunTime = detail.RunTimeTicks ?? listItem.RunTimeTicks;
  final mergedUserData = _mergeUserData(detail.UserData, listItem.UserData);
  if (mergedRunTime == detail.RunTimeTicks &&
      identical(mergedUserData, detail.UserData)) {
    return detail;
  }
  return detail.copyWith(RunTimeTicks: mergedRunTime, UserData: mergedUserData);
}

EmbyRawUserData? _mergeUserData(
  EmbyRawUserData? detail,
  EmbyRawUserData? listItem,
) {
  if (listItem == null) return detail;
  if (detail == null) return listItem;
  if (detail.PlaybackPositionTicks == null &&
      listItem.PlaybackPositionTicks != null) {
    return detail.copyWith(
      PlaybackPositionTicks: listItem.PlaybackPositionTicks,
    );
  }
  return detail;
}

EmbyRawItem applyPlaybackReturnProgress(EmbyRawItem item, int? positionTicks) {
  if (positionTicks == null || positionTicks <= 0) return item;
  final userData = (item.UserData ?? const EmbyRawUserData()).copyWith(
    PlaybackPositionTicks: positionTicks,
  );
  return item.copyWith(UserData: userData);
}
