 class @LabelManager
+  errorMessage: 'Unable to update label prioritization at this time'
+
   constructor: (opts = {}) ->
     # Defaults
     {

    $label.detach().appendTo($target)

    # Return if we are not persisting state
    return if persistState

    if action is 'remove'
      xhr = $.ajax url: url, type: 'DELETE'

      # If request fails, put label back to Other labels group
      xhr.fail ->
        _this.toggleLabelPriority($label, 'remove', true)

        # Show a message
        new Flash('Unable to update label prioritization at this time' , 'alert')
    else
      @savePrioritySort()

  onPrioritySortUpdate: ->
    @savePrioritySort()

  savePrioritySort: ->
    xhr = $.post
            url: @prioritizedLabels.data('url')
            data:
              label_ids: @getSortedLabelsIds()

    xhr.fail ->
      new Flash('Unable to update label prioritization at this time' , 'alert')

  getSortedLabelsIds: ->
    sortedIds = []

    action = if $btn.parents('.js-prioritized-labels').length then 'remove' else 'add'
    _this.toggleLabelPriority($label, action)

  toggleLabelPriority: ($label, action, persistState = true) ->
    _this = @
    url = $label.find('.js-toggle-priority').data 'url'

