"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "handleCodeError", {
    enumerable: true,
    get: function() {
        return handleCodeError;
    }
});
const _CodeFieldtokens = require("../CodeField.tokens");
const _constants = require("./constants");
const _initialValuesHelper = require("./initialValuesHelper");
const handleCodeError = ({ codeLength, codeErrorBehavior, currentCode, inputRefs, inputContainerRef, captionRef, setInnerValue, codeSetter })=>{
    if (!inputContainerRef.current) {
        return;
    }
    switch(codeErrorBehavior){
        case 'keep':
            inputContainerRef.current.classList.add(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation);
            captionRef.current?.classList.add(_CodeFieldtokens.classes.captionError);
            setTimeout(()=>{
                const newCode = [
                    ...currentCode
                ];
                newCode.pop();
                codeSetter(newCode);
                setInnerValue(currentCode);
                inputContainerRef.current?.classList.remove(_CodeFieldtokens.classes.codeErrorAnimation);
                setTimeout(()=>{
                    inputRefs.current[inputRefs.current.length - 1]?.setSelectionRange(0, 1);
                    inputRefs.current[inputRefs.current.length - 1]?.focus();
                }, 0);
            }, _constants.ANIMATION_TIMEOUT);
            break;
        case 'remove-code':
        default:
            inputContainerRef.current.classList.add(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation, _CodeFieldtokens.classes.codeErrorFade);
            captionRef.current?.classList.add(_CodeFieldtokens.classes.captionError);
            setTimeout(()=>{
                codeSetter((0, _initialValuesHelper.getCodeValue)(codeLength, ''));
                inputRefs.current[0]?.focus();
                inputContainerRef.current?.classList.remove(_CodeFieldtokens.classes.codeError, _CodeFieldtokens.classes.codeErrorAnimation, _CodeFieldtokens.classes.codeErrorFade);
            }, _constants.ANIMATION_TIMEOUT);
    }
};
