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


  execute: ->
    return unless @isMode('visual')
    @withUndoJoin =>
      @eachSelection (selection) =>
        @mutate(selection)

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

CommandPrefix = 'vim-mode-plus-user'
stateByEditor = new Map
checkPointByEditor = new Map
disposableByEditor = new Map
# -------------------------
class MoveSelectedText extends TransformString
  countTimes: (fn) ->
    _.times @getCount(), ->
      fn()

  getSelectedTexts: ->
    @editor.getSelections().map((selection) -> selection.getText()).join('')

  withUndoJoin: (fn) ->
    unless disposableByEditor.has(@editor)
      disposableByEditor.set @editor, @editor.onDidDestroy =>
        checkPointByEditor.delete(@editor)
        stateByEditor.delete(@editor)

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

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

class MoveSelectedTextUp extends MoveSelectedText
  @commandPrefix: CommandPrefix
