<commit_before>import undoable from 'redux-undo';

import { ActionTypes } from 'redux-undo';
import { REPLACE_SONG, INSERT_TRACK, DELETE_TRACK } from '../actions/tracks';
import { INSERT_MEASURE, DELETE_MEASURE, CHANGE_BPM, CHANGE_TIME_SIGNATURE } from '../actions/track';

import trackReducer from './track';
import { prepareTrack } from '../util';

const replaceTrack = (tracks, currentTrackIndex, layout, scoreBox, action) => {
  return tracks.map((track, index) => {
    if(index === currentTrackIndex) {
      return prepareTrack(trackReducer(track, action), layout, scoreBox);
    }
    return track;
  });
};

const applyActionToEachTrack = (tracks, layout, scoreBox, action) => (
  tracks.map(track => prepareTrack(trackReducer(track, action), layout, scoreBox))
);

const undoableTracks = undoable((state, action) => {
  return action.newTracks;
});

const tracks = (tracks, action, currentTrackIndex, layout, scoreBox, newTracks) => {
  switch(action.type) {
    case ActionTypes.UNDO:
    case ActionTypes.REDO: {
      const newState = undoableTracks(tracks, action);
      return newState;
    }

    case REPLACE_SONG: {
      return undoableTracks(tracks, {
        type: 'tracks',
        newTracks: action.tracks.map(track => prepareTrack(track, layout, scoreBox))
      });
    }

    case INSERT_TRACK:
    case DELETE_TRACK: {
      return undoableTracks(tracks, {
        type: 'tracks',
        newTracks: replaceTrack(newTracks, currentTrackIndex, layout, scoreBox, action)
      });
    }

    case INSERT_MEASURE:<commit_msg>fix bug in undo where the layout data is messed up in the initial version of the state<commit_after>import undoable from 'redux-undo';

import { ActionTypes } from 'redux-undo';
import { REPLACE_SONG, INSERT_TRACK, DELETE_TRACK } from '../actions/tracks';
import { INSERT_MEASURE, DELETE_MEASURE, CHANGE_BPM, CHANGE_TIME_SIGNATURE } from '../actions/track';

import trackReducer from './track';
import { prepareTrack } from '../util';

const replaceTrack = (tracks, currentTrackIndex, layout, scoreBox, action) => {
  return tracks.map((track, index) => {
    if(index === currentTrackIndex) {
      return prepareTrack(trackReducer(track, action), layout, scoreBox);
    }
    return track;
  });
};

const applyActionToEachTrack = (tracks, layout, scoreBox, action) => (
  tracks.map(track => prepareTrack(trackReducer(track, action), layout, scoreBox))
);

const undoableTracks = undoable((state, action) => {
  return action.newTracks;
});

const tracks = (tracks, action, currentTrackIndex, layout, scoreBox, newTracks) => {
  switch(action.type) {
    case ActionTypes.UNDO:
    case ActionTypes.REDO: {
      if(action.type === ActionTypes.UNDO && tracks.past.length < 2) {
        return tracks;
      }
      return undoableTracks(tracks, action);
    }

    case REPLACE_SONG: {
      return undoableTracks(tracks, {
        type: 'tracks',
        newTracks: action.tracks.map(track => prepareTrack(track, layout, scoreBox))
      });
    }

    case INSERT_TRACK:
    case DELETE_TRACK: {
      return undoableTracks(tracks, {
        type: 'tracks',
        newTracks: replaceTrack(newTracks, currentTrackIndex, layout, scoreBox, action)
      });
    }

    case INSERT_MEASURE:
{'fraction_non_alphanumeric': 0.1255397902529303, 'fraction_numerical': 0.00030845157310302283, 'mean_word_length': 3.790251107828656, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2062091', 'n_tokens_mistral': 1066, 'n_tokens_neox': 999, 'n_words': 241}