import { toConsumableArray as _toConsumableArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { classes } from '../CodeField.tokens.js';
import { ANIMATION_TIMEOUT } from './constants.js';

var handleItemError = function handleItemError(_ref) {
  var _inputRefs$current$in, _inputRefs$current$in4;
  var currentSymbol = _ref.currentSymbol,
    itemErrorBehavior = _ref.itemErrorBehavior,
    index = _ref.index,
    newCode = _ref.newCode,
    inputRefs = _ref.inputRefs,
    setInnerValue = _ref.setInnerValue,
    codeSetter = _ref.codeSetter,
    onChange = _ref.onChange;
  if (!inputRefs.current[index] || currentSymbol === ' ') {
    return;
  }
  switch (itemErrorBehavior) {
    case 'keep':
      setInnerValue(newCode);
      if (onChange) {
        onChange(newCode.join(''));
      }
      (_inputRefs$current$in = inputRefs.current[index]) === null || _inputRefs$current$in === void 0 || _inputRefs$current$in.classList.add(classes.itemError, classes.itemErrorAnimation);
      setTimeout(function () {
        var _inputRefs$current$in2;
        (_inputRefs$current$in2 = inputRefs.current[index]) === null || _inputRefs$current$in2 === void 0 || _inputRefs$current$in2.classList.remove(classes.itemErrorAnimation);
        setTimeout(function () {
          var _inputRefs$current$in3;
          return (_inputRefs$current$in3 = inputRefs.current[index]) === null || _inputRefs$current$in3 === void 0 ? void 0 : _inputRefs$current$in3.setSelectionRange(0, 1);
        }, 0);
      }, ANIMATION_TIMEOUT);
      break;
    case 'forbid-enter':
      newCode[index] = '';
      codeSetter(newCode);
      if (onChange) {
        onChange(newCode.join(''));
      }
      break;
    case 'remove-symbol':
    default:
      setInnerValue(newCode);
      if (onChange) {
        onChange(newCode.join(''));
      }
      (_inputRefs$current$in4 = inputRefs.current[index]) === null || _inputRefs$current$in4 === void 0 || _inputRefs$current$in4.classList.add(classes.itemError, classes.itemErrorFade, classes.itemErrorAnimation);
      setTimeout(function () {
        var _inputRefs$current$in5;
        var updatedCode = _toConsumableArray(newCode);
        updatedCode[index] = '';
        codeSetter(updatedCode);
        (_inputRefs$current$in5 = inputRefs.current[index]) === null || _inputRefs$current$in5 === void 0 || _inputRefs$current$in5.classList.remove(classes.itemError, classes.itemErrorFade, classes.itemErrorAnimation);
      }, ANIMATION_TIMEOUT);
  }
};

export { handleItemError };
