/**
 * Return the track object for the track corresponding to this uid
 *
 * Null or undefined if none.
 */
const getTrackByUid = (tracks, uid) => {
  for (const trackType in tracks) {
    const theseTracks = tracks[trackType];

    // no tracks of this type specified, probably
    if (!theseTracks.filter) continue;

    const filteredTracks = theseTracks.filter(d => d.uid == uid);

    if (filteredTracks.length) { return filteredTracks[0]; }

    // check to see if this track is part of a combined track
    const combinedTracks = theseTracks.filter(d => d.type == 'combined');

    if (combinedTracks.length) {
      for (let i = 0; i < combinedTracks.length; i++) {
        const ct = combinedTracks[i];
        const filteredTracks = ct.contents.filter(d => d.uid == uid);

        if (filteredTracks.length) { return filteredTracks[0]; }
      }
    }
  }

  return null;
};

export default getTrackByUid;

/**
 * Return the track object for the track corresponding to this uid
 *
 * Null or undefined if none.
 */
const getTrackByUid = (tracks, uid) => {
  let found = null;

  const checkTrack = (track) => {
    if (track.uid === uid) found = track;
    if (track.type.substr(0, 8) === 'combined') track.contents.forEach(checkTrack);
    return null;
  };

  Object.keys(tracks)
    .map(trackType => tracks[trackType])
    .filter(tracksByPos => tracksByPos.filter)
    .reduce((a, b) => a.concat(b), [])
    .forEach(checkTrack);

  return found;
};

export default getTrackByUid;

Simplify and support recursively combined tracks.
Simplify and support recursively combined tracks.
