import { StudioProjectState, StudioTrack } from '../types';

/**
 * Creates a state updater function that updates a list of tracks within the project state.
 * This is a common pattern used throughout the codebase to update a track's properties.
 *
 * @param trackIds The IDs of the tracks to update
 * @param updateFn A function that receives a current track and returns an updated track
 * @returns A function that can be used with setState to update the track
 */
export default function updateTracks(
  trackIds: string[],
  updateFn: (track: StudioTrack) => StudioTrack
) {
  return (state: StudioProjectState): StudioProjectState => ({
    ...state,
    tracks: state.tracks.map((track) =>
      trackIds.includes(track.id) ? updateFn(track) : track
    ),
  });
}

export const updateSelectedTracks =
  (updateFn: (track: StudioTrack) => StudioTrack) =>
  (state: StudioProjectState): StudioProjectState => ({
    ...state,
    tracks: state.tracks.map((track) =>
      state.selection.trackIds.includes(track.id) ? updateFn(track) : track
    ),
  });
