'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var getMaskedValue = function getMaskedValue(_ref) {
  var value = _ref.value,
    prevValue = _ref.prevValue,
    format = _ref.format,
    delimiters = _ref.delimiters,
    selectionStart = _ref.selectionStart;
  if (!value) {
    return {
      formattedValue: '',
      selectionStart: 0
    };
  }
  if (!format || !(delimiters !== null && delimiters !== void 0 && delimiters.length)) {
    return {
      formattedValue: value,
      selectionStart: selectionStart
    };
  }
  var currentSelection = selectionStart || 0;
  var dateRegExp = new RegExp("[^0-9".concat(delimiters.join(''), "]"), 'g');
  var formattedValue = value.replace(dateRegExp, '');
  if (formattedValue.length >= format.length) {
    return {
      formattedValue: formattedValue.slice(0, format.length),
      selectionStart: selectionStart
    };
  }
  if (prevValue && formattedValue.length < prevValue.length) {
    if (delimiters.includes(format[currentSelection])) {
      return {
        formattedValue: formattedValue.slice(0, currentSelection - 1),
        selectionStart: currentSelection - 1
      };
    }
  }
  if (delimiters.includes(format[currentSelection])) {
    return {
      formattedValue: formattedValue + format[currentSelection],
      selectionStart: currentSelection + 1
    };
  }
  return {
    formattedValue: formattedValue,
    selectionStart: currentSelection
  };
};

exports.getMaskedValue = getMaskedValue;
