import{t as e}from"./getChunk.mjs";const t=e=>typeof e==`object`&&!!e&&!Array.isArray(e),n=e=>{let t=5381;for(let n=0;n<e.length;n++)t=(t<<5)+t^e.charCodeAt(n);return(t>>>0).toString(16).padStart(8,`0`)},r=(e,n,r)=>{let i=e;for(let e=0;e<n.length-1;e++){let r=n[e],a=typeof n[e+1]==`number`;if(typeof r==`number`){if(!Array.isArray(i))throw Error(`Expected array at path segment ${e}`);i[r]===void 0&&(i[r]=a?[]:{}),i=i[r]}else{if(!t(i))throw Error(`Expected object at path segment ${e}`);r in i||(i[r]=a?[]:{}),i=i[r]}}let a=n[n.length-1];if(typeof a==`number`){if(!Array.isArray(i))throw Error(`Expected array at final segment`);i[a]=r}else{if(!t(i))throw Error(`Expected object at final segment`);i[a]=r}},i=(t,n)=>{if(n<=0)throw Error(`maxChars must be > 0`);let r=[],i=0;for(;i<t.length;){let a=e(t,{charStart:i,charLength:n});if(!a)break;r.push(a),i+=a.length}return r},a=(e,t,n=[],r=new WeakSet)=>{let a=Math.max(1,Math.floor((t-400)*.8)),o=[],s=(e,n)=>{if(typeof e==`string`){let r={op:`set`,path:n,value:e};if(JSON.stringify(r).length+150<=t){o.push(r);return}let s=i(e,a);o.push({op:`set`,path:[...n,`__splittedType`],value:`string`}),o.push({op:`set`,path:[...n,`__total`],value:s.length});for(let e=0;e<s.length;e++)o.push({op:`set`,path:[...n,String(e+1)],value:s[e]});return}if(typeof e!=`object`||!e){o.push({op:`set`,path:n,value:e});return}if(r.has(e))throw Error(`Cannot serialize circular structures to JSON.`);if(r.add(e),Array.isArray(e))for(let t=0;t<e.length;t++)s(e[t],[...n,t]);else for(let t of Object.keys(e))s(e[t],[...n,t]);r.delete(e)};return s(e,n),o},o=(e,r)=>{if(!t(e)&&!Array.isArray(e))throw Error(`Root must be an object or array.`);if(r<500)throw Error(`maxChars is too small. Use at least 500 characters.`);let i=Array.isArray(e)?`array`:`object`,o;try{o=JSON.stringify(e)}catch{throw Error(`Cannot serialize circular structures to JSON.`)}let s=n(o),c=a(e,r),l=[],u={schemaVersion:1,index:0,total:0,rootType:i,checksum:s,entries:[]},d=JSON.stringify({...u,entries:[]}).length,f=()=>{u.entries.length>0&&(l.push(u),u={schemaVersion:1,index:0,total:0,rootType:i,checksum:s,entries:[]})};for(let e of c)if(d+JSON.stringify(u.entries).length+(u.entries.length?1:0)+JSON.stringify(e).length<=r)u.entries.push(e);else{if(u.entries.length>0&&f(),d+JSON.stringify([e]).length>r)throw Error(`A single entry exceeds maxChars and cannot be split. Reduce entry size or increase maxChars.`);u.entries.push(e)}f(),l.length===0&&l.push({schemaVersion:1,index:0,total:0,rootType:i,checksum:s,entries:[]});let p=l.length;return l.forEach((e,t)=>{e.index=t,e.total=p}),l},s=e=>{let t=e.rootType===`array`?[]:{};for(let n of e.entries)n.op===`set`&&r(t,n.path,n.value);let n=e=>{if(typeof e!=`object`||!e)return e;if(Array.isArray(e)){for(let t=0;t<e.length;t++)e[t]=n(e[t]);return e}if(e.__splittedType===`string`){let t=e.__total;if(typeof t!=`number`||t<=0)return e;let n=[],r=!0;for(let i=1;i<=t;i++){let t=e[String(i)];if(typeof t!=`string`){r=!1;break}n.push(t)}return r?n.join(``):e}if(e.__splittedType===`array`){let t=e.__total;if(typeof t!=`number`||t<0)return e;let r=[],i=!0;for(let a=1;a<=t;a++){let t=e[String(a)];if(!Array.isArray(t)){i=!1;break}for(let e=0;e<t.length;e++)r.push(n(t[e]))}return i?r:e}for(let t of Object.keys(e))e[t]=n(e[t]);return e};return n(t)};export{s as c,o as s};
//# sourceMappingURL=chunkJSON.mjs.map