{"version":3,"file":"input-otp-B9zfjb5u.cjs","names":[],"sources":["../src/components/input-otp/component.tsx"],"sourcesContent":["import { ErrorMessage } from '@local/components/error';\nimport { Input } from '@local/components/input';\nimport { CSS_VARS } from '@local/styles/utils';\n\nimport { ClipboardEvent, FocusEvent, KeyboardEvent, useCallback, useRef, useState } from 'react';\n\nimport { IInputOTP } from './component.types';\n\nexport const InputOTP = (props: IInputOTP) => {\n  const [otp, setOtp] = useState<string[]>(new Array(props.length).fill(''));\n  const inputsRef = useRef<(HTMLInputElement | null)[]>([]);\n  const wrapperRef = useRef<HTMLDivElement | null>(null);\n\n  const handlePaste = useCallback(\n    (index: number, e: ClipboardEvent<HTMLInputElement>) => {\n      e.preventDefault();\n\n      const pasteData = e.clipboardData.getData('Text');\n      const digits = pasteData.replace(/\\D/g, '').split('');\n\n      if (!digits.length) return;\n\n      setOtp((prevOtp) => {\n        const newOtp = [...prevOtp];\n        let currentIndex = index;\n\n        for (let i = 0; i < digits.length && currentIndex < newOtp.length; i++) {\n          newOtp[currentIndex] = digits[i];\n          currentIndex++;\n        }\n\n        const joined = newOtp.join('');\n\n        props.onChange?.(joined);\n\n        if (newOtp.every((char) => char !== '')) {\n          props.onComplete?.(joined);\n        }\n\n        setTimeout(() => {\n          const firstEmpty = newOtp.indexOf('');\n          if (firstEmpty !== -1) {\n            inputsRef.current[firstEmpty]?.focus();\n          } else if (currentIndex < newOtp.length) {\n            inputsRef.current[currentIndex]?.focus();\n          }\n        }, 0);\n\n        return newOtp;\n      });\n    },\n    [props.onChange, props.onComplete],\n  );\n\n  const handleChange = useCallback(\n    (index: number, value: string) => {\n      if (!/^\\d*$/.test(value)) return;\n\n      setOtp((prevOtp) => {\n        const newOtp = [...prevOtp];\n        newOtp[index] = value.slice(-1);\n\n        if (props.onChange) {\n          props.onChange(newOtp.join(''));\n        }\n\n        if (newOtp.every((char) => char !== '')) {\n          props.onComplete?.(newOtp.join(''));\n        }\n\n        if (value && index < prevOtp.length - 1) {\n          setTimeout(() => {\n            inputsRef.current[index + 1]?.focus();\n          }, 0);\n        }\n\n        return newOtp;\n      });\n    },\n    [props.onChange, props.onComplete],\n  );\n\n  const handleKeyDown = (index: number, e: KeyboardEvent<HTMLInputElement>) => {\n    const input = inputsRef.current[index];\n\n    if (!input) return;\n\n    if (e.key === 'ArrowRight') {\n      const nextIndex = index < otp.length - 1 ? index + 1 : 0;\n      const nextInput = inputsRef.current[nextIndex];\n\n      if (nextInput) {\n        nextInput.focus();\n        setTimeout(() => nextInput.setSelectionRange(0, nextInput.value.length), 0);\n      }\n    }\n\n    if (e.key === 'ArrowLeft') {\n      const prevIndex = index > 0 ? index - 1 : otp.length - 1;\n      const prevInput = inputsRef.current[prevIndex];\n\n      if (prevInput) {\n        prevInput.focus();\n        setTimeout(() => prevInput.setSelectionRange(0, prevInput.value.length), 0);\n      }\n    }\n\n    if (e.key === 'Backspace') {\n      if (input.value) {\n        setTimeout(() => input.setSelectionRange(0, input.value.length), 0);\n      } else if (index > 0) {\n        setTimeout(() => {\n          const prevInput = inputsRef.current[index - 1];\n          if (prevInput) {\n            prevInput.focus();\n            prevInput.setSelectionRange(0, prevInput.value.length);\n          }\n        }, 0);\n      }\n    }\n  };\n\n  const handleFocusInput = useCallback((e: FocusEvent<HTMLInputElement>) => {\n    setTimeout(() => e.target.setSelectionRange(0, e.target.value.length), 0);\n  }, []);\n  return (\n    <>\n      <div\n        className={props.className}\n        style={{\n          gap: `calc(${CSS_VARS.size[props.size].padding} - 2px)`,\n          width: '100%',\n          display: 'flex',\n          alignItems: 'flex-start',\n          ...props.style,\n        }}\n        id={props.id}\n        ref={wrapperRef}\n        onBlur={(e) => {\n          setTimeout(() => {\n            if (wrapperRef.current && !wrapperRef.current.contains(document.activeElement)) {\n              props.onBlur?.(e);\n            }\n          }, 0);\n        }}\n        onFocus={(e) => {\n          setTimeout(() => {\n            if (wrapperRef.current?.contains(document.activeElement)) {\n              props.onFocus?.(e);\n            }\n          }, 0);\n        }}\n      >\n        {otp.map((digit, index) => (\n          <Input\n            isWidthAsHeight\n            key={index}\n            tabIndex={index + 1}\n            ref={(el) => {\n              inputsRef.current[index] = el;\n            }}\n            variety='standard'\n            type='text'\n            inputMode='numeric'\n            maxLength={1}\n            value={digit}\n            onFocus={handleFocusInput}\n            onPaste={(e) => handlePaste(index, e)}\n            onChange={(value) => handleChange(index, value)}\n            onKeyDown={(e) => handleKeyDown(index, e)}\n            genre={props.genre}\n            size={props.size}\n            sxTypography={props.sxTypography}\n            isBold={props.isBold}\n            isFullRadius={props.isFullRadius}\n            isDisabled={props.isDisabled}\n            isHidden={props.isHidden}\n            isHiddenBorder={props.isHiddenBorder}\n          />\n        ))}\n      </div>\n      {props?.error?.isError && (\n        <ErrorMessage\n          size={props?.error.size ?? props.size}\n          sxTypography={{ size: '16px', weight: '400', ...props?.error.sxTypography }}\n          {...props.error}\n        />\n      )}\n    </>\n  );\n};\n"],"mappings":"oMAQA,IAAa,EAAY,GAAqB,CAC5C,KAAM,CAAC,EAAK,CAAA,KAAA,EAAA,UAA6B,IAAI,MAAM,EAAM,MAAA,EAAQ,KAAK,EAAA,CAAG,EACnE,KAAA,EAAA,QAAgD,CAAA,CAAE,EAClD,KAAA,EAAA,QAA2C,IAAA,EAE3C,KAAA,EAAA,aAAA,CACH,EAAe,IAAwC,CACtD,EAAE,eAAA,EAGF,MAAM,EADY,EAAE,cAAc,QAAQ,MAAA,EACjB,QAAQ,MAAO,EAAA,EAAI,MAAM,EAAA,EAE7C,EAAO,QAEZ,EAAQ,GAAY,CAClB,MAAM,EAAS,CAAC,GAAG,CAAA,EACnB,IAAI,EAAe,EAEnB,QAAS,EAAI,EAAG,EAAI,EAAO,QAAU,EAAe,EAAO,OAAQ,IACjE,EAAO,CAAA,EAAgB,EAAO,CAAA,EAC9B,IAGF,MAAM,EAAS,EAAO,KAAK,EAAA,EAE3B,OAAA,EAAM,WAAW,CAAA,EAEb,EAAO,MAAO,GAAS,IAAS,EAAA,GAClC,EAAM,aAAa,CAA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