"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var w=require("react/jsx-runtime"),R=require("./text-input.js"),E=require("../../lib/cn.js"),f=require("react");const h=/\D/g,x=({pinLength:u,onComplete:$=c=>c,onChange:m=c=>c,id:v="pin-code-input",autoFocus:C=!0,className:j})=>{const[c,y]=f.useState(()=>Array.from({length:u},()=>"")),l=f.useRef(Array(u).fill(null));f.useEffect(()=>{y(e=>u<e.length?e.slice(0,u):u>e.length?[...e,...Array(u-e.length).fill("")]:e)},[u]),f.useEffect(()=>{var e;C&&l.current[0]&&((e=l.current[0])===null||e===void 0||e.focus())},[C,u]);const d=(e,r)=>{var t;y(r),m(r);const n=r.join("");n.length===u&&((t=l.current[e])===null||t===void 0||t.blur(),$(n))},k=f.useCallback((e,r)=>{const t=[...c];r.replace(h,"").split("").forEach(n=>{var a,o;e>=u||(t[e]=n,e++,(o=(a=l?.current)===null||a===void 0?void 0:a[e])===null||o===void 0||o.focus())}),d(e,t)},[c,d]),I=f.useCallback(e=>{var r,t;const n=parseInt(e.currentTarget.dataset.id||"",10),a=e.target.value.replace(h,"");if(a.length>1)return k(n,a);const o=[...c];o[n]=a,a.length&&((t=(r=l?.current)===null||r===void 0?void 0:r[n+1])===null||t===void 0||t.focus()),d(n,o)},[d]),A=f.useCallback(e=>{var r,t,n,a,o;const b=e.currentTarget.value.length,i=parseInt(e.currentTarget.dataset.id||"",10),p=[...c];switch(e.key){case"Backspace":{if(b&&(p[i]="",d(i,p)),i){const s=p.reduceRight((g,T,q)=>g!==-1?g:T!==""?q:g,-1);(r=l?.current[s>=0?s:0])===null||r===void 0||r.focus()}return}case"ArrowLeft":{const s=i?i-1:u-1;(t=l?.current[s])===null||t===void 0||t.focus();return}case"ArrowRight":{const s=i===u-1?0:i+1;(n=l?.current[s])===null||n===void 0||n.focus();return}default:{const s=e.key.replace(h,"");b&&s.length&&s!==e.currentTarget.value&&(p[i]=s,d(i,p),(o=(a=l?.current)===null||a===void 0?void 0:a[i+1])===null||o===void 0||o.focus())}}},[m,d]);return w.jsx("form",{id:v,className:"m-0 min-w-0 flex w-full justify-between max-w-[30rem] gap-2",children:c.map((e,r)=>w.jsx(R.TextInput.Char,{id:v?`${v}-${r}`:void 0,"aria-label":`one time password input ${r+1}`,"data-id":r,name:`one time password input ${r+1}`,value:c[r]||"",className:E.cn("caret-transparent rounded-[0.5rem]",u===6&&r===2?"mr-4":"mr-0",j),attr:{onChange:I,onKeyUp:A,autoComplete:r===0?"one-time-code":"off",inputMode:"numeric"},ref:t=>{l?.current&&(l.current[r]=t)}},`${v||void 0}-${r}`))})};exports.PinCodeInput=x,exports.default=x;
//# sourceMappingURL=pincode-input.js.map
