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);
}
import { classes } from "../CodeField.tokens";
import { ANIMATION_TIMEOUT } from "./constants";
export var handleItemError = function handleItemError(param) {
    var currentSymbol = param.currentSymbol, itemErrorBehavior = param.itemErrorBehavior, index = param.index, newCode = param.newCode, inputRefs = param.inputRefs, setInnerValue = param.setInnerValue, codeSetter = param.codeSetter, onChange = param.onChange;
    if (!inputRefs.current[index] || currentSymbol === ' ') {
        return;
    }
    switch(itemErrorBehavior){
        case 'keep':
            var _inputRefs_current_index;
            setInnerValue(newCode);
            if (onChange) {
                onChange(newCode.join(''));
            }
            (_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.classList.add(classes.itemError, classes.itemErrorAnimation);
            setTimeout(function() {
                var _inputRefs_current_index;
                (_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.classList.remove(classes.itemErrorAnimation);
                setTimeout(function() {
                    var _inputRefs_current_index;
                    return (_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.setSelectionRange(0, 1);
                }, 0);
            }, ANIMATION_TIMEOUT);
            break;
        case 'forbid-enter':
            newCode[index] = '';
            codeSetter(newCode);
            if (onChange) {
                onChange(newCode.join(''));
            }
            break;
        case 'remove-symbol':
        default:
            var _inputRefs_current_index1;
            setInnerValue(newCode);
            if (onChange) {
                onChange(newCode.join(''));
            }
            (_inputRefs_current_index1 = inputRefs.current[index]) === null || _inputRefs_current_index1 === void 0 ? void 0 : _inputRefs_current_index1.classList.add(classes.itemError, classes.itemErrorFade, classes.itemErrorAnimation);
            setTimeout(function() {
                var _inputRefs_current_index;
                var updatedCode = _to_consumable_array(newCode);
                updatedCode[index] = '';
                codeSetter(updatedCode);
                (_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.classList.remove(classes.itemError, classes.itemErrorFade, classes.itemErrorAnimation);
            }, ANIMATION_TIMEOUT);
    }
};
