import{r as E,Y as w,b5 as I,c as u,Z as K}from"./index-dyx4v2bZ.js";function T(n){try{return JSON.parse(n),!0}catch{return!1}}function f(n){try{return JSON.parse(n)}catch{return{}}}function p(n){return Object.values(n).every(a=>typeof a=="string")}function P(n){const a=x(),i=E(N(n)),l=E(typeof n=="string"?n:JSON.stringify(n)),s=w({});v(n);function k(e){if(i.value!==e){switch(e){case"assisted":v(d.value);break;case"freehand":l.value=JSON.stringify(h.value,void 0,2);break}i.value=e}}function b(e){s.value=Object.entries(e).reduce((t,[r,c])=>(t[a()]={key:r,value:String(c)},t),{})}function o(){s.value={...s.value,[a()]:{value:"",key:""}}}function m(e,t){const r=s.value[e];r&&(s.value={...s.value,[e]:{...r,key:t}})}function A(e,t){const r=s.value[e];r&&(s.value={...s.value,[e]:{...r,value:t}})}function O(e){if(!s.value[e])return;const t={...s.value};delete t[e],s.value=t}function J(e){const t=s.value[e];if(t&&y.value.has(t.key))return"This key is already in use. Please remove duplicate keys."}function N(e){return typeof e!="string"?p(e)?"assisted":"freehand":I.exec(e)||!T(e)?"freehand":p(f(e))?"assisted":"freehand"}function v(e){const t=typeof e=="string"?f(e):e;s.value=Object.entries(t).reduce((r,[c,j])=>(r[a()]={key:c,value:String(j)},r),{}),Object.keys(s.value).length===0&&o()}const S=u(()=>Object.values(s.value).some(e=>e.key==="")),y=u(()=>{const e=new Set,t=new Set;for(const{key:r}of Object.values(s.value))e.has(r)?t.add(r):e.add(r);return t}),g=u(()=>{switch(i.value){case"assisted":return y.value.size===0;case"freehand":return!0;default:return!1}}),d=u(()=>{switch(i.value){case"assisted":{const e=Object.values(s.value).filter(t=>!(t.key===""&&t.value==="")).reduce((t,r)=>(t[r.key]=r.value,t),{});return JSON.stringify(e)}case"freehand":return l.value;default:return""}}),h=u(()=>f(d.value));return{mode:u({get:()=>i.value,set:k}),assistedEntries:K(s),addEntryDisabled:S,addAssistedEntry:o,updateAssistedEntryKey:m,updateAssistedEntryValue:A,removeAssistedEntry:O,getAssistedEntryError:J,updateAssistedEntries:b,freehandValue:l,isValid:g,currentValue:d,currentValueObject:h}}function x(){let n=0;return()=>String(++n)}export{P as u};
