var rusha = new (require('rusha'))()
var metadataReader = require('music-metadata')
var fs = require('file-system')(['', 'audio/mp3', 'audio/wav', 'audio/ogg'])
var coversActions = require('./covers.js')

var actions = {

  // Add a file as a song. This hashes the file, adds it to the filesystem,
  // gets the metadata and the duration and dispatches the result metadata.
  // To add multiple files just dispatch this action multiple times.
  ADD_SONG: (file) => {
    return (dispatch, getState) => {
      // Update the display state to include the import progress
      dispatch({type: 'INCREMENT_IMPORTING_SONGS'})

      // Extract the file ending
      var file_ending = 'mp3'
      if (file.name.match(/^.*(\.[A-Za-z0-9]{3})$/)) {
        file_ending = file.name.replace(/^.*(\.[A-Za-z0-9]{3})$/, '$1')
      }

      // Read the file as an data url
      var reader = new window.FileReader()
      reader.readAsDataURL(file)
      reader.onerror = function (err) {
        dispatch({type: 'DECREMENT_IMPORTING_SONGS'})
        throw new Error('Error reading file: ' + err)
      }

      reader.onloadend = function () {
        // Hash the file contents and set the filename based on that
        var hash = rusha.digestFromString(this.result)
        var hashName = hash + file_ending

        // Get the metadata off the file
        metadataReader(file, meta => {
          // Add the song to the file system
          fs.add({filename: hashName, file: file}, (err) => {
            if (err) {
              dispatch({type: 'DECREMENT_IMPORTING_SONGS'})
              throw new Error('Error adding file: ' + err)
            }

            // Read the file as an url from the filesystem
            fs.get(hashName, (err, url) => {
              if (err) {
                dispatch({type: 'DECREMENT_IMPORTING_SONGS'})
                throw new Error('Error getting file: ' + err)
              }

              // Create an audio element to check on the duration
              var audio = document.createElement('audio')
              audio.src = url
              audio.addEventListener('durationchange', () => {
                var duration = audio.duration

                // Dispatch an action to update the view and save
                // the song data in local storage
                var song = {
                  id: hash,
                  filename: url,
                  ...meta,
                  addedAt: (new Date()).toString(),
                  local: true,
                  duration: duration,
                  favorited: false,
                  coverId: getCoverId(meta),
                  availability: 0,
                  hashName: hashName,
                  originalFilename: file.name
                }

                // Dispatch an action to get the cover from the scraping server
                coversActions.GET_COVER(song.album, song.artist, song.coverId)(dispatch, getState)

                dispatch({type: 'DECREMENT_IMPORTING_SONGS'})
                dispatch({
                  type: 'ADD_SONG',
                  song: song
                })
              })
            })
          })
        })
      }
    }
  },

  // Reset the counter for importing songs at application start
  RESET_IMPORTING_SONGS: () => ({
    type: 'RESET_IMPORTING_SONGS'
  }),

  // Remove a song
  REMOVE_SONG: (id) => {
    return (dispatch, getState) => {
      var state = getState()
      const filename = state.songs.filter(x => x.id === id)[0].hashName
      fs.delete(filename, (err) => {
        if (err) throw new Error('Error removing song: ' + err)

        // If we still hold providers for this, just set the song on "local",
        // else it was a song that only we had, so remove it completely
        if (state.sync.providers[id] && state.sync.providers[id].length > 0) {
          dispatch({type: 'TOGGLE_SONG_LOCAL', id})
        } else {
          dispatch({type: 'REMOVE_SONG', id})
        }
      })
    }
  },

  // Toggle the favorite status of a song
  TOGGLE_SONG_FAVORITE: (id) => {
    return {
      type: 'TOGGLE_SONG_FAVORITE',
      id
    }
  },

  // Remove all songs
  CLEAR_DATA: () => {
    return () => {
      // Clear the filesystem
      fs.clear((err) => {
        if (err) throw new Error('Error clearing filesystem: ' + err)

        // Remove the state from local storage
        window.localStorage.removeItem('peermusic-storage')

        // Refresh the page
        window.location.reload()
      })
    }
  }

}

// Get the cover id from metadata
function getCoverId (meta) {
  const cleanup = s => (s || '').toLowerCase().replace(/[^a-zA-Z0-9]/g, '')
  return rusha.digestFromString(cleanup(meta.album) + cleanup(meta.artist))
}

module.exports = actions

@@ -4163,29 +4163,8 @@
 %3E %7B%0A
-    return () =%3E %7B%0A  
     
@@ -4191,18 +4191,16 @@
 tem%0A    
-  
 fs.clear
@@ -4203,34 +4203,32 @@
 lear((err) =%3E %7B%0A
-  
       if (err) t
@@ -4282,26 +4282,24 @@
 err)%0A%0A      
- 
- 
 // Remove th
@@ -4321,26 +4321,24 @@
 cal storage%0A
-  
       window
@@ -4384,18 +4384,16 @@
 rage')%0A%0A
-  
       //
@@ -4412,26 +4412,24 @@
  page%0A      
-  
 window.locat
@@ -4441,34 +4441,26 @@
 eload()%0A    
-  %7D)%0A    
 %7D
+)
 %0A  %7D%0A%0A%7D%0A%0A// 

Remove unnecessary function