(function(l,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(l=typeof globalThis<"u"?globalThis:l||self,b(l.TsNanoForm={}))})(this,function(l){"use strict";const b={thousands:".",decimal:",",precision:2,allowNegative:!1},$={map:new Map([["0",{pattern:/\d/}],["A",{pattern:/[a-zA-Z0-9]/}],["S",{pattern:/[A-Za-z]/}],["X",{pattern:/[A-Za-z]/,transform:(e,t)=>({prevValue:e,newChar:t.toLocaleUpperCase()})}],["x",{pattern:/[A-Za-z]/,transform:(e,t)=>({prevValue:e,newChar:t.toLocaleLowerCase()})}],["Z",{pattern:/[a-zA-Z0-9]/,transform:(e,t)=>({prevValue:e,newChar:t.toLocaleUpperCase()})}],["z",{pattern:/[a-zA-Z0-9]/,transform:(e,t)=>({prevValue:e,newChar:t.toLocaleLowerCase()})}]])},F=(e,t)=>!!N(t).reduce((n,s)=>n&&n[s],e),P=(e,t,n)=>N(t).reduce((s,r)=>s&&s[r],e)??n,z=(e,t,n)=>{N(t).reduce((s,r,c,f)=>(s[r]===void 0&&(s[r]=C(r)?{}:[]),c===f.length-1&&(s[r]=n),s[r]),e)},T=(e,t)=>!e||e==="0"?`0${t===0?"":".".padEnd(t+1,"0")}`:Number(e.match(new RegExp("^-?\\d+(?:.\\d{0,"+(t||-1)+"})?"))[0]).toFixed(t),N=e=>Array.isArray(e)?e:e.match(/([^[.\]])+/g)||[],x=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Z=e=>(e||"").replace(/[^0-9]/g,""),D=e=>typeof e=="object"&&e!==null,I=e=>e%1!=0,C=e=>(typeof e=="number"||typeof e=="string"&&e.trim()!=="")&&!isNaN(e),J=e=>JSON.parse(JSON.stringify(e)),g=e=>String(e||""),L=(e,t)=>{var n,s;return t.allowNegative&&((n=e.match(/-/g))==null?void 0:n.length)===1&&((s=e.match(/\+/g))==null?void 0:s.length)!==1?"-":""},K=(e,t,n)=>{if(n!=null&&n.transform){const{prevValue:s,newChar:r}=n.transform(t,e);return s+r}return t+e},Y=(e,t,n)=>{let s=0;return[...t].reduce((r,c)=>{const f=e[s];if(!f)return r;if(f===c)return++s,r+c;const a=n.map.get(c);return a?a.pattern.test(f)?(++s,K(f,r,a)):(s=-1,r):r+c},"")},B=(e,t)=>new RegExp(e===0?"(\\d{1,3})(?=(\\d{3})+(?!\\d))":`(\\d)(?=(\\d{3})+${x(t)})`,"g"),X=(e,t,n)=>`${t}${n.prefix||""}${e.replace(".",n.decimal).replace(B(n.precision,n.decimal),`$1${n.thousands}`)}`,R=(e,t)=>C(e)&&I(Number(e))?T(e,t):T(`${Number(Z(e))/+`1${"".padEnd(t,"0")}`}`,t),j=(e,t,n)=>Array.isArray(t)?t.sort((s,r)=>s.length-r.length).find(s=>k(s,n).length>=k(e,n).length)||t[t.length-1]||"":t,V=(e,t,n)=>{const s=n.beforeMask?n.beforeMask(e):e,r=Y(s,j(e,t,n),n);return n.afterMask?n.afterMask(r):r},k=(e,t)=>{const n=[...t.map.values()].map(s=>s.pattern);return e.replace(new RegExp(`${[...e].filter(s=>!n.find(r=>r.test(s))).map(s=>x(s)).join("|")}`,"g"),"")},h=(e,t)=>{const n=L(e,t),s=R(e,t.precision),r=t.beforeMask?t.beforeMask(s):s,c=X(r,n,t);return t.afterMask?t.afterMask(c):c},A=(e,t)=>e?`${L(e,t)}${R(e,t.precision)}`:"0",q=(e,t)=>[...e].reduce((n,s)=>t.map.get(s)?n+"_":n+s,""),E=e=>{let t=e,n=[];const s=a=>(n=[...n,a],()=>{n=n.filter(u=>u!==a)}),r=(a,u)=>{for(const p of n)p(a,u)};return{subscribe:s,emit:r,get:()=>t,set:a=>{const u=t;t=a,r(a,u)}}},_=(e,t)=>(F(e,t)||z(e,t,E()),P(e,t)),y=e=>typeof e=="object"&&"subscribe"in e,M=(e,t)=>{const n=J(e);if(!D(e)||y(e))return t(e);for(const s in e)n[s]=M(e[s],t);return n},U=e=>e?{...e,precision:!e.precision||e.precision<0?0:e.precision,decimal:e.decimal?e.decimal:".",allowNegative:!!(e!=null&&e.allowNegative)}:b,G=(e,t,n,s,r)=>{const c=_(t,e),f=_(n,e),a=()=>c.get(),u=()=>g(f.get());return{getValue:a,getMasked:i=>V(g(c.get()),i,s),getUnmasked:()=>k(g(c.get()),s),getMoneyMasked:()=>h(g(c.get()),r),getMoneyUnmasked:()=>A(g(c.get()),r),getError:u,setError:i=>(f.set(i),g(u())),setValue:i=>(c.set(typeof i=="string"?k(i,s):i),a()),setMasked:(i,m)=>(c.set(V(i,m,s)),g(a())),setMoney:i=>(c.set(A(i,r)),g(a())),setMoneyMasked:i=>(c.set(h(i,r)),g(a())),subscribeValue:i=>c.subscribe(i),subscribeError:i=>f.subscribe(i)}},H=e=>{var O,w;const t=M((e==null?void 0:e.initialValues)||{},o=>E(o)),n=M((e==null?void 0:e.initialValues)||{},()=>E());let s=!1,r=((O=e==null?void 0:e.options)==null?void 0:O.maskOptions)??$,c=U((w=e==null?void 0:e.options)==null?void 0:w.moneyOptions);const f=()=>M(t,o=>y(o)&&o.get()),a=()=>M(n,o=>y(o)&&g(o.get())),u=o=>M(t,d=>y(d)&&d.subscribe(o)),p=o=>M(n,d=>y(d)&&d.subscribe(o)),S=o=>{M(o,d=>y(d)&&d.set(""))};return{getIsValid:()=>s,getValues:f,getErrors:a,subscribeAllValues:u,subscribeAllErrors:p,reset:S,field:o=>G(o,t,n,r,c),submit:o=>{const d=f(),i=e!=null&&e.resolver?e.resolver(d)||{}:{};S(n),s=!0,Object.keys(i).map(m=>{if(!i[m])return;s=!1,_(n,m).set(i[m])}),s&&o(d)},mask:(o,d)=>V(o,d,r),unmask:o=>k(o,r),maskMoney:o=>h(o,c),unmaskMoney:o=>A(o,c),getPlaceholder:o=>q(o,r),setRulesMask:o=>{r=o},setRulesMoney:o=>{c=U(o)},getRules:()=>({rulesMask:r,rulesMoney:c})}};l.DEFAULT_MASK_OPTIONS=$,l.DEFAULT_MONEY_OPTIONS=b,l.default=H,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});