import React, { forwardRef, Fragment, useCallback, useRef, useState } from "react";
import cls from "classnames";
import { useDidMountEffect, useCodeHook } from "../../hooks";
import { getSizeValueFromProp } from "../../utils";
import { BACKSPACE_KEY, FORBIDDEN_KEYS, ONLY_DIGITS_PATTERN } from "../../utils/constants";
import { getCodeValue, getFieldPattern, getPlaceholderValue, handleCodeError, handleItemError } from "./utils";
import { classes } from "./CodeInput.tokens";
import { base, CaptionWrapper, CodeGroup, CodeWrapper, ItemInput, Separator, ItemWrapper, ItemCircle } from "./CodeInput.styles";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
export const codeInputRoot = (Root)=>/*#__PURE__*/ forwardRef(({ className, placeholder, autoFocus, codeLength = 6, caption, captionAlign = 'left', width, view, size, hidden, disabled, isError, allowedSymbols = ONLY_DIGITS_PATTERN, itemErrorBehavior = 'remove-symbol', codeErrorBehavior = 'remove-code', autoComplete = 'one-time-code', inputMode, setIsError, onChange, onFullCodeEnter, ...rest }, ref)=>{
        const [code, setCode] = useState(getCodeValue(codeLength, ''));
        const [originalValue, setOriginalValue] = useState(code.join(''));
        const inputRefs = useRef([]);
        const inputContainerRef = useRef(null);
        const captionRef = useRef(null);
        const fieldPattern = getFieldPattern(allowedSymbols);
        const placeholderValue = getPlaceholderValue(codeLength, placeholder);
        const parts = codeLength === 6 ? 2 : 1;
        const widthValue = width ? getSizeValueFromProp(width, 'rem') : undefined;
        const getLastActiveIndex = ()=>{
            if (code.length && code.length < codeLength) {
                return code.length;
            }
            const lastEmptyIndex = code.findIndex((digit)=>digit === '');
            return lastEmptyIndex >= 0 ? lastEmptyIndex : codeLength - 1;
        };
        const handleClick = ()=>{
            if (disabled) {
                return;
            }
            const lastActiveIndex = getLastActiveIndex();
            if (inputRefs.current[lastActiveIndex]) {
                inputRefs.current[lastActiveIndex].focus();
            }
        };
        const handleOnKeyDown = (event, index, type)=>{
            if (disabled) {
                return;
            }
            const { key } = event;
            if (FORBIDDEN_KEYS.includes(key)) {
                event.preventDefault();
                return;
            }
            if (key === BACKSPACE_KEY) {
                if (index > 0 && code[index] === '') {
                    inputRefs.current[index - 1]?.focus();
                }
            }
            if (type === 'circle') {
                event.preventDefault();
                handleAddSymbol(key, index);
                inputRefs.current[index]?.focus();
            }
        };
        const codeSetter = (newCode)=>{
            setCode(newCode);
            setOriginalValue(newCode.join(''));
            if (onChange) {
                onChange(newCode.join(''));
            }
        };
        const handleChange = (event, index)=>{
            if (disabled) {
                return;
            }
            const rawSymbol = event.currentTarget.value;
            // На мобильных устройствах вставка вызывает onChange по 1 символу
            if (rawSymbol.length > 1) {
                const newCode = [
                    ...code
                ];
                const pastedData = (fieldPattern ? rawSymbol.split('').map((symb)=>symb.match(fieldPattern)?.[0] || '').filter(Boolean) : rawSymbol.split('')).slice(0, codeLength - index);
                pastedData.forEach((element, i)=>{
                    newCode[index + i] = element;
                });
                const activeIndex = Math.min(index + pastedData.length, codeLength - 1);
                inputRefs.current[activeIndex]?.focus();
                codeSetter(newCode);
                return;
            }
            const symbol = rawSymbol.charAt(rawSymbol.length - 1);
            handleAddSymbol(symbol, index);
        };
        const handleAddSymbol = (symbol, index)=>{
            const newCode = [
                ...code
            ];
            inputRefs.current[index]?.classList.remove(classes.itemError);
            if (isError) {
                captionRef.current?.classList.remove(classes.captionError);
                if (codeErrorBehavior === 'keep') {
                    inputContainerRef.current?.classList.remove(classes.codeError, classes.codeErrorAnimation);
                }
                if (setIsError) {
                    setIsError(false);
                }
            }
            if (!fieldPattern) {
                newCode[index] = symbol;
                codeSetter(newCode);
                if (symbol && index < codeLength - 1) {
                    inputRefs.current[index + 1]?.focus();
                }
                return;
            }
            if (!symbol) {
                newCode[index] = '';
                codeSetter(newCode);
                return;
            }
            if (fieldPattern.test(symbol)) {
                newCode[index] = symbol.charAt(symbol.length - 1);
                codeSetter(newCode);
                if (index < codeLength - 1) {
                    inputRefs.current[index + 1]?.focus();
                }
            } else {
                newCode[index] = symbol.charAt(symbol.length - 1);
                handleItemError({
                    currentSymbol: symbol,
                    itemErrorBehavior,
                    index,
                    newCode,
                    inputRefs,
                    inputContainerRef,
                    codeSetter
                });
            }
        };
        const handlePaste = (event)=>{
            const newCode = [
                ...code
            ];
            if (disabled) {
                return;
            }
            event.preventDefault();
            const rawData = event.clipboardData.getData('text/plain');
            const pastedData = (fieldPattern ? rawData.split('').map((symb)=>symb.match(fieldPattern)?.[0] || '').filter(Boolean) || [] : rawData.split('')).slice(0, codeLength);
            pastedData.forEach((element, index)=>{
                newCode[index] = element;
            });
            const activeIndex = Math.min(pastedData.length, codeLength - 1);
            inputRefs.current[activeIndex]?.focus();
            codeSetter(newCode);
        };
        const handleFullCodeEnter = useCallback((fullCode)=>{
            if (onFullCodeEnter) {
                onFullCodeEnter(fullCode);
            }
        }, []);
        useCodeHook({
            inputRefs,
            codeLength,
            disabled,
            autoFocus,
            originalValue,
            getLastActiveIndex,
            handleFullCodeEnter
        });
        useDidMountEffect(()=>{
            if (isError) {
                handleCodeError({
                    codeLength,
                    codeErrorBehavior,
                    currentCode: code,
                    inputRefs,
                    inputContainerRef,
                    captionRef,
                    setCode,
                    codeSetter
                });
            }
        }, [
            isError
        ]);
        return /*#__PURE__*/ React.createElement(Root, {
            ref: ref,
            view: view,
            size: size,
            disabled: disabled,
            onClick: handleClick,
            className: cls(className, {
                [classes.captionAlignLeft]: captionAlign === 'left'
            }),
            ...rest
        }, /*#__PURE__*/ React.createElement(CodeWrapper, {
            ref: inputContainerRef
        }, [
            ...Array(parts)
        ].map((_, partIndex)=>/*#__PURE__*/ React.createElement(Fragment, {
                key: partIndex
            }, /*#__PURE__*/ React.createElement(CodeGroup, {
                role: "group"
            }, [
                ...Array(codeLength / parts)
            ].map((_, i)=>{
                const inputCorrectIndex = i + codeLength / parts * partIndex;
                return /*#__PURE__*/ React.createElement(ItemWrapper, {
                    key: String(partIndex + i + partIndex * i)
                }, (!code[inputCorrectIndex] || Boolean(code[inputCorrectIndex]) && hidden) && /*#__PURE__*/ React.createElement(ItemCircle, {
                    role: "tab",
                    tabIndex: originalValue.length === inputCorrectIndex ? 0 : -1,
                    onKeyDown: (e)=>{
                        handleOnKeyDown(e, inputCorrectIndex, 'circle');
                    },
                    className: cls([
                        Boolean(code[inputCorrectIndex]) && hidden ? classes.itemCirlceFilled : '',
                        `${classes.itemCircle}-${inputCorrectIndex}`
                    ])
                }), /*#__PURE__*/ React.createElement(ItemInput, {
                    ref: (element)=>{
                        inputRefs.current[inputCorrectIndex] = element;
                    },
                    className: cls({
                        [classes.hoverEnabled]: inputCorrectIndex >= originalValue.length
                    }),
                    hide: hidden,
                    inputMode: inputMode,
                    value: code[inputCorrectIndex] || '',
                    tabIndex: originalValue.length === inputCorrectIndex ? 0 : -1,
                    autoComplete: autoComplete,
                    onChange: (e)=>{
                        handleChange(e, inputCorrectIndex);
                    },
                    onKeyDown: (e)=>{
                        handleOnKeyDown(e, inputCorrectIndex);
                    },
                    onPaste: handlePaste,
                    ...placeholderValue && {
                        placeholder: placeholderValue[inputCorrectIndex]
                    }
                }));
            })), partIndex !== parts - 1 && /*#__PURE__*/ React.createElement(Separator, null)))), caption && /*#__PURE__*/ React.createElement(CaptionWrapper, {
            ref: captionRef,
            captionAlign: captionAlign,
            widthValue: widthValue
        }, caption));
    });
export const codeInputConfig = {
    name: 'CodeInput',
    tag: 'div',
    layout: codeInputRoot,
    base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default'
    }
};
