"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "handleCodeError", {
    enumerable: true,
    get: function() {
        return handleCodeError;
    }
});
var _CodeFieldtokens = require("../CodeField.tokens");
var _constants = require("./constants");
var _initialValuesHelper = require("./initialValuesHelper");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var handleCodeError = function handleCodeError(param) {
    var codeLength = param.codeLength, codeErrorBehavior = param.codeErrorBehavior, currentCode = param.currentCode, inputRefs = param.inputRefs, inputContainerRef = param.inputContainerRef, captionRef = param.captionRef, setInnerValue = param.setInnerValue, codeSetter = param.codeSetter;
    if (!inputContainerRef.current) {
        return;
    }
    switch(codeErrorBehavior){
        case 'keep':
            var _captionRef_current;
            inputContainerRef.current.classList.add(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation);
            (_captionRef_current = captionRef.current) === null || _captionRef_current === void 0 ? void 0 : _captionRef_current.classList.add(_CodeFieldtokens.classes.captionError);
            setTimeout(function() {
                var _inputContainerRef_current;
                var newCode = _to_consumable_array(currentCode);
                newCode.pop();
                codeSetter(newCode);
                setInnerValue(currentCode);
                (_inputContainerRef_current = inputContainerRef.current) === null || _inputContainerRef_current === void 0 ? void 0 : _inputContainerRef_current.classList.remove(_CodeFieldtokens.classes.codeErrorAnimation);
                setTimeout(function() {
                    var _inputRefs_current_, _inputRefs_current_1;
                    (_inputRefs_current_ = inputRefs.current[inputRefs.current.length - 1]) === null || _inputRefs_current_ === void 0 ? void 0 : _inputRefs_current_.setSelectionRange(0, 1);
                    (_inputRefs_current_1 = inputRefs.current[inputRefs.current.length - 1]) === null || _inputRefs_current_1 === void 0 ? void 0 : _inputRefs_current_1.focus();
                }, 0);
            }, _constants.ANIMATION_TIMEOUT);
            break;
        case 'remove-code':
        default:
            var _captionRef_current1;
            inputContainerRef.current.classList.add(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation, _CodeFieldtokens.classes.codeErrorFade);
            (_captionRef_current1 = captionRef.current) === null || _captionRef_current1 === void 0 ? void 0 : _captionRef_current1.classList.add(_CodeFieldtokens.classes.captionError);
            setTimeout(function() {
                var _inputRefs_current_, _inputContainerRef_current;
                codeSetter((0, _initialValuesHelper.getCodeValue)(codeLength, ''));
                (_inputRefs_current_ = inputRefs.current[0]) === null || _inputRefs_current_ === void 0 ? void 0 : _inputRefs_current_.focus();
                (_inputContainerRef_current = inputContainerRef.current) === null || _inputContainerRef_current === void 0 ? void 0 : _inputContainerRef_current.classList.remove(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation, _CodeFieldtokens.classes.codeErrorFade);
            }, _constants.ANIMATION_TIMEOUT);
    }
};
