 _ = require 'underscore-plus'
+{CompositeDisposable} = require 'atom'
 
 requireFrom = (pack, path) ->
   packPath = atom.packages.resolvePackagePath(pack)

CommandPrefix = 'vim-mode-plus-user'
stateByEditor = new Map
checkPointByEditor = new Map
# -------------------------
class MoveSelectedText extends TransformString
  getSelectedTexts: ->
    @editor.getSelections().map((selection) -> selection.getText()).join('')

  withUndoJoin: (fn) ->
    unless (isSequential = stateByEditor.get(@editor) is @getSelectedTexts())
      checkPointByEditor.set(@editor, @editor.createCheckpoint())

    fn()

    stateByEditor.set(@editor, @getSelectedTexts())
    if isSequential and checkPointByEditor.has(@editor)
      @editor.groupChangesSinceCheckpoint(checkPointByEditor.get(@editor))

class MoveSelectedTextUp extends MoveSelectedText
  @commandPrefix: CommandPrefix


  execute: ->
    return unless @isMode('visual')

    @withUndoJoin =>
      selections = @editor.getSelectionsOrderedByBufferPosition()
      selections.reverse() if @direction is 'down'
      @editor.transact =>
        for selection in selections
          @countTimes =>
            @mutate(selection)

  mutate: (selection) ->
    switch @vimState.submode
