'use strict';

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

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var CodeInput_tokens = require('../CodeInput.tokens.js');
var constants = require('../../../utils/constants.js');
var initialValuesHelper = require('../../../utils/initialValuesHelper.js');

var handleCodeError = function handleCodeError(_ref) {
  var _captionRef$current, _captionRef$current2;
  var codeLength = _ref.codeLength,
    codeErrorBehavior = _ref.codeErrorBehavior,
    currentCode = _ref.currentCode,
    inputRefs = _ref.inputRefs,
    inputContainerRef = _ref.inputContainerRef,
    captionRef = _ref.captionRef,
    setCode = _ref.setCode,
    codeSetter = _ref.codeSetter;
  if (!inputContainerRef.current) {
    return;
  }
  switch (codeErrorBehavior) {
    case 'keep':
      inputContainerRef.current.classList.add(CodeInput_tokens.classes.codeError, CodeInput_tokens.classes.codeErrorAnimation);
      (_captionRef$current = captionRef.current) === null || _captionRef$current === void 0 || _captionRef$current.classList.add(CodeInput_tokens.classes.captionError);
      setTimeout(function () {
        var _inputContainerRef$cu;
        var newCode = _rollupPluginBabelHelpers.toConsumableArray(currentCode);
        newCode.pop();
        codeSetter(newCode);
        setCode(currentCode);
        (_inputContainerRef$cu = inputContainerRef.current) === null || _inputContainerRef$cu === void 0 || _inputContainerRef$cu.classList.remove(CodeInput_tokens.classes.codeErrorAnimation);
        setTimeout(function () {
          var _inputRefs$current, _inputRefs$current2;
          (_inputRefs$current = inputRefs.current[inputRefs.current.length - 1]) === null || _inputRefs$current === void 0 || _inputRefs$current.setSelectionRange(0, 1);
          (_inputRefs$current2 = inputRefs.current[inputRefs.current.length - 1]) === null || _inputRefs$current2 === void 0 || _inputRefs$current2.focus();
        }, 0);
      }, constants.ANIMATION_TIMEOUT);
      break;
    case 'remove-code':
    default:
      inputContainerRef.current.classList.add(CodeInput_tokens.classes.codeError, CodeInput_tokens.classes.codeErrorAnimation, CodeInput_tokens.classes.codeErrorFade);
      (_captionRef$current2 = captionRef.current) === null || _captionRef$current2 === void 0 || _captionRef$current2.classList.add(CodeInput_tokens.classes.captionError);
      setTimeout(function () {
        var _inputRefs$current$, _inputContainerRef$cu2;
        codeSetter(initialValuesHelper.getCodeValue(codeLength, ''));
        (_inputRefs$current$ = inputRefs.current[0]) === null || _inputRefs$current$ === void 0 || _inputRefs$current$.focus();
        (_inputContainerRef$cu2 = inputContainerRef.current) === null || _inputContainerRef$cu2 === void 0 || _inputContainerRef$cu2.classList.remove(CodeInput_tokens.classes.codeError, CodeInput_tokens.classes.codeErrorAnimation, CodeInput_tokens.classes.codeErrorFade);
      }, constants.ANIMATION_TIMEOUT);
  }
};

exports.handleCodeError = handleCodeError;
