"use strict";const S=require("../input/input.cjs"),j=require("react/jsx-runtime"),s=require("react"),l=require("../tools/tools.cjs"),O=function(v){const{as:I="div",className:g,defaultValue:y,dropOldClass:C,inputProps:a,length:K=4,maxLength:f=1,style:h,variables:m,...A}=v,[c,D]=s.useState(()=>Array.from({length:K}).map((e,t)=>({id:l.generateRandomId(),value:l.isArray(y)?String(y[t]):""}))),r=s.useRef(new Map),R=s.useRef(!1);s.useEffect(()=>{if(R.current){const e=r.current;return()=>{e.clear()}}else R.current=!0},[]);const q=s.useMemo(()=>{const e=l.classx(!C&&"d-flex gap-4",g),t=l.stylex((n,u)=>({tKey:l.convertBsKeyToVar(u)}),m,h);return{className:e,style:t}},[g,C,h,m]),o=s.useCallback(e=>{var t;(t=r.current.get(c[e].id))==null||t.focus()},[c]),k=s.useCallback((e,t)=>{D(n=>{const u=n.map((w,B)=>B===e?{...w,value:t}:w);return t.length===f&&t&&e<u.length-1&&o(e+1),u})},[o,f]),b=s.useCallback((e,t)=>{const n=t.key;n==="Backspace"&&!c[e].value&&e>0&&o(e-1),(n==="ArrowLeft"&&e>0||n==="ArrowRight"&&e<c.length-1)&&(o(n==="ArrowLeft"?e-1:e+1),t.preventDefault())},[c,o]),d=s.useCallback(e=>{var t;(t=r.current.get(c[e].id))==null||t.select()},[c]),F=s.useCallback((e,t)=>{var n;r.current.set(t.id,e),(n=a==null?void 0:a.onRef)==null||n.call(a,e)},[a]),L=s.useCallback((e,t)=>{var n;k(t,e.target.value),(n=a==null?void 0:a.onChange)==null||n.call(a,e)},[k,a]),N=s.useCallback((e,t)=>{var n;b(t,e),(n=a==null?void 0:a.onKeyDown)==null||n.call(a,e)},[b,a]),_=s.useCallback((e,t)=>{var n;d(t),(n=a==null?void 0:a.onFocus)==null||n.call(a,e)},[d,a]);return j.jsx(I,{...A,...q,children:c.map((e,t)=>s.createElement(S.Input,{className:"text-center",...l.mergeProps(a,{onChange:n=>L(n,t),onFocus:n=>_(n,t),onKeyDown:n=>N(n,t),onRef:n=>F(n,e)}),key:e.id,maxLength:f,value:e.value}))})};O.displayName="BRL.InputOtp";exports.InputOtp=O;
//# sourceMappingURL=input-otp.cjs.map
