"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "handleCodeError", {
    enumerable: true,
    get: function() {
        return handleCodeError;
    }
});
const _CodeInputtokens = require("../CodeInput.tokens");
const _utils = require("../../../utils");
const _constants = require("../../../utils/constants");
const handleCodeError = ({ codeLength, codeErrorBehavior, currentCode, inputRefs, inputContainerRef, captionRef, setCode, codeSetter })=>{
    if (!inputContainerRef.current) {
        return;
    }
    switch(codeErrorBehavior){
        case 'keep':
            inputContainerRef.current.classList.add(_CodeInputtokens.classes.codeError, _CodeInputtokens.classes.codeErrorAnimation);
            captionRef.current?.classList.add(_CodeInputtokens.classes.captionError);
            setTimeout(()=>{
                const newCode = [
                    ...currentCode
                ];
                newCode.pop();
                codeSetter(newCode);
                setCode(currentCode);
                inputContainerRef.current?.classList.remove(_CodeInputtokens.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(_CodeInputtokens.classes.codeError, _CodeInputtokens.classes.codeErrorAnimation, _CodeInputtokens.classes.codeErrorFade);
            captionRef.current?.classList.add(_CodeInputtokens.classes.captionError);
            setTimeout(()=>{
                codeSetter((0, _utils.getCodeValue)(codeLength, ''));
                inputRefs.current[0]?.focus();
                inputContainerRef.current?.classList.remove(_CodeInputtokens.classes.codeError, _CodeInputtokens.classes.codeErrorAnimation, _CodeInputtokens.classes.codeErrorFade);
            }, _constants.ANIMATION_TIMEOUT);
    }
};
