'use strict';

var getCountOfSymbolsInSelection = require('../getCountOfSymbolsInSelection/getCountOfSymbolsInSelection.js');
var getDistanceToSymbol = require('../getDistanceToSymbol/getDistanceToSymbol.js');

const getCursorPositionAfterActionStroke = (action, selectionStart, selectionEnd, pattern, pastedLength) => {
  let cursorPosition = selectionStart;
  switch (action) {
    case 'Backspace':
      // If not multiple delete.
      if (selectionStart === selectionEnd) {
        cursorPosition -= getDistanceToSymbol.getDistanceToPreviousSymbol(selectionStart, pattern);
        cursorPosition = cursorPosition > 0 ? cursorPosition - 1 : 0;
      }
      break;
    case 'Paste':
      cursorPosition += pastedLength + getCountOfSymbolsInSelection(selectionStart, selectionStart + pastedLength, pattern) + getDistanceToSymbol.getDistanceToNextSymbol(selectionStart + pastedLength, pattern);
      break;
    case 'Cut':
    case 'Delete':
      break;
    default:
      cursorPosition += 1 + getDistanceToSymbol.getDistanceToNextSymbol(selectionStart, pattern);
      break;
  }
  return cursorPosition;
};

module.exports = getCursorPositionAfterActionStroke;
//# sourceMappingURL=getCursorPositionAfterKeystroke.js.map
