import { AxiosResponse } from "axios" ; import { SearchResultModel } from "../models/SearchResultModel" ; import { image404_url } from "./constants" ; import { PlaylistTrackModel } from "../models/PlaylistTrackModel" ; import { handleError } from "./apiErrorHandler" ; export const mapJSONTrackSearchToModel = ( data ) => { let results = new Map < string , SearchResultModel > ( ) ; try { let searchResultsAsJSON = data . data . trackResult . tracks . items ; for ( let i = 0 ; i < searchResultsAsJSON . length ; i ++ ) { let tempImgUrl = searchResultsAsJSON [ i ] . album . images [ 0 ] . url || image404_url ; let tempTitle = searchResultsAsJSON [ i ] . name || "Title Not Found" ; let tempArtistName = searchResultsAsJSON [ i ] . artists [ 0 ] . name || "Artist Not Found" ; let tempTrackId = searchResultsAsJSON [ i ] . id ; results . set ( tempTrackId , { trackId : tempTrackId , title : tempTitle , imgUrl : tempImgUrl , artistName : tempArtistName , } ) ; } } catch ( e ) { console . error ( "Failed to convert track result response to model" , e ) ; handleError ( { message : "401" } ) ; } return results ; } ; export const mapJSONRecommendedTracksToModel = ( data ) => { let explicit = [ ] ; let nonExplicit = [ ] ; try { if ( ! data ) { return [ [ ] , [ ] ] ; } let dataAsJSON = data . data . trackResult ; for ( let i = 0 ; i < dataAsJSON . tracks . length ; i ++ ) { let curr = dataAsJSON . tracks [ i ] ; let track = { trackId : curr . id , title : curr . name , imgUrl : curr . album . images [ 0 ] . url , artistName : curr . artists [ 0 ] . name , explicit : curr . explicit } ; explicit . push ( track ) ; if ( ! track . explicit ) { nonExplicit . push ( track ) ; } } } catch ( e ) { console . error ( "Failed to convert recommendations to model" ) ; handleError ( { message : "401" } ) ; } return [ explicit , nonExplicit ] ; } ; export const idsFromTracks = ( tracks ) => { let ids = [ ] ; tracks . forEach ( ( t ) => { ids . push ( t . trackId ) ; } ) ; return ids ; } ;