var delimiter = ':';
var normalizeTimeSegment = function normalizeTimeSegment(type, input) {
  var max = type === 'HH' ? 23 : 59;
  var digits = input.replace(/\D/g, '');
  var num = parseInt(digits, 10);
  if (!digits) {
    return {
      text: '',
      value: null,
      carryover: ''
    };
  }
  if (Math.floor(max / 10) < num && digits.length === 1) {
    return {
      text: num.toString().padStart(2, '0'),
      value: num,
      carryover: ''
    };
  }
  if (num <= max && digits.length === 1) {
    return {
      text: digits,
      value: num,
      carryover: ''
    };
  }
  if (digits.length === 1) {
    return {
      text: max.toString().padStart(2, '0'),
      value: max,
      carryover: ''
    };
  }
  if (Number.isNaN(num)) {
    return {
      text: '',
      value: null,
      carryover: ''
    };
  }
  if (num <= max) {
    return {
      text: digits.padStart(2, '0'),
      value: num,
      carryover: ''
    };
  }
  var firstDigit = parseInt(digits[0], 10);
  var secondDigit = parseInt(digits[1], 10);
  if (firstDigit <= max) {
    return {
      text: firstDigit.toString().padStart(2, '0'),
      value: firstDigit,
      carryover: secondDigit.toString()
    };
  }
  return {
    text: max.toString().padStart(2, '0'),
    value: max,
    carryover: digits.slice(1)
  };
};
var processTimeInput = function processTimeInput(input, format, cursorPosition) {
  var digits = input.replace(/\D/g, '');
  var tokens = format.split(delimiter);
  var values = {
    hh: null,
    mm: null,
    ss: null
  };
  var segments = [];
  var digitIndex = 0;
  var newCursorPosition = cursorPosition !== null && cursorPosition !== void 0 ? cursorPosition : 0;
  tokens.forEach(function (token) {
    var segmentDigits = digits.slice(digitIndex, digitIndex + 2);
    digitIndex += segmentDigits.length;
    var _normalizeTimeSegment = normalizeTimeSegment(token, segmentDigits),
      text = _normalizeTimeSegment.text,
      value = _normalizeTimeSegment.value;
    values[token.toLowerCase()] = value;
    segments.push(text);
  });
  var innerString = segments[0];
  if (segments.length > 1 && segments[1]) {
    innerString += ":".concat(segments[1]);
  }
  if (format === 'HH:mm:ss' && segments.length > 2 && segments[2]) {
    innerString += ":".concat(segments[2]);
  }
  if (input.length > innerString.length) {
    newCursorPosition = Math.min(newCursorPosition, innerString.length);
  } else if (innerString.length - 2 <= newCursorPosition) {
    newCursorPosition = innerString.length;
  }
  return {
    innerString: innerString,
    values: values,
    newCursorPosition: newCursorPosition
  };
};

export { delimiter, normalizeTimeSegment, processTimeInput };
