from apollo_utils.core.constants.dsp import DSP from typing import Any, Dict, List, Optional from server.constants.playlists.delphi.by_api.track_positions_previous_playlists import TrackPositionsPreviousPlaylists from server.scenarios.playlists.by_track.previous.common import get_previous_playlists_by_track from server.utils.common import group_by_items async def get_previous_playlists_apple_by_track_ap( isrc: str, country_code: List[str], meta_country_code: str, delphi_include: Optional[List[TrackPositionsPreviousPlaylists.INCLUDE]] = None, streams_country_code: Optional[List[str]] = None, ) -> Dict[str, List[Dict[str, Any]]]: """Adapter to create a structure for response formation Groups By All playlists into one object by playlist_id And adds a List of all others found track in playlists to the main one by the "countries" key Args: isrc: ISRC. country_code: meta_country_code: Country code of a main playlist all the Metadata will be taken delphi_include: Include optional fields. streams_country_code: Streams country codes. Returns: Grouped Track in Playlists with main playlist by playlist id. Main playlist is chosen by meta_country_code """ previous_playlists = await get_previous_playlists_by_track( dsp=DSP.APPLE, isrc=isrc, country_code=country_code, delphi_include=[TrackPositionsPreviousPlaylists.INCLUDE.PLAYLISTS] + delphi_include, sort_by=[TrackPositionsPreviousPlaylists.SORT_BY.REMOVED_DATE_TIME], sort_order=[TrackPositionsPreviousPlaylists.SORT_ORDER.ASC], streams_country_code=streams_country_code, ) items = await group_by_items( items=previous_playlists["items"], item_group_by_key="playlist_id", item_meta_identifier_key="playlist.country_code", meta_identifier=meta_country_code, add_key="countries", ) return {"items": items}