@asaidimu/anansi 8.6.7 → 8.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("hash-wasm"),t=require("fzstd");var n=class e{kind;value;constructor(e,t){this.kind=e,this.value=t}isZero(){return this.kind===`zero`}isNull(){return this.kind===`null`}static fromJSON(t){if(t===void 0)return new e(`zero`,void 0);if(t===null)return new e(`null`,void 0);if(typeof t==`string`)return new e(`string`,t);if(typeof t==`boolean`)return new e(`boolean`,t);if(typeof t==`number`)return Number.isInteger(t)?new e(`integer`,t):new e(`float`,t);if(Array.isArray(t))return new e(`array`,t);if(typeof t==`object`)return new e(`object`,t);throw Error(`anansi: unsupported literal ${typeof t}`)}};function r(e){let t=typeof e==`string`?JSON.parse(e):e;if(!t.name)throw Error(`anansi: schema requires a name`);return t}function i(e){return!!e&&!Array.isArray(e)&&typeof e.id==`string`}function a(e){return Array.isArray(e)}function o(e){return!!e&&!Array.isArray(e)&&typeof e.id!=`string`}function s(e,t){return e?`${e}.${t}`:t}var c=class{source;nested=new Map;building=new Set;constructor(e){this.source=e}compile(){for(let e of Object.keys(this.source.schemas??{}))this.compileNested(e);return{root:this.compileFields(this.source.fields??{},[],``),schemas:this.nested}}compileNested(e){let t=this.nested.get(e);if(t)return t;if(this.building.has(e))return null;let n=(this.source.schemas??{})[e];if(!n)throw Error(`anansi: schema '${e}' not found`);this.building.add(e);try{let t=n.values&&n.values.length>0?`enum`:n.type??`unknown`,r={id:e,name:n.name,effectiveType:t,fields:[],isRecursive:!1,values:n.values,enumDef:t===`enum`?l(n.values??[],n.type):void 0};return this.nested.set(e,r),n.fields&&(r.fields=this.compileFields(n.fields,[e],``)),r.isRecursive=r.fields.some(e=>e.kind.tag===`recursive`),r}finally{this.building.delete(e)}}compileFields(e,t,n){let r=[];for(let[i,a]of Object.entries(e))r.push(this.compileField(i,a,t,n));return r}compileField(e,t,r,c){let u=t.type??`unknown`,d={id:e,name:t.name,path:s(c,t.name),type:u,required:!!t.required,deprecated:!!t.deprecated,unique:!!t.unique,nullable:t.nullable!==!1,hasDefault:!n.fromJSON(t.default).isZero(),kind:{tag:`scalar`}},f=t.schema;switch(u){case`geometry`:break;case`record`:d.kind=i(f)?{tag:`container`,c:{itemSchema:this.resolve(f.id)??void 0,record:!0}}:{tag:`container`,c:{record:!0}};break;case`array`:if(a(f))throw Error(`anansi: ${d.path}: array cannot reference multiple schemas`);if(i(f))d.kind={tag:`container`,c:{itemSchema:this.require(f.id,d.path),record:!1}};else if(o(f)){let e=f.type??`unknown`,t={itemType:e,record:!1};(f.values?.length??0)>0&&(t.itemEnum=l(f.values,e)),d.kind={tag:`container`,c:t}}else d.kind={tag:`container`,c:{record:!1}};break;case`object`:d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:o(f)?{tag:`object`,schema:this.synthesizeInline(f,e,r)}:{tag:`scalar`};break;case`union`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`union`,variants:e}}else d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:{tag:`scalar`};break;case`composite`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`composite`,parts:e}}else d.kind={tag:`scalar`};break;case`enum`:if((t.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(t.values,u)};else if(o(f)&&(f.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(f.values,f.type??`string`)};else if(i(f)){let e=this.resolve(f.id);d.kind=e&&e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}else d.kind={tag:`scalar`};break;default:if(i(f)){let e=this.resolve(f.id);d.kind=e===null?{tag:`recursive`,schemaId:f.id}:e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}}return d}resolve(e){return this.nested.has(e)?this.nested.get(e):this.building.has(e)?null:this.compileNested(e)}require(e,t){let n=this.resolve(e);if(!n)throw Error(`anansi: ${t}: unresolvable schema reference '${e}'`);return n}synthesizeInline(e,t,n){let r=`inline:${[...n,t].join(`/`)}`,i=this.nested.get(r);if(i)return i;let a={id:r,name:r,effectiveType:e.type??`object`,fields:[],isRecursive:!1};this.nested.set(r,a);let o=e.fields??{};return a.fields=this.compileFields(o,[...n,r],``),a}};function l(e,t){let n=new Map,r=[];for(let t of e)t===null||[`string`,`number`,`boolean`].includes(typeof t)?n.set(JSON.stringify(t),t):r.push(t);return{lookup:n,complex:r,expectNumeric:t===`integer`||t===`number`}}const u={TypeUnknown:0,TypeInt:1,TypeFloat:2,TypeString:3,TypeBool:4,TypeBytes:5,TypeGeometry:6,TypeRecord:7,TypeArrayUnknown:8,TypeArrayInt:9,TypeArrayFloat:10,TypeArrayString:11,TypeArrayBool:12,TypeArrayBytes:13,TypeArrayObject:14,TypeArrayGeometry:15},d={[u.TypeUnknown]:`unknown`,[u.TypeInt]:`int`,[u.TypeFloat]:`float`,[u.TypeString]:`string`,[u.TypeBool]:`bool`,[u.TypeBytes]:`bytes`,[u.TypeGeometry]:`geometry`,[u.TypeRecord]:`record`,[u.TypeArrayUnknown]:`array_unknown`,[u.TypeArrayInt]:`array_int`,[u.TypeArrayFloat]:`array_float`,[u.TypeArrayString]:`array_string`,[u.TypeArrayBool]:`array_bool`,[u.TypeArrayBytes]:`array_bytes`,[u.TypeArrayObject]:`array_object`,[u.TypeArrayGeometry]:`array_geometry`},f=16384;function p(e){return(e.raw&4294967264|(e.raw>>>28&15)<<1)>>>0}function m(e,t,n,r,i){let a=0;return a|=(e&15)<<28,a|=(n&63)<<22,a|=(r&127)<<15,a|=(i.child&63)<<9,a|=(t&3)<<7,i.required&&(a|=64),i.hasDefault&&(a|=32),i.deprecated&&(a|=16),i.unique&&(a|=8),i.terminal&&(a|=4),i.nullable&&(a|=2),i.recursive&&(a|=1),h(a>>>0)}function h(e){return{raw:e,dt:e>>>28&15,schemaIdx:e>>>22&63,fieldIdx:e>>>15&127,childSchemaIdx:e>>>9&63,kind:e>>>7&3,required:!!(e&64),hasDefault:!!(e&32),deprecated:!!(e&16),unique:!!(e&8),terminal:!!(e&4),nullable:!!(e&2),recursive:!!(e&1)}}function ee(e){switch(e){case`string`:return u.TypeString;case`number`:return u.TypeFloat;case`decimal`:return u.TypeString;case`integer`:return u.TypeInt;case`boolean`:return u.TypeBool;case`bytes`:return u.TypeBytes;default:return u.TypeUnknown}}function g(e){return e?.expectNumeric?u.TypeInt:u.TypeString}function te(e,t){if(e)return u.TypeArrayObject;switch(t){case`string`:return u.TypeArrayString;case`number`:return u.TypeArrayFloat;case`decimal`:return u.TypeArrayString;case`integer`:return u.TypeArrayInt;case`boolean`:return u.TypeArrayBool;case`bytes`:return u.TypeArrayBytes;case`geometry`:return u.TypeArrayGeometry;case`unknown`:return u.TypeArrayUnknown;default:return u.TypeUnknown}}function ne(e){let t=e.kind;if(e.type===`geometry`)return[u.TypeGeometry,0,!0];if(t.tag===`scalar`)return[ee(e.type),0,!0];if(t.tag===`enum`)return[g(t.enumDef),0,!0];if(t.tag===`recursive`)return[u.TypeUnknown,1,!0];if(t.tag===`object`)return[u.TypeUnknown,1,!1];if(t.tag===`container`){let e=!t.c.itemSchema;return t.c.record?[u.TypeRecord,1,e]:[te(t.c.itemSchema,t.c.itemType),2,e]}return t.tag===`union`?[u.TypeUnknown,3,!1]:[u.TypeUnknown,1,!1]}var re=class{slots=[];descriptors=[];metas=[];localOffsets=[];fieldTypes=[];linkedSlots=[];assignSlot(e){if(this.slots.length>=63)throw Error(`anansi: exceeds maximum of 63 nested schemas while assigning '${e.name}'`);let t=this.slots.length;return this.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),this.linkedSlots.push({idx:t,slot:this.slots[t],fields:[]}),t}childSlotForField(e,t){let n=e.kind;return n.tag===`recursive`?{idx:t,kindTag:n}:n.tag===`object`?{idx:this.assignSlot(n.schema),kindTag:n}:n.tag===`composite`?{idx:this.assignSlot({id:`composite:${e.path}`,name:`composite:${e.name}`,effectiveType:`object`,fields:ie(n.parts),isRecursive:!1}),kindTag:n}:n.tag===`container`&&n.c.itemSchema?{idx:this.assignSlot(n.c.itemSchema),kindTag:n}:null}linkFields(e,t){let n=this.linkedSlots[t],r=[];for(let i=0;i<e.length;i++){if(e.length>128)throw Error(`anansi: slot ${t} exceeds 128 fields`);let a=e[i],[o,s,c]=ne(a),l=this.childSlotForField(a,t),u=l?l.idx:63,d=a.kind.tag===`recursive`,f=m(o,s,t,i,{required:a.required,hasDefault:a.hasDefault,deprecated:a.deprecated,unique:a.unique,terminal:c,nullable:a.nullable,recursive:d,child:u}),p=this.descriptors.length;this.descriptors.push(f),this.fieldTypes.push(a.type);let h={meta:{name:a.name,path:a.path},fd:f,dp:0,abs:p};n.fields.push(h),this.metas.push(h.meta),!c&&u!==63&&r.push({rf:a,fd:f,childIdx:u})}for(let e of r){let t=e.rf.kind;if(t.tag===`object`||t.tag===`composite`){let n=t.tag===`object`?t.schema.fields:ie(t.parts),r=this.linkFields(n,e.childIdx);this.setSlot(e.childIdx,r.count)}else if(t.tag===`container`&&t.c.itemSchema){let n=this.linkFields(t.c.itemSchema.fields,e.childIdx);this.setSlot(e.childIdx,n.count)}let r=n.fields.find(t=>t.fd===e.fd);r.child=this.linkedSlots[e.childIdx]}return{count:e.length,slot:n}}setSlot(e,t){let n=this.slots[e];n.fieldStart===-1&&(n.fieldStart=this.descriptors.length-t),n.fieldCount=t}};function ie(e){let t=[];for(let n of e)t.push(...n.fields);return t}function ae(e){for(let t=e.slots.length-1;t>=0;t--){let n=e.slots[t];n.fieldCount<0&&(n.fieldStart=0,n.fieldCount=0);let r=0;for(let t=0;t<n.fieldCount;t++){let i=n.fieldStart+t,a=e.descriptors[i];a.terminal?r+=1:a.childSchemaIdx!==63&&(r+=e.slots[a.childSchemaIdx].footprint)}n.footprint=r}for(let t of e.slots){let n=0;for(let r=0;r<t.fieldCount;r++){let i=t.fieldStart+r,a=e.descriptors[i];e.localOffsets[i]=n,a.terminal?n+=1:a.childSchemaIdx!==63&&(n+=e.slots[a.childSchemaIdx].footprint)}}}function oe(e,t,n,r){if(r.length===0)return 0;if(r.length===1){let[n,i]=r[0],a=e[n].fieldStart+i;return t[a].terminal?a+1>>>0:0}let i=f;for(let a=0;a<r.length;a++){let[o,s]=r[a],c=e[o].fieldStart+s,l=t[c];if(i+=n[c],a===r.length-1)return l.terminal?i>>>0:0;if(l.terminal)return 0}return i>>>0}function se(e,t){return(t<<5|e<<1)>>>0}function ce(e){let{root:t,schemas:n}=new c(e).compile(),r=new re;r.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),r.linkedSlots.push({idx:0,slot:r.slots[0],fields:[]});let i=r.linkFields(t,0);return r.slots[0].fieldStart=0,r.slots[0].fieldCount=i.count,ae(r),ue(r,r.linkedSlots[0],[]),{slots:r.slots,metas:r.metas,descriptors:r.descriptors,localOffsets:r.localOffsets,fieldTypes:r.fieldTypes,root:r.linkedSlots[0],childrenByPath:le(r.linkedSlots[0])}}function le(e){let t=new Map;for(let n of e.fields)n.child&&t.set(n.meta.path,n.child);return t}function ue(e,t,n){for(let r=0;r<t.slot.fieldCount;r++){let i=t.slot.fieldStart+r,a=e.descriptors[i],o=t.fields[r],s=[...n,[t.idx,r]],c=oe(e.slots,e.descriptors,e.localOffsets,s);o.dp=c===0?p(a):se(a.dt,c),!a.terminal&&o.child&&ue(e,o.child,s)}}function de(e){let t=(e,n,r)=>{let i=[];for(let a=0;a<e.slot.fieldCount;a++){let o=e.fields[a],s=o.fd,c=[...n,[e.idx,a]],l=r?`${r}.${o.meta.name}`:o.meta.name,f={name:o.meta.name,path:l,t:d[s.dt],dp:o.dp};if(!s.terminal&&o.child){s.dt===u.TypeArrayObject?(f.child=t(o.child,c,l),i.push(f)):i.push(...t(o.child,c,l));continue}i.push(f)}return i};return t(e.root,[],``)}function _(e,t){for(t>>>=0;t>=128;)e.push(t&127|128),t>>>=7;e.push(t)}function fe(e,t){if(!Number.isSafeInteger(t))throw Error(`anansi: integer ${t} exceeds Number.MAX_SAFE_INTEGER`);me(e,BigInt(t))}function pe(e,t){for(;t>=128n;)e.push(Number(t&127n)|128),t>>=7n;e.push(Number(t))}function me(e,t){pe(e,t<<1n^t>>63n)}var v=class{data;pos;constructor(e,t=0){this.data=e,this.pos=t}eof(){return this.pos>=this.data.length}remaining(){return this.data.length-this.pos}byte(){if(this.eof())throw Error(`anansi: unexpected end of buffer`);return this.data[this.pos++]}take(e){if(e<0||this.pos+e>this.data.length)throw Error(`anansi: unexpected end of buffer (need ${e}, have ${this.remaining()})`);let t=this.data.subarray(this.pos,this.pos+e);return this.pos+=e,t}uvarint(){let e=0,t=0;for(let n=0;n<10;n++){let r=this.byte();if(r<128){if(n===9&&r>1)throw Error(`anansi: varint overflow`);return(e|r<<t)>>>0}e|=(r&127)<<t,t+=7}throw Error(`anansi: varint overflow`)}uvarintBig(){let e=0n,t=0n,n=0;for(;n<10;){let r=this.byte();if(e|=BigInt(r&127)<<t,r<128)return e;t+=7n,n++}throw Error(`anansi: varint overflow`)}varint(){let e=this.uvarintBig(),t=e>>1n^-(e&1n),n=Number(t);if(!Number.isSafeInteger(n))throw Error(`anansi: decoded integer ${n} exceeds Number.MAX_SAFE_INTEGER`);return n}};const y={Unknown:0,Int:1,Float:2,String:3,Bool:4,Bytes:5,Geometry:6,Record:7,ArrayUnknown:8,ArrayInt:9,ArrayFloat:10,ArrayString:11,ArrayBool:12,ArrayBytes:13,ArrayObject:14,ArrayGeometry:15},he=new TextEncoder,ge=new TextDecoder;function _e(e){let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.subarray(r,r+n));return btoa(t)}function ve(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function ye(e,t){fe(e,t)}function be(e){return e.varint()}function b(e,t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t,!0);for(let t=0;t<8;t++)e.push(n.getUint8(t))}function x(e){let t=e.take(8);return new DataView(t.buffer,t.byteOffset,8).getFloat64(0,!0)}function S(e,t){let n=he.encode(t);_(e,n.length);for(let t of n)e.push(t)}function C(e){let t=e.uvarint();return ge.decode(e.take(t))}function xe(e,t){e.push(+!!t)}function Se(e){return e.byte()!==0}function w(e,t){let n=new Uint8Array(t.length+7>>3);t.forEach((e,t)=>{e&&(n[t>>3]|=1<<(t&7))});for(let t of n)e.push(t)}function Ce(e,t){let n=e.take(t+7>>3),r=[];for(let e=0;e<t;e++)r.push(!!(n[e>>3]&1<<(e&7)));return r}function we(e,t){let n=ve(t);_(e,n.length);for(let t of n)e.push(t)}function Te(e){let t=e.uvarint();return _e(e.take(t))}function Ee(e,t){_(e,t.length);for(let n of t){_(e,n.length/2);for(let t=0;t<n.length;t+=2)b(e,n[t]),b(e,n[t+1])}}function De(e){let t=e.uvarint(),n=[];for(let r=0;r<t;r++){let t=e.uvarint(),r=[];for(let n=0;n<t;n++)r.push(x(e),x(e));n.push(r)}return n}const T={Null:0,Int:y.Int,Float:y.Float,String:y.String,Bool:y.Bool,Bytes:y.Bytes,Geometry:y.Geometry,Record:y.Record,ArrUnknown:y.ArrayUnknown,ArrInt:y.ArrayInt,ArrFloat:y.ArrayFloat,ArrString:y.ArrayString,ArrBool:y.ArrayBool,ArrBytes:y.ArrayBytes};function Oe(e){return e.length>0&&Array.isArray(e[0])&&Array.isArray(e[0][0])}function E(e,t){if(t==null){e.push(T.Null);return}switch(typeof t){case`boolean`:e.push(T.Bool),xe(e,t);return;case`string`:e.push(T.String),S(e,t);return;case`number`:Number.isSafeInteger(t)?(e.push(T.Int),ye(e,t)):(e.push(T.Float),b(e,t));return;case`object`:if(Array.isArray(t)){if(Oe(t)){e.push(T.Geometry),Ee(e,t);return}if(t.every(e=>typeof e==`string`)){e.push(T.ArrString),_(e,t.length);for(let n of t)S(e,n);return}if(t.every(e=>Number.isSafeInteger(e))){e.push(T.ArrInt),_(e,t.length);for(let n of t)ye(e,n);return}if(t.every(e=>typeof e==`number`)){e.push(T.ArrFloat),_(e,t.length);for(let n of t)b(e,n);return}if(t.every(e=>typeof e==`boolean`)){e.push(T.ArrBool);let n=t;_(e,n.length),w(e,n);return}e.push(T.ArrUnknown),_(e,t.length);for(let n of t)E(e,n);return}e.push(T.Record),ke(e,t);return;default:throw Error(`anansi: unsupported any value ${typeof t}`)}}function D(e){let t=e.byte();switch(t){case T.Null:return null;case T.Bool:return Se(e);case T.String:return C(e);case T.Int:return be(e);case T.Float:return x(e);case T.Bytes:return Te(e);case T.Geometry:return De(e);case T.Record:return Ae(e);case T.ArrInt:{let t=e.uvarint();return Array.from({length:t},()=>be(e))}case T.ArrFloat:{let t=e.uvarint();return Array.from({length:t},()=>x(e))}case T.ArrString:{let t=e.uvarint();return Array.from({length:t},()=>C(e))}case T.ArrBool:return Ce(e,e.uvarint());case T.ArrUnknown:{let t=e.uvarint();return Array.from({length:t},()=>D(e))}default:throw Error(`anansi: unknown any tag ${t}`)}}function ke(e,t){let n=Object.keys(t).sort();_(e,n.length);for(let r of n)S(e,r),E(e,t[r])}function Ae(e){let t=e.uvarint(),n={};for(let r=0;r<t;r++){let t=C(e);n[t]=D(e)}return n}function je(e,t){_(e,t.length);for(let n of t)ye(e,n)}function Me(e){let t=e.uvarint();return Array.from({length:t},()=>be(e))}function Ne(e,t){_(e,t.length);for(let n of t)b(e,n)}function Pe(e){let t=e.uvarint();return Array.from({length:t},()=>x(e))}function Fe(e,t){_(e,t.length);for(let n of t)S(e,n)}function Ie(e){let t=e.uvarint();return Array.from({length:t},()=>C(e))}function Le(e,t){_(e,t.length),w(e,t)}function Re(e){return Ce(e,e.uvarint())}function ze(e,t){_(e,t.length);for(let n of t)we(e,n)}function Be(e){let t=e.uvarint();return Array.from({length:t},()=>Te(e))}function Ve(e,t){_(e,t.length);for(let n of t)Ee(e,n)}function He(e){let t=e.uvarint();return Array.from({length:t},()=>De(e))}function Ue(e,t){_(e,t.length);for(let n of t)E(e,n)}function We(e){let t=e.uvarint();return Array.from({length:t},()=>D(e))}const O={Dense:0,Sparse:1,Batch:2,Stream:3};function k(e,t){let n=t.split(`.`),r=e;for(let e=0;e<n.length;e++){if(typeof r!=`object`||!r)return[!1,void 0];let t=r;if(!(n[e]in t))return[!1,void 0];r=t[n[e]]}return r===void 0?[!1,void 0]:[!0,r]}function A(e,t){let[n,r]=k(e,t.path);return!n||r===void 0?`notSet`:r===null?`null`:`value`}function Ge(e,t){let n=t.length+1;return{...e,path:e.path.slice(n)}}function Ke(e,t){return e.map(e=>Ge(e,t))}function qe(e,t,n){switch(t.t){case`int`:ye(e,n);break;case`float`:b(e,n);break;case`string`:S(e,n);break;case`bool`:xe(e,n);break;case`bytes`:we(e,n);break;case`geometry`:Ee(e,n);break;case`record`:ke(e,n);break;case`unknown`:E(e,n);break;case`array_int`:je(e,n);break;case`array_float`:Ne(e,n);break;case`array_string`:Fe(e,n);break;case`array_bool`:Le(e,n);break;case`array_bytes`:ze(e,n);break;case`array_geometry`:Ve(e,n);break;case`array_unknown`:Ue(e,n);break;default:throw Error(`anansi: cannot write value for type ${t.t} field ${t.path}`)}}function Je(e,t){switch(t.t){case`int`:return e.varint();case`float`:return x(e);case`string`:return C(e);case`bool`:return Se(e);case`bytes`:return Te(e);case`geometry`:return De(e);case`record`:return Ae(e);case`unknown`:return D(e);case`array_int`:return Me(e);case`array_float`:return Pe(e);case`array_string`:return Ie(e);case`array_bool`:return Re(e);case`array_bytes`:return Be(e);case`array_geometry`:return He(e);case`array_unknown`:return We(e);default:throw Error(`anansi: cannot read value for type ${t.t} field ${t.path}`)}}function Ye(e,t,n,r,i){let a=Ke(t,n);_(e,r.length);for(let t of r){let n=0;for(let e of a)A(t,e)!==`notSet`&&n++;let r=a.length>0?n/a.length:1,o=!(a.length<=64||r>.25),s=o?O.Sparse:O.Dense,c=(i.flags&-200|s)>>>0,l=[],u={flags:i.flags&-4|s,version:i.version};o?N(l,a,t,u):j(l,a,t,u);let d=[c,i.version,...l];_(e,d.length);for(let t of d)e.push(t)}}function Xe(e,t,n,r,i){let a=e.uvarint();for(let o=0;o<a;o++){let a=e.uvarint(),o=new v(e.take(a)),s=o.byte();o.byte();let c=i?i():{},l=s&3;if(l===O.Dense)M(o,t,c,n);else if(l===O.Sparse)P(o,t,c,n);else throw Error(`anansi: unsupported nested packet type ${l}`);r.push(c)}}const Ze={flags:O.Dense,version:0};function j(e,t,n,r=Ze){let i=2*t.length,a=new Uint8Array(i+7>>3);t.forEach((e,t)=>{let r=A(n,e)===`notSet`?0:A(n,e)===`null`?1:2;a[t>>2]|=r<<((t&3)<<1)});for(let t of a)e.push(t);let o=$e(t);for(let t=0;t<=15;t++){let i=o[t];if(!(!i||i.length===0)){if(t===y.Bool){let t=[];for(let e of i)A(n,e)===`value`&&t.push(k(n,e.path)[1]);w(e,t);continue}for(let t of i)if(A(n,t)===`value`){if(t.t===`array_object`){Ye(e,t.child,t.name,k(n,t.path)[1]??[],r);continue}qe(e,t,k(n,t.path)[1])}}}}function M(e,t,n,r){let i=[],a=2*t.length,o=e.take(a+7>>3);for(let e=0;e<t.length;e++)i.push(o[e>>2]>>((e&3)<<1)&3);let s=$e(t);for(let a=0;a<=15;a++){let o=s[a];if(o){if(a===y.Bool){let a=o.filter((e,n)=>i[t.indexOf(o[n])]===2).length,s=Ce(e,a),c=0;o.forEach(e=>{let a=t.indexOf(e);i[a]===2&&F(n,e.path,r,s[c++])});continue}for(let a of o){let o=i[t.indexOf(a)];if(o!==0){if(o===1){F(n,a.path,r,null);continue}if(a.t===`array_object`){let t=[];Xe(e,a.child,a.name,t),F(n,a.path,r,t);continue}F(n,a.path,r,Je(e,a))}}}}}function N(e,t,n,r={flags:O.Sparse,version:0}){let i=t.filter(e=>A(n,e)!==`notSet`);_(e,i.length);for(let t of i){let i=A(n,t),a=t.dp;i===`null`?a|=1:a&=4294967294,_(e,a),i===`value`&&(t.t===`array_object`?Ye(e,t.child,t.name,k(n,t.path)[1]??[],r):qe(e,t,k(n,t.path)[1]))}}function P(e,t,n,r){let i=new Map;for(let e of t)i.set(e.dp&4294967294,e);let a=e.uvarint();for(let t=0;t<a;t++){let t=e.uvarint(),a=!!(t&1),o=i.get(t&4294967294);if(!o)throw Error(`anansi: sparse packet references unknown data point ${t}`);if(a){F(n,o.path,r,null);continue}if(o.t===`array_object`){let t=[];Xe(e,o.child,o.name,t),F(n,o.path,r,t);continue}F(n,o.path,r,Je(e,o))}}function F(e,t,n,r){let i=t;if(n&&i.startsWith(n+`.`)){i=i.slice(n.length+1),Qe(e,i.split(`.`),r);return}Qe(e,i.split(`.`),r)}function Qe(e,t,n){let r=e;for(let e=0;e<t.length-1;e++){let n=t[e];(typeof r[n]!=`object`||r[n]===null)&&(r[n]={}),r=r[n]}r[t[t.length-1]]=n}function $e(e){let t=[];for(let n of e){let e=et[n.t]??y.Unknown;(t[e]??=[]).push(n)}return t}const et=Object.fromEntries(Object.entries({unknown:y.Unknown,int:y.Int,float:y.Float,string:y.String,bool:y.Bool,bytes:y.Bytes,geometry:y.Geometry,record:y.Record,array_unknown:y.ArrayUnknown,array_int:y.ArrayInt,array_float:y.ArrayFloat,array_string:y.ArrayString,array_bool:y.ArrayBool,array_bytes:y.ArrayBytes,array_object:y.ArrayObject,array_geometry:y.ArrayGeometry}));function I(e){if(e<0||e>1023)throw Error(`anansi: schema version ${e} exceeds maximum of 1023`);return{flags:e>>8<<4,version:e&255}}function tt(e,t){return(e>>4&3)<<8|t}function nt(e,t,n=0,r=`auto`){let i=I(n),a=0;for(let n of e)A(t,n)!==`notSet`&&a++;let o=e.length?a/e.length:1,s=r===`sparse`||r===`auto`&&!(e.length<=64||o>.25),c=i.flags|(s?O.Sparse:O.Dense),l=[c,i.version],u={flags:c,version:i.version},d=[];return s?N(d,e,t,u):j(d,e,t,u),new Uint8Array([...l,...d])}function rt(e,t){let n=new v(e),r=n.byte(),i=n.byte();if((r>>4&3)===O.Stream)throw Error(`anansi: stream packets unsupported`);if(r&196)throw Error(`anansi: compressed/encrypted/hashed packets require transform support not enabled in this build`);let a=r&3,o={};if(a===O.Dense)M(n,t,o);else if(a===O.Sparse)P(n,t,o);else throw Error(`anansi: use batch API for packet type ${a}`);return{version:tt(r,i),doc:o}}function it(e,t,n=0){let r=I(n),i=0,a=0;for(let n of t){for(let t of e)A(n,t)!==`notSet`&&i++;a+=e.length}let o=a?i/a:1,s=e.length>64&&o<=.25,c=[r.flags|O.Batch,r.version],l=[];_(l,t.length),l.push(s?2:0);let u={flags:r.flags|O.Batch,version:r.version};for(let n of t)s?N(l,e,n,u):j(l,e,n,u);return new Uint8Array([...c,...l])}function at(e,t,n=0){let r=I(n),i=[r.flags|O.Batch|8,r.version],a=ot(e,t,{flags:i[0],version:r.version});return new Uint8Array([...i,...a])}function ot(e,t,n){let r=[];_(r,t.length),r.push(1);let i=$e(e);for(let e=0;e<=15;e++){let a=i[e];if(!a||a.length===0)continue;let o=2*a.length*t.length,s=new Uint8Array(o+7>>3),c=0;for(let e of a)for(let n of t){let t=A(n,e),r=t===`notSet`?0:t===`null`?1:2;s[c>>3]|=r<<(c&7),c+=2}for(let e of s)r.push(e);if(e===y.Bool){for(let e of a){let n=[];for(let r of t)A(r,e)===`value`&&n.push(k(r,e.path)[1]);w(r,n)}continue}for(let e of a)for(let i of t)if(A(i,e)===`value`){if(e.t===`array_object`){Ye(r,e.child,e.name,k(i,e.path)[1]??[],n);continue}qe(r,e,k(i,e.path)[1])}}return r}function st(e,t){let n=new v(e),r=n.byte(),i=n.byte();if((r&3)!==O.Batch)throw Error(`anansi: not a batch packet`);if(r&196)throw Error(`anansi: transformed batch packets unsupported in this build`);let a=n.uvarint(),o=n.byte(),s=Array.from({length:a},()=>({}));if(o&1||r&8)ct(n,t,s);else{let e=!!(o&2);for(let r of s)e?P(n,t,r):M(n,t,r)}return{version:tt(r,i),docs:s}}function ct(e,t,n){let r=$e(t);for(let t=0;t<=15;t++){let i=r[t];if(!i||i.length===0)continue;let a=2*i.length*n.length,o=e.take(a+7>>3),s=[],c=0;for(let e=0;e<i.length;e++){let e=[];for(let t=0;t<n.length;t++)e.push(o[c>>3]>>(c&7)&3),c+=2;s.push(e)}if(t===y.Bool){i.forEach((t,r)=>{let i=s[r].filter(e=>e===2).length,a=Ce(e,i),o=0;s[r].forEach((e,r)=>{if(e!==0){if(e===1){F(n[r],t.path,void 0,null);return}F(n[r],t.path,void 0,a[o++])}})});continue}i.forEach((t,r)=>{s[r].forEach((r,i)=>{if(r!==0){if(r===1){F(n[i],t.path,void 0,null);return}if(t.t===`array_object`){let r=[];Xe(e,t.child,t.name,r),F(n[i],t.path,void 0,r);return}F(n[i],t.path,void 0,Je(e,t))}})})}}const lt=e=>e&3;let ut,dt=!1;function ft(){if(!dt){dt=!0;try{ut=globalThis.process?.getBuiltinModule?.(`node:zlib`)??void 0}catch{ut=void 0}}return ut}function pt(e){let t=ft()?.zstdCompressSync?.(e);if(!t)throw Error(`anansi: zstd compression is unavailable in this runtime (no node:zlib). Send plain packets, or provide a WASM compressor.`);return t}function mt(e,n){return ft()?.zstdDecompressSync?.(e)||(0,t.decompress)(e)}async function ht(t){let n=await(0,e.blake3)(t,128),r=new Uint8Array(16);for(let e=0;e<16;e++)r[e]=parseInt(n.slice(e*2,e*2+2),16);return r}async function gt(e){if(e.length!==32)throw Error(`anansi: AES-256-GCM requires a 32-byte key, got ${e.length}`);return crypto.subtle.importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`,`decrypt`])}async function _t(e,t,n,r){let i=e;r.integrity&&(i|=128),r.compression&&(i|=4),r.encryptionKey&&(i|=64);let a=[i,t];if(r.integrity)for(let e of await ht(n))a.push(e);let o=[];if(r.compression){_(o,n.length);for(let e of pt(n))o.push(e)}else for(let e of n)o.push(e);if(!r.encryptionKey)return new Uint8Array([...a,...o]);let s=await gt(r.encryptionKey),c=crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await crypto.subtle.encrypt({name:`AES-GCM`,iv:c},s,new Uint8Array(o))),u=new Uint8Array(a.length+12+l.length);return u.set(Uint8Array.from(a),0),u.set(c,a.length),u.set(l,a.length+12),u}async function vt(e,t,n){let r;t&128&&(r=e.take(16));let i=e.data.subarray(e.pos);if(t&64){if(!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);if(i.length<12)throw Error(`anansi: truncated nonce`);let e=i.subarray(0,12);i=i.subarray(12);let t=await gt(n.decryptionKey),r=await crypto.subtle.decrypt({name:`AES-GCM`,iv:e},t,i);i=new Uint8Array(r)}let a=i.length,o=new v(i);if(t&4){if(a=o.uvarint(),a>536870912)throw Error(`anansi: declared uncompressed size ${a} exceeds limit`);let e=mt(i.subarray(o.pos),a);if(e.length!==a)throw Error(`anansi: decompressed ${e.length} bytes, header declared ${a}`);i=e}if(r){let e=await ht(i);for(let t=0;t<16;t++)if(r[t]!==e[t])throw Error(`anansi: integrity check failed (packet digest mismatch)`)}return new v(i)}function yt(e,t){let n=0;for(let r of e){let[e,i]=bt(t,r.path);e&&i!=null&&n++}return e.length<=64||e.length===0||n/e.length>.25?`dense`:`sparse`}function bt(e,t){let n=e;for(let e of t.split(`.`)){if(typeof n!=`object`||!n)return[!1,void 0];let t=n;if(!(e in t))return[!1,void 0];n=t[e]}return[n!==void 0,n]}function xt(e,t,n){let r=n===`sparse`||n===`auto`&&yt(e,t)===`sparse`,i=[];return r?N(i,e,t):j(i,e,t),new Uint8Array(i)}async function St(e,t,n=0,r={}){let i=I(n),a=r.kind??`auto`,o=a===`sparse`||a===`auto`&&yt(e,t)===`sparse`,s=i.flags|(o?O.Sparse:O.Dense),c=xt(e,t,o?`sparse`:`dense`);return _t(s,i.version,c,r)}async function Ct(e,t,n={}){let r=new v(e),i=r.byte(),a=r.byte();if(i&64&&!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);let o=await vt(r,i,n),s={},c=lt(i);if(c===O.Dense)M(o,t,s);else if(c===O.Sparse)P(o,t,s);else throw Error(`anansi: use batch API for packet type ${c}`);return{version:tt(i,a),doc:s}}function wt(e,t){let n=0,r=0;for(let i of t)for(let t of e){let[e,a]=bt(i,t.path);e&&a!=null&&n++,r++}let i=r>0?n/r:1,a=e.length>64&&i<=.25,o=[];_(o,t.length),o.push(a?2:0);for(let n of t)a?N(o,e,n):j(o,e,n);return{body:new Uint8Array(o),sparse:a}}async function Tt(e,t,n=0,r={}){let i=I(n),{body:a,sparse:o}=wt(e,t);return _t(i.flags|O.Batch|(o?2:0),i.version,a,r)}async function Et(e,t,n=0,r={}){let i=I(n),a=i.flags|O.Batch|8,o=new Uint8Array(ot(e,t,{flags:a,version:i.version}));return _t(a,i.version,o,r)}async function Dt(e,t,n={}){let r=new v(e),i=r.byte(),a=r.byte();if(lt(i)!==O.Batch)throw Error(`anansi: not a batch packet`);let o=await vt(r,i,n),s=o.uvarint(),c=o.byte(),l=Array.from({length:s},()=>({}));if(c&1||i&8)ct(o,t,l);else for(let e of l)c&2?P(o,t,e):M(o,t,e);return{version:tt(i,a),docs:l}}var Ot=class e{linked;fields;fullVersion;kind;enc;dec;constructor(e,t,n,r,i,a){this.linked=e,this.fields=t,this.fullVersion=n,this.kind=r,this.enc=i,this.dec=a}static async create(t,n={}){let i=ce(r(t));return new e(i,de(i),n.fullVersion??0,n.kind??`auto`,{compression:n.compression,integrity:n.integrity,encryptionKey:n.encryptionKey},{decryptionKey:n.decryptionKey})}async encode(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?St(this.fields,e,this.fullVersion,{...this.enc,kind:this.kind}):nt(this.fields,e,this.fullVersion,this.kind)}async decode(e){return Ct(e,this.fields,this.dec)}async encodeBatch(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Tt(this.fields,e,this.fullVersion,this.enc):it(this.fields,e,this.fullVersion)}async decodeBatch(e){return Dt(e,this.fields,this.dec)}async encodeColumnar(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Et(this.fields,e,this.fullVersion,this.enc):Et(this.fields,e,this.fullVersion,{})}};function kt(e,t){let n=[];switch(e.type){case`modifyProperty`:n.push({op:`replace`,path:`/${e.id}`,value:e.value});break;case`addField`:n.push({op:`add`,path:`/fields/${e.id}`,value:e.definition});break;case`removeField`:n.push({op:`remove`,path:`/fields/${e.id}`});break;case`modifyField`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/fields/${e.id}/${t}`,value:r});break;case`addIndex`:t.indexes||n.push({op:`add`,path:`/indexes`,value:{}}),n.push({op:`add`,path:`/indexes/${e.id}`,value:e.definition});break;case`removeIndex`:n.push({op:`remove`,path:`/indexes/${e.id}`});break;case`modifyIndex`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/indexes/${e.id}/${t}`,value:r});break;case`addConstraint`:t.constraints||n.push({op:`add`,path:`/constraints`,value:{}}),n.push({op:`add`,path:`/constraints/${e.id}`,value:e.constraint});break;case`removeConstraint`:n.push({op:`remove`,path:`/constraints/${e.id}`});break;case`modifyConstraint`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/constraints/${e.id}/${t}`,value:r});break;case`addSchema`:t.schemas||n.push({op:`add`,path:`/schemas`,value:{}}),n.push({op:`add`,path:`/schemas/${e.id}`,value:e.definition});break;case`removeSchema`:n.push({op:`remove`,path:`/schemas/${e.id}`});break;case`modifySchema`:for(let r of e.changes)n.push(...kt(r,t))}return n}function At(e,t){switch(e.type){case`removeField`:case`removeIndex`:case`removeConstraint`:return`major`;case`modifyField`:{let t=e.changes;return t.required===!0||t.type!==void 0||t.schema!==void 0||t.unique===!0?`major`:t.deprecated?`minor`:`patch`}case`modifyIndex`:{let t=e.changes;return t.unique!==void 0||t.fields!==void 0?`major`:`minor`}case`modifyConstraint`:{let n=t?.constraints?.[e.id];return n&&`predicate`in e.changes&&e.changes.predicate!==void 0||n&&`parameters`in e.changes&&e.changes.parameters!==void 0?`major`:`minor`}case`addField`:case`addIndex`:case`addConstraint`:case`addSchema`:case`deprecateField`:return`minor`;case`modifySchema`:{let n=`patch`;for(let r of e.changes){let e=At(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}default:return`patch`}}function jt(e,t){let n=`patch`;for(let r of e){let e=At(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}function Mt(e,t){let n=e.replace(/^v/,``).match(/^(\d+)\.(\d+)\.(\d+)/);if(!n)throw Error(`anansi: invalid version format "${e}"`);let[,r,i,a]=n.map(Number);switch(t){case`major`:r++,i=0,a=0;break;case`minor`:i++,a=0;break;case`patch`:a++}return`${r}.${i}.${a}`}function Nt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function Pt(e,t){let n={...e};for(let e of Object.keys(t)){let r=t[e];Nt(r)&&Nt(n[e])?n[e]=Pt(n[e],r):r!==void 0&&(n[e]=r)}return n}async function Ft(e){if(typeof globalThis.crypto?.subtle?.digest==`function`){let t=new TextEncoder().encode(e),n=await globalThis.crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}let{createHash:t}=await import(`node:crypto`);return t(`sha256`).update(e).digest(`hex`)}let It=function(e){return e.INVALID_SCHEMA=`INVALID_SCHEMA`,e.INVALID_MIGRATION=`INVALID_MIGRATION`,e.CHECKSUM_MISMATCH=`CHECKSUM_MISMATCH`,e.TIMEOUT=`TIMEOUT`,e.MEMORY_LIMIT=`MEMORY_LIMIT`,e.CONCURRENT_OPERATION=`CONCURRENT_OPERATION`,e.TRANSFORM_ERROR=`TRANSFORM_ERROR`,e.VERSION_NOT_FOUND=`VERSION_NOT_FOUND`,e.CIRCULAR_DEPENDENCY=`CIRCULAR_DEPENDENCY`,e.STREAM_ERROR=`STREAM_ERROR`,e.ROLLBACK_ERROR=`ROLLBACK_ERROR`,e.MISSING_TRANSFORM=`MISSING_TRANSFORM`,e}({});var L=class extends Error{code;migrationId;cause;constructor(e,t,n,r){super(e),this.code=t,this.migrationId=n,this.cause=r,this.name=`MigrationError`}};function Lt(e){return e===``||e===`/`?[]:e.slice(1).split(`/`).map(e=>e.replace(/~1/g,`/`).replace(/~0/g,`~`))}function R(e,t){let n=e;for(let e=0;e<t.length-1;e++){let r=t[e];if(typeof n!=`object`||!n)throw Error(`anansi/engine: path "${r}" not found`);n=Array.isArray(n)?n[parseInt(r,10)]:n[r]}return{parent:n,key:t[t.length-1]}}function Rt(e,t){let n=JSON.parse(JSON.stringify(e));for(let e of t){let t=Lt(e.path);switch(e.op){case`add`:{if(t.length===0){n=e.value;break}let{parent:r,key:i}=R(n,t);if(Array.isArray(r)){let t=i===`-`?r.length:parseInt(i,10);r.splice(t,0,e.value)}else r[i]=e.value;break}case`remove`:{let{parent:e,key:r}=R(n,t);Array.isArray(e)?e.splice(parseInt(r,10),1):delete e[r];break}case`replace`:{let{parent:r,key:i}=R(n,t);r[i]=e.value;break}case`move`:{let t=Lt(e.from),r=R(n,t),i=Array.isArray(r.parent)?r.parent.splice(parseInt(r.key,10),1)[0]:(()=>{let e=r.parent[r.key];return delete r.parent[r.key],e})(),a=Lt(e.path),o=R(n,a);Array.isArray(o.parent)?o.parent.splice(parseInt(o.key,10),0,i):o.parent[o.key]=i;break}case`test`:{let{parent:r,key:i}=R(n,t);if(JSON.stringify(r[i])!==JSON.stringify(e.value))throw Error(`anansi/engine: test failed at ${e.path}`);break}}}return n}async function zt(e){return Ft(JSON.stringify({id:e.id,schemaVersion:e.schemaVersion,changes:e.changes,description:e.description,rollback:e.rollback,createdAt:e.createdAt}))}async function Bt(e,t){return e.transform?typeof e.transform==`string`?e.transform.startsWith(`http://`)||e.transform.startsWith(`https://`)?Vt(e.transform,t):Ht(e.transform,t):e.transform[t]:null}async function Vt(e,t){try{let n=await fetch(e);if(!n.ok)throw new L(`Failed to fetch transform module: ${e}`,`TRANSFORM_ERROR`);let r=await n.text();if(typeof window<`u`){let e=new Blob([r],{type:`application/javascript`});return(await import(URL.createObjectURL(e))).default[t]}{let{runInNewContext:n}=await import(`node:vm`),i={module:{exports:{}},console};return n(r,i,e),i.module.exports[t]}}catch(t){throw t instanceof L?t:new L(`Failed to load remote transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}async function Ht(e,t){try{return(await import(e)).default[t]}catch(t){throw t instanceof L?t:new L(`Failed to import local transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}var Ut=class e{currentSchema;history=[];migrations=[];isProcessing=!1;constructor(e,t,n){this.currentSchema=e,t&&(this.migrations=t.sort((e,t)=>e.schemaVersion.localeCompare(t.schemaVersion))),n&&(this.history=n.sort((e,t)=>e.version.localeCompare(t.version)))}data(){return{schema:this.currentSchema,history:[...this.history],migrations:[...this.migrations]}}async add(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);if(!e.changes?.length)throw new L(`Migration must include changes`,`INVALID_MIGRATION`);let t={id:Date.now().toString(),schemaVersion:this.currentSchema.version,changes:e.changes,description:e.description,status:`pending`,rollback:e.rollback,transform:e.transform,createdAt:new Date().toISOString(),checksum:``};t.checksum=await zt(t),this.migrations.push(t)}async dryRun(t,n,r){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{this.isProcessing=!0;let i={...this.currentSchema},a=this.getRelevantMigrations(n,r),o;if(n===`forward`)o=a.reduce((e,t)=>this.applySchemaChanges(e,t.changes,t.id),i);else{let e=[...this.history];for(let t=0;t<a.length;t++){let t=e.pop();t&&(i={...t})}o=i}let s=await e.processMigrationList(t,n,a);return{newSchema:o,dataPreview:s}}catch(e){throw e instanceof L?e:new L(`Dry run failed`,`INVALID_SCHEMA`,void 0,e)}finally{this.isProcessing=!1}}async prepareMigration(){let e=this.migrations.filter(e=>e.status===`pending`);return await this.validateMigrations(e),e}async migrate(t){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);let n=await this.prepareMigration();try{this.isProcessing=!0,this.transformSchema(`forward`);let r=await e.processMigrationList(t,`forward`,n);return this.markMigrationsApplied(n),r}finally{this.isProcessing=!1}}async rollback(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);return this.migrations.filter(e=>e.status===`applied`).slice(-1)[0]?this.rollbackToVersion(this.history[this.history.length-1]?.version||this.currentSchema.version,e):e}async rollbackToVersion(t,n){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{let r=this.history.findIndex(e=>e.version===t);if(r===-1)throw new L(`Version ${t} not found in history`,`VERSION_NOT_FOUND`);let i=this.migrations.filter(e=>e.schemaVersion===t&&e.status===`applied`).sort((e,t)=>t.id.localeCompare(e.id)),a=this.history.length-r;if(a<0)return n;for(let e=0;e<a;e++)this.transformSchema(`backward`);let o=await e.processMigrationList(n,`backward`,i);return this.migrations=this.migrations.map(e=>e.schemaVersion>=t&&e.status===`applied`?{...e,status:`rolled_back`}:e),o}finally{this.isProcessing=!1}}static async processMigrationList(e,t,n){return(await Promise.all(n.map(async e=>{try{return{migration:e,transform:await Bt(e,t)}}catch(t){throw new L(`Failed to resolve transform for migration ${e.id}`,`TRANSFORM_ERROR`,e.id,t)}}))).filter(e=>!!e.transform).reduce((e,{migration:t,transform:n})=>e.pipeThrough(new TransformStream({async transform(e,r){try{let t=await n(e);r.enqueue(t)}catch(e){r.error(new L(`Data transformation failed for migration ${t.id}`,`TRANSFORM_ERROR`,t.id,e))}}})),e)}getRelevantMigrations(e,t){return[...this.migrations].filter(n=>{let r=e===`forward`?`pending`:`applied`,i=!t||n.schemaVersion.localeCompare(t)>=0;return n.status===r&&i}).sort((t,n)=>e===`forward`?t.id.localeCompare(n.id):n.id.localeCompare(t.id))}applySchemaChanges(e,t,n){let r=jt(t,e),i=Mt(e.version,r);return t.map(t=>{try{return kt(t,e)}catch(e){throw new L(`Invalid schema change`,`INVALID_SCHEMA`,n,e)}}).reduce((e,t)=>{try{return Rt(e,t)}catch(e){throw new L(`Failed to apply patch`,`INVALID_SCHEMA`,n,e)}},{...e,version:i})}async validateMigrations(e){await Promise.all(e.map(async e=>{let t=await zt(e);if(e.checksum!==t)throw new L(`Checksum mismatch`,`CHECKSUM_MISMATCH`,e.id)}))}markMigrationsApplied(e){this.migrations=this.migrations.map(t=>e.some(e=>e.id===t.id)?{...t,status:`applied`}:t)}transformSchema(e){try{if(e===`backward`){let e=this.history.pop();if(!e)throw Error(`No previous version`);this.currentSchema=e;return}let t=this.migrations.filter(e=>e.status===`pending`).flatMap(e=>e.changes);if(!t.length)return;this.history.push(structuredClone(this.currentSchema)),this.currentSchema=t.reduce((e,t)=>this.applySchemaChanges(e,[t]),this.currentSchema)}catch(e){throw e instanceof L?e:new L(`Schema transformation failed`,`INVALID_SCHEMA`,void 0,e)}}};function Wt(){return{maxDepth:20,mode:`strict`}}var z=class e{raw;constructor(e){this.raw=e}static zero(){return new e({kind:`zero`})}static fromSingle(t){return new e({kind:`single`,value:t})}static fromMultiple(t){return new e({kind:`multiple`,value:t})}static fromInline(t){return new e({kind:`inline`,value:t})}static fromFieldSchema(t){if(t==null)return e.zero();if(Array.isArray(t))return e.fromMultiple(t);let n=t;return`id`in n&&typeof n.id==`string`?e.fromSingle(t):`type`in n?e.fromInline(t):e.zero()}isZero(){return this.raw.kind===`zero`}isSingle(){return this.raw.kind===`single`}isMultiple(){return this.raw.kind===`multiple`}isInline(){return this.raw.kind===`inline`}asSingle(){if(this.raw.kind!==`single`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'single'`);return this.raw.value}asMultiple(){if(this.raw.kind!==`multiple`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'multiple'`);return this.raw.value}asInline(){if(this.raw.kind!==`inline`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'inline'`);return this.raw.value}};function Gt(e){let t=e;if(`predicate`in t&&typeof t.predicate==`string`){let t=e;return{kind:`rule`,name:t.name,description:t.description??void 0,predicate:t.predicate,parameters:t.parameters??void 0,fields:t.fields??[]}}let n=e;return{kind:`group`,name:n.name,description:n.description??void 0,operator:n.operator,rules:(n.rules??[]).map(e=>Gt(e))}}function B(e){if(!e)return{};let t={};for(let[n,r]of Object.entries(e))t[n]=Gt(r);return t}const V={issues:[],success:!0,skipped:!1},H={issues:[],success:!0,skipped:!0};function U(e){return{issues:e,success:!1,skipped:!1}}function W(e,t,n){return{code:e,message:t,path:n}}function G(e,t){return e===``?t:`${e}.${t}`}function Kt(e,t,n){return e===``?[n,[n]]:[`${e}.${n}`,[...t,n]]}function qt(e){return e===``?[]:e.split(`.`)}function K(e){let t=e.length;for(let n of e)for(let e of n)e===`[`&&t++;return t}function Jt(e,t,n){let r=[],i=[];for(let a of n){let n=qt(a);e===``?(r.push(a),i.push(n)):(r.push(G(e,a)),i.push([...t,...n]))}return[r,i]}function Yt(e,t){if(t.length===0)return[e,!0];let n=e;for(let e of t){if(n==null)return[void 0,!1];let t=e.indexOf(`[`);if(t===-1){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let t=n;if(!(e in t))return[void 0,!1];n=t[e]}else{let r=e.slice(0,t);if(r!==``){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let e=n;if(!(r in e))return[void 0,!1];n=e[r]}let i=e.slice(t).matchAll(/\[(\d+)\]/g);for(let e of i){let t=e[1];if(t===void 0)return[void 0,!1];let r=parseInt(t,10);if(!Array.isArray(n)||r>=n.length)return[void 0,!1];n=n[r]}}}return[n,!0]}function q(e,t){return Yt(e.rootData,t)}function J(e){return typeof e==`object`&&e&&!Array.isArray(e)?e:null}function Xt(e){if(e==null)return!0;let t=typeof e;return t===`string`||t===`number`||t===`boolean`||t===`bigint`||t===`symbol`}function Zt(e,t,n=!1){if(e===t)return!0;if(e===null||t===null||e===void 0||t===void 0)return e===t;let r=typeof e,i=typeof t;if(r===`string`&&i===`string`||r===`boolean`&&i===`boolean`||r===`number`&&i===`number`||n&&r===`number`&&i===`number`)return e===t;if(r!==`object`||i!==`object`)return!1;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!Zt(e[r],t[r],n))return!1;return!0}if(Array.isArray(e)!==Array.isArray(t))return!1;let a=e,o=t,s=Object.keys(a),c=Object.keys(o);if(s.length!==c.length)return!1;for(let e of s)if(!(e in o)||!Zt(a[e],o[e],n))return!1;return!0}function Qt(e){return typeof e==`number`&&Number.isInteger(e)}function $t(e){return typeof e==`number`&&!Number.isInteger(e)}function Y(e){return typeof e==`number`}var X=class{id;path;pathParts;deps;constructor(e,t,n,r=[]){this.id=e,this.path=t,this.pathParts=n,this.deps=r}getID(){return this.id}getPath(){return this.path}getPathParts(){return this.pathParts}getDependencies(){return this.deps}},en=class extends X{expectedFields;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expectedFields=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n)return V;let r=J(t);if(!r)return U([W(`TYPE_MISMATCH`,`Expected object for unexpected field check`,this.path)]);let i=[];for(let e of Object.keys(r))this.expectedFields.has(e)||i.push(W(`UNEXPECTED_FIELD`,`Unexpected field '${e}'`,G(this.path,e)));return i.length>0?U(i):V}},tn=class extends X{fieldName;parentPath;parentPathParts;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.fieldName=r,this.parentPath=i,this.parentPathParts=a}async execute(e){let[t,n]=q(e,this.parentPathParts);if(!n&&this.parentPath!==``)return V;if(t==null)return H;let r=J(t);return r?this.fieldName in r?V:U([W(`REQUIRED_FIELD_MISSING`,`Required field '${this.fieldName}' is missing`,this.path)]):U([W(`INVALID_DATA_STRUCTURE`,`Cannot check for required fields on non-object parent`,this.parentPath)])}},nn=class extends X{expected;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expected=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;switch(this.expected){case`string`:if(typeof t==`string`)return V;break;case`boolean`:if(typeof t==`boolean`)return V;break;case`number`:if(Y(t))return V;break;case`integer`:if(Qt(t))return V;break;case`decimal`:if($t(t))return V;break;case`bytes`:if(t instanceof Uint8Array||Array.isArray(t)&&t.every(e=>typeof e==`number`))return V;break;case`record`:if(J(t)!==null)return V;break;case`unknown`:return V;default:return V}return U([W(`TYPE_MISMATCH`,`Expected ${this.expected}, got ${typeof t} (${JSON.stringify(t)})`,this.path)])}},rn=class extends X{lookup;complex;expectNumeric;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.lookup=r,this.complex=i,this.expectNumeric=a}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(typeof t!=`string`&&typeof t!=`number`&&typeof t!=`boolean`)return U([W(`TYPE_MISMATCH`,`Expected scalar for enum, got ${typeof t}`,this.path)]);if(this.lookup.has(t))return V;for(let e of this.complex)if(Zt(t,e,this.expectNumeric))return V;return U([W(`ENUM_VIOLATION`,`Value ${JSON.stringify(t)} is not in the allowed list`,this.path)])}},an=class extends X{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return U([W(`ARRAY_TYPE_MISMATCH`,`Expected array`,this.path)]);let r=K(this.pathParts);if(r>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let i=[],a=e.maxDepth-r;for(let n=0;n<t.length;n++){let r=t[n],o=`${this.path}[${n}]`,s=await this.graph.traverse(e.functionMap,{item:r},e.mode,a,e.originalRoot);for(let e of s)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,o):e.path})}return i.length>0?U(i):V}},on=class extends X{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;let r=J(t);if(!r)return U([W(`OBJECT_TYPE_MISMATCH`,`Expected object for record, got ${typeof t}`,this.path)]);let i=K(this.pathParts);if(i>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let a=[],o=e.maxDepth-i;for(let[t,n]of Object.entries(r)){let r=G(this.path,t),i=await this.graph.traverse(e.functionMap,{item:n},e.mode,o,e.originalRoot);for(let e of i)a.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,r):e.path})}return a.length>0?U(a):V}},sn=class extends X{itemGraph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.itemGraph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return U([W(`SET_TYPE_MISMATCH`,`Expected array for set validation`,this.path)]);if(this.itemGraph){let n=K(this.pathParts);if(n>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=e.maxDepth-n,i=[];for(let n=0;n<t.length;n++){let a=`${this.path}[${n}]`,o=await this.itemGraph.traverse(e.functionMap,{item:t[n]},e.mode,r,e.originalRoot);for(let e of o)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,a):e.path})}if(i.length>0)return U(i)}if(t.length<=1)return V;let r=new Map,i=[];for(let e=0;e<t.length;e++){let n=t[e];if(Xt(n)){let t=r.get(n);if(t!==void 0)return U([W(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t})`,this.path)]);r.set(n,e)}else{let t=i.find(e=>Zt(n,e.val,!1));if(t)return U([W(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t.index})`,this.path)]);i.push({val:n,index:e})}}return V}},cn=class extends X{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){let[t,n]=q(e,this.pathParts);if(!n)return V;if(!Array.isArray(t))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry must be an array of coordinate arrays`,this.path)]);for(let e=0;e<t.length;e++){let n=t[e];if(!Array.isArray(n))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry inner element at index ${e} must be an array`,`${this.path}[${e}]`)]);for(let t=0;t<n.length;t++)if(!Y(n[t]))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry element at [${e}][${t}] is not a valid number`,`${this.path}[${e}][${t}]`)])}return V}},ln=class extends X{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){return V}},un=class extends X{validationGraph;schemaName;constructor(e,t,n,r,i,a=[]){super(e,t,n,a),this.validationGraph=r,this.schemaName=i}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;let r=J(t);if(!r)return U([W(`TYPE_MISMATCH`,`Expected object for recursive schema ${this.schemaName}, got ${typeof t}`,this.path)]);if(K(this.pathParts)>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Recursive schema '${this.schemaName}' exceeds maximum depth of ${e.maxDepth}`,this.path)]);let i=(await this.validationGraph.traverse(e.functionMap,r,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path===``?this.path:G(this.path,e.path)}));return i.length>0?U(i):V}},dn=class extends X{graphs;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graphs=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(K(this.pathParts)>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=await Promise.all(this.graphs.map(async(n,r)=>{let i={root:t},a=(await n.traverse(e.functionMap,i,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path.startsWith(`root`)?e.path.replace(`root`,this.path):e.path,message:`[variant ${r}] ${e.message}`})),o=a.length===0;return!o&&a.length===0&&a.push(W(`INTERNAL_ERROR`,`Variant ${r} failed without reporting any issues`,this.path)),{ok:o,issues:a}}));return r.some(e=>e.ok)?V:U([W(`UNION_MISMATCH`,`Value at '${this.path}' did not match any variant of the union`,this.path),...r.flatMap(e=>e.issues)])}},fn=class extends X{constraint;fieldPaths;fieldPathParts;constraintPath;constraintPathParts;scope;constructor(e,t,n,r,i,a,o,s,c,l=[]){super(e,t,n,l),this.constraint=r,this.fieldPaths=i,this.fieldPathParts=a,this.constraintPath=o,this.constraintPathParts=s,this.scope=c}async execute(e){if(this.constraint.kind!==`rule`)return U([W(`INTERNAL_ERROR`,`ConstraintNode '${this.constraint.name}': expected a rule, got a group`,this.constraintPath)]);switch(this.scope){case`global`:return this.executeGlobalConstraint(e,this.constraint);case`recursive`:return this.executeRecursiveConstraint(e,this.constraint)}}executeGlobalConstraint(e,t){return gn(e,t.name,this.constraintPath,this.fieldPaths,this.fieldPathParts,()=>_n({...e,data:e.rootData},t,this.constraintPath))}executeRecursiveConstraint(e,t){let[n,r]=q(e,this.constraintPathParts);if(!r)return Promise.resolve(H);let i=this.fieldPaths.map(e=>this.constraintPath!==``&&e.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPath.length+1):e),a=this.fieldPathParts.map((e,t)=>{let n=this.fieldPaths[t]??``;return this.constraintPath!==``&&n.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPathParts.length):e});return gn(e,t.name,this.constraintPath,i,a,()=>_n({...e,data:n},t,this.constraintPath))}},pn=class extends X{group;name;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.group=r,this.name=i}async execute(e){let{allPaths:t,allParts:n}=hn(this.group,this.path,this.pathParts);return gn(e,this.name,this.path,t,n,()=>this.executeGroup(e,this.group,this.path,this.name))}async executeGroup(e,t,n,r){let i=[],a=[];for(let o of t.rules){let t;if(o.kind===`rule`)t=await _n(e,o,n);else if(o.kind===`group`)t=await this.executeGroup(e,o,n,o.name);else return U([W(`INTERNAL_ERROR`,`Unknown constraint kind in group '${r}'`,n)]);i.push(t.success),t.success||a.push(...t.issues)}return mn(t.operator,i)?V:U([W(`CONSTRAINT_GROUP_VIOLATION`,`Constraint group '${r}' failed`,n),...a])}};function mn(e,t){switch(e){case`and`:return t.every(Boolean);case`or`:return t.some(Boolean);case`not`:return t.length===1&&!t[0];case`nor`:return t.every(e=>!e);case`xor`:return t.filter(Boolean).length===1;case`nand`:return!t.every(Boolean);case`xnor`:return t.filter(Boolean).length!==1;default:return!1}}function hn(e,t,n){let r=new Map;function i(e){for(let a of e.rules)if(a.kind===`rule`){let[e,i]=Jt(t,n,a.fields);for(let t=0;t<e.length;t++){let n=e[t],a=i[t];r.set(n,a)}}else a.kind===`group`&&i(a)}i(e);let a=Array.from(r.keys());return{allPaths:a,allParts:a.map(e=>r.get(e))}}async function gn(e,t,n,r,i,a){let o=[],s=[];for(let t=0;t<r.length;t++){let n=i[t]??[],a=r[t]??``,[,c]=q(e,n);c?o.push(a):s.push(a)}if(s.length===0)return a();if(o.length===0)switch(e.mode){case`strict`:return U([W(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:case`loose`:return H}switch(e.mode){case`strict`:return U([W(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:return U([W(`CONSTRAINT_PARTIAL_UPDATE`,`Constraint '${t}' couples fields ${JSON.stringify(r)}. Cannot update only ${JSON.stringify(o)} - all coupled fields must be updated together`,n)]);case`loose`:return H}}async function _n(e,t,n){let r=e.functionMap[t.predicate];if(!r)return U([W(`MISSING_PREDICATE`,`Predicate '${t.predicate}' not found`,n)]);let i=await r({root:e.originalRoot,data:e.data,keys:t.fields,parameters:t.parameters});return i.length>0?U(i.map(e=>({...e,path:e.path===``?n:G(n,e.path)}))):V}var vn=class{constraints=new Map;add(e,t,n,r,i){let a=this.constraints.get(e);(!a||n>=a.specificity)&&this.constraints.set(e,{constraint:t,specificity:n,basePath:r,scope:i})}getEffective(){return Array.from(this.constraints.values())}};function yn(){return new vn}var bn=class{buildingSchemas=new Map;recursiveGraphCache=new Map;isRecursive(e){return(this.buildingSchemas.get(e)??0)>0}markBuilding(e){this.buildingSchemas.set(e,(this.buildingSchemas.get(e)??0)+1)}unmarkBuilding(e){let t=(this.buildingSchemas.get(e)??0)-1;t<=0?this.buildingSchemas.delete(e):this.buildingSchemas.set(e,t)}makeGraphCacheKey(e,t){let n=Object.keys(t).sort();return n.length===0?e:`${e}:${n.join(`,`)}`}async getOrBuildRecursiveGraph(e,t,n,r){let i=this.makeGraphCacheKey(e,n),a=this.recursiveGraphCache.get(i);if(a)return a;let o=new On;this.recursiveGraphCache.set(i,o),this.markBuilding(e);try{let e=xn(t,r);await o.buildFromSchema(e,``,[],new Map,t,n,r,this,!1,!1),o.finalize()}catch(e){throw this.recursiveGraphCache.delete(i),e}finally{this.unmarkBuilding(e)}return o}};function xn(e,t){let n=`fields`in e?e.fields:{},r=e.constraints??{},i={name:e.name,version:`0`,fields:n,constraints:r};return e.description!==void 0&&(i.description=e.description),t.schemas!==void 0&&(i.schemas=t.schemas),i}function Z(e){return`type`in e&&e.type?e.type:`fields`in e&&e.fields&&Object.keys(e.fields).length>0?`object`:`unknown`}function Sn(e){switch(e){case`object`:case`array`:case`set`:case`record`:case`union`:case`composite`:case`enum`:case`geometry`:return!0;default:return!1}}function Cn(e){let t=[];for(let n of e.rules)n.kind===`rule`?t.push(...n.fields):n.kind===`group`&&t.push(...Cn(n));return t}function wn(e,t){let n={},r=e.constraints??{},i=e=>e===t||e.startsWith(t+`.`);for(let[e,t]of Object.entries(r)){let r=Gt(t);r.kind===`rule`?r.fields.some(i)&&(n[e]=r):r.kind===`group`&&Cn(r).some(i)&&(n[e]=r)}return n}function Tn(e,t,n,r){let i=yn(),a=r===``?`global`:`recursive`;for(let[,t]of Object.entries(e))i.add(t.name,t,1,r,a);for(let[,e]of Object.entries(t))i.add(e.name,e,2,r,a);for(let[,e]of Object.entries(n))i.add(e.name,e,3,r,a);return i.getEffective()}function En(e){let t=new Map,n=[];for(let r of e)r!=null&&(typeof r==`string`||typeof r==`number`||typeof r==`boolean`?t.set(r,!0):n.push(r));return{lookup:t,complex:n}}function Dn(e,t){let n=new Set;function r(e){let i=Z(e);if(i===`object`||i===`unknown`){if(`fields`in e&&e.fields)for(let t of Object.values(e.fields))n.add(t.name)}else if(i===`union`||i===`composite`){if(!(`schema`in e)||!e.schema)return;let n=z.fromFieldSchema(e.schema);if(!n.isMultiple())return;for(let e of n.asMultiple()){let n=t.schemas?.[e.id];n&&r(n)}}}for(let n of e){let e=t.schemas?.[n.id];e&&r(e)}return n}var On=class e{nodes=new Map;dependencies=new Map;visitedState=new Map;executionOrder=[];nextNodeID=1;nullablePaths=new Set;buildNodeID(){return this.nextNodeID++}addNode(e){let t=e.getID();this.nodes.has(t)||(this.nodes.set(t,e),this.dependencies.set(t,e.getDependencies()))}createUnexpectedFieldsNode(e,t,n,r=[]){return new en(this.buildNodeID(),e,t,n,r)}createRequiredFieldNode(e,t,n,r,i,a=[]){return new tn(this.buildNodeID(),e,t,n,r,i,a)}createTypeCheckNode(e,t,n,r=[]){return new nn(this.buildNodeID(),e,t,n,r)}createCompletionNode(e,t,n){return new ln(this.buildNodeID(),e,t,n)}finalize(){this.visitedState=new Map;let e=[],t=!1,n=r=>{if(t)return;let i=this.visitedState.get(r)??0;if(i===1){t=!0;return}if(i!==2){this.visitedState.set(r,1);for(let e of this.dependencies.get(r)??[])n(e);this.visitedState.set(r,2),e.push(r)}};for(let e of this.nodes.keys())if((this.visitedState.get(e)??0)===0&&(n(e),t))throw Error(`Circular dependency detected in validation graph at node ${e}`);this.executionOrder=e}async traverse(e,t,n,r,i){let a={originalRoot:i,rootData:t,data:t,functionMap:e,maxDepth:r,mode:n,visited:new Map,issues:[]};for(let e of this.executionOrder){let t=this.nodes.get(e),r=this.dependencies.get(e)??[],i=!1;for(let t of r){let n=a.visited.get(t);if(n!==void 0&&!n){a.visited.set(e,!0),i=!0;break}}if(i)continue;let o=t.getPathParts(),s=t.getPath(),[c,l]=q(a,o),u=t instanceof tn,d=this.nullablePaths.has(s);if((!l||c===void 0)&&!u){a.visited.set(e,!0);continue}if(c===null){if(u)continue;if(d){a.visited.set(e,!0);continue}}let f=await t.execute(a);if(a.visited.set(e,f.success),f&&!f.skipped&&f.issues.length>0)for(let e of f.issues)switch(n){case`strict`:a.issues.push(e);break;case`partialStrict`:e.code!==`REQUIRED_FIELD_MISSING`&&a.issues.push(e);break;case`loose`:e.code!==`REQUIRED_FIELD_MISSING`&&e.code!==`UNEXPECTED_FIELD`&&a.issues.push(e)}}return a.issues}async buildFromSchema(e,t,n,r,i,a,o,s,c,l){let u=[],d=i&&`fields`in i&&i.fields?i.fields:e.fields,f=null;if(!c){let e=new Set(Object.values(d).map(e=>e.name));f=this.createUnexpectedFieldsNode(t,n,e),this.addNode(f),u.push({id:f.getID(),deps:[]})}let p=[],m=[];for(let i of Object.values(d)){let a=await this.buildFieldNodes(i,t,n,p,e,r,o,s,l);m.push(...a)}let h=Tn(B(i?i.constraints:e.constraints),a,o&&t!==``?wn(o,t):{},t),ee=m.map(e=>e.id),g=this.buildFromEffectiveConstraints(h,n,ee,r);if(g.length>0){let e=this.createCompletionNode(t,n,g);this.addNode(e),u.push({id:e.getID(),deps:g})}return u}async buildFieldNodes(e,t,n,r,i,a,o,s,c){let[l,u]=Kt(t,n,e.name);e.nullable!==!1&&this.nullablePaths.add(l);let d=r,f=[];if(e.required){let r=this.createRequiredFieldNode(l,u,e.name,t,n,d);this.addNode(r),d=[r.getID()],f.push({id:r.getID(),deps:d})}if(!Sn(e.type)){let t=this.createTypeCheckNode(l,u,e.type,d);this.addNode(t),d=[t.getID()],f.push({id:t.getID(),deps:d})}let p=await this.buildFieldTypeNodes(e,l,u,d,i,a,o,s,c);return f.push(...p),f}async buildFieldTypeNodes(e,t,n,r,i,a,o,s,c){let l=[],u=null;switch(e.type){case`enum`:u=await this.buildEnumNode(e,t,n,r,o);break;case`array`:u=await this.buildContainerNode(e,t,n,r,o,s,`array`,`array items`);break;case`set`:{let i=await this.buildContainerNode(e,t,n,r,o,s,`array`,`set elements`),a=i instanceof an?i.graph:null;u=new sn(this.buildNodeID(),t,n,a,r);break}case`record`:u=await this.buildContainerNode(e,t,n,r,o,s,`record`,`record items`);break;case`union`:u=await this.buildUnionNode(e,t,n,r,o,s);break;case`composite`:{let i=await this.buildCompositeNode(e,t,n,r,o,s);return l.push(...i),l}case`geometry`:u=new cn(this.buildNodeID(),t,n,r);break;case`object`:{let r=await this.buildObjectFieldNodes(e,t,n,i,a,o,s,c);return l.push(...r),l}default:return l}return u&&(this.addNode(u),l.push({id:u.getID(),deps:u.getDependencies()})),l}async buildObjectFieldNodes(e,t,n,r,i,a,o,s){let c=[],l=z.fromFieldSchema(e.schema);if(l.isZero())throw Error(`FieldSchemaReference is zero/uninitialized at path '${t}'`);if(!l.isSingle())throw Error(`Object field must use a single schema reference at path '${t}'`);let u=l.asSingle(),d=a.schemas?.[u.id];if(!d)throw Error(`Nested schema '${u.id}' not found for field '${e.name}' at path '${t}'`);if(o.isRecursive(u.id)){let e=B(u.constraints),r=await o.getOrBuildRecursiveGraph(u.id,d,e,a),i=new un(this.buildNodeID(),t,n,r,u.id);return this.addNode(i),c.push({id:i.getID(),deps:[]}),c}let f=xn(d,a);o.markBuilding(u.id);try{let e=await this.buildFromSchema(f,t,n,i,d,B(u.constraints),a,o,s,!1),r=new ln(this.buildNodeID(),t,n,e.map(e=>e.id));this.addNode(r),c.push({id:r.getID(),deps:e.map(e=>e.id)})}finally{o.unmarkBuilding(u.id)}return c}async buildContainerNode(t,n,r,i,a,o,s,c){let l=e=>s===`array`?new an(this.buildNodeID(),n,r,e,i):new on(this.buildNodeID(),n,r,e,i),u=z.fromFieldSchema(t.schema);if(u.isZero())return l(null);if(u.isSingle()){let e=u.asSingle();if(e.id!==``){let t=a.schemas?.[e.id];if(!t)throw Error(`Nested schema '${e.id}' not found for ${c} at path '${n}'`);let r=Z(t),i=r===`composite`||r===`union`,s;s=r===`object`?z.fromSingle({id:e.id}):`schema`in t&&t.schema?z.fromFieldSchema(t.schema):z.zero();let u=s.isZero()?void 0:s.isSingle()?s.asSingle():s.isMultiple()?s.asMultiple():s.asInline(),d={name:`item`,type:r};u!==void 0&&(d.schema=u);let f=`default`in t?t.default:void 0;return f!==void 0&&(d.default=f),l(await this.createSubGraph(`item`,d,n,a,o,i,!1))}}if(u.isInline()){let t=u.asInline();if(!t.type)throw Error(`Inline descriptor missing 'type' field at path '${n}'`);let r=new e,i=new Map,s=t.type;if(s===`object`){let e={name:`__inline_record`,version:`0`,fields:{}};a.schemas!==void 0&&(e.schemas=a.schemas),await r.buildFromSchema(e,``,[],i,null,{},a,o,!0,!1)}else{let e=r.createTypeCheckNode(`item`,[`item`],s);r.addNode(e)}return r.finalize(),l(r)}throw Error(`Invalid schema reference for ${c} at path '${n}'`)}async buildEnumNode(e,t,n,r,i){let a=z.fromFieldSchema(e.schema);if(a.isZero())throw Error(`Enum field must have schema reference at path '${t}'`);let o=a.isSingle()?[a.asSingle()]:a.isMultiple()?a.asMultiple():[];if(o.length===0&&!a.isInline())throw Error(`Enum schema reference must be single, multiple, or inline at path '${t}'`);if(a.isInline()){let e=a.asInline();if(!e.type)throw Error(`Inline enum descriptor missing 'type' at path '${t}'`);if(!e.values||e.values.length===0)throw Error(`Inline enum descriptor missing 'values' at path '${t}'`);let{lookup:i,complex:o}=En(e.values),s=e.type===`number`||e.type===`integer`||e.type===`decimal`;return new rn(this.buildNodeID(),t,n,i,o,s,r)}let s=new Map,c=[],l=null;for(let e of o){let n,r,a;if(e.id!==``){let o=i.schemas?.[e.id];if(!o)throw Error(`Enum schema '${e.id}' not found at path '${t}'`);if(!(`values`in o)||!o.values?.length)throw Error(`Enum schema '${e.id}' has no values defined at path '${t}'`);a=o.type,{lookup:n,complex:r}=En(o.values)}else throw Error(`Inline descriptor in named-reference array is not permitted at path '${t}'`);for(let[e]of n)s.set(e,!0);c=c.concat(r),l||=a}let u=l===`number`||l===`integer`||l===`decimal`;return new rn(this.buildNodeID(),t,n,s,c,u,r)}async buildUnionNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Union field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Nested schema '${e.id}' not found for union at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:Z(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!1);c.push(o)}return new dn(this.buildNodeID(),t,n,c,r)}async buildCompositeNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Composite field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=Dn(s,i),l=this.createUnexpectedFieldsNode(t,n,c);this.addNode(l);let u=[l.getID()],d=[];for(let e of s){let r=i.schemas?.[e.id];if(!r)throw Error(`Nested schema '${e.id}' not found for composite at path '${t}'`);let o=Z(r);if(o===`object`||o===`unknown`){let o=xn(r,i);a.markBuilding(e.id);let s;try{s=await this.buildFromSchema(o,t,n,new Map,r,B(e.constraints),i,a,!0,!0)}finally{a.unmarkBuilding(e.id)}for(let e of s)e.deps.includes(l.getID())||(e.deps=[l.getID(),...e.deps],this.dependencies.set(e.id,e.deps));d.push(...s)}else if(o===`union`){if(!(`schema`in r)||!r.schema)throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let o=z.fromFieldSchema(r.schema);if(!o.isMultiple())throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Union variant schema '${e.id}' not found at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:Z(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!0);c.push(o)}let l=new dn(this.buildNodeID(),t,n,c,u);this.addNode(l),d.push({id:l.getID(),deps:u})}else throw Error(`Composite part '${e.id}' has unsupported effective type '${o}'; composite parts must be object or union schemas at path '${t}'`)}return[{id:l.getID(),deps:[]},...d]}buildFromEffectiveConstraints(e,t,n,r){let i=[];for(let a of e){let e=this.buildFromConstraintRuleWithScope(a.constraint,a.basePath,t,n,r,a.scope);i.push(...e)}return i}buildFromConstraintRuleWithScope(e,t,n,r,i,a){let o=`${t}:${e.name}`;if(i.get(o))return[];if(i.set(o,!0),e.kind===`rule`){let[i,o]=Jt(t,n,e.fields),s=new fn(this.buildNodeID(),t,n,e,i,o,t,n,a,r);return this.addNode(s),[s.getID()]}if(e.kind===`group`){let i=new pn(this.buildNodeID(),t,n,e,e.name,a,r);return this.addNode(i),[i.getID()]}return[]}async createSubGraph(t,n,r,i,a,o,s){let c={name:`subgraph_${r}`,version:`0`,fields:{[t]:n}};i.schemas!==void 0&&(c.schemas=i.schemas);let l=new e,u=new Map;return await l.buildFromSchema(c,``,[],u,null,{},i,a,o,s),l.finalize(),l}},kn=class e{fmap;graph;config;constructor(e,t,n){this.graph=e,this.fmap=t,this.config=n}async validate(e){return this.graph.traverse(this.fmap,e,`strict`,this.config.maxDepth,e)}async validatePartial(e){return this.graph.traverse(this.fmap,e,`partialStrict`,this.config.maxDepth,e)}async validateLoose(e){return this.graph.traverse(this.fmap,e,`loose`,this.config.maxDepth,e)}static async create(t,n,r=Wt()){let i=new On,a=new bn,o=new Map;return await i.buildFromSchema(t,``,[],o,null,{},t,a,!1,!1),i.finalize(),new e(i,n,r)}},An={version:`1.0.0`,name:`Schema`,description:`Meta-schema defining the structure of schema definitions`,fields:{"019d7775-6565-7755-904d-2d0cdf0e1b86":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6565-73d6-a7be-130b3c1e93c8":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6565-722f-9a5a-2810f537a61c":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6565-7fe8-86f6-57a8a305a314":{name:`schemas`,type:`record`,schema:{id:`019d7775-6565-7db3-9bb0-be9fc50550bd`}},"019d7775-6565-7c1f-a6f7-aeb5c4eb35e4":{name:`name`,required:!0,type:`string`},"019d7775-6565-7545-9e25-fc471f57bcfb":{name:`description`,type:`string`},"019d7775-6565-7bdc-a9d4-776b1f163346":{name:`version`,required:!0,type:`string`},"019d7775-6565-74ed-8a52-12027f3c577d":{name:`metadata`,type:`record`}},schemas:{"019d7775-6566-7da1-b24a-67bd81dd2b0e":{name:`Unknown`,type:`unknown`},"019d7775-6565-7db3-9bb0-be9fc50550bd":{name:`NestedSchema`,fields:{"019d7775-6566-77a1-9b36-bbd57f927d11":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6567-7931-b2d6-fb52039c28b5":{name:`type`,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6567-7742-90ae-a96178ae505d":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"019d7775-6567-7709-a7e3-ed75f833da15":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-748e-b099-50c76a5b79c2":{name:`name`,required:!0,type:`string`},"019d7775-6566-74df-a8df-038cc26af034":{name:`description`,type:`string`},"019d7775-6567-748e-bcad-76068cc4739f":{name:`metadata`,type:`record`},"019d7775-6567-7022-b7fb-3975ccae2b72":{name:`concrete`,type:`boolean`},"019d7775-6567-7f25-b7e2-acc72435964f":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6567-750b-a618-5bcdc4e5d04a":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6567-75c4-b399-eb4764617462":{name:`default`,type:`unknown`}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`}}},"019d7775-6565-71ef-a08f-cdcdb116f947":{name:`IndexCondition`,fields:{"019d7775-6567-75ef-8e82-347da30f06fd":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7a70-8b56-e55704397fc2`}},"019d7775-6567-7466-a873-e7866e9d031f":{name:`field`,required:!0,type:`string`},"019d7775-6567-7526-9704-d19fb4eb8d8d":{name:`value`,required:!0,type:`unknown`},"a2f03bc4-e620-4b7f-a2ad-8271471ac1e4":{name:`description`,type:`string`},"8782f46d-5f1f-4102-9c3c-917752d8c9d1":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-713e-8355-df812c6bb8c9":{name:`index_condition_exclusive_type`,predicate:`index_condition_type_exclusive`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`}}},"019d7775-6565-7d6a-a531-47e7ea838bc4":{name:`IndexConditionGroup`,fields:{"019d7775-6567-7b4e-893f-ee7afb0c4be1":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6567-7af2-817f-bc5b8732c198":{name:`conditions`,required:!0,type:`array`,schema:{id:`019d7775-6565-74a3-a6e0-881a0148fc59`}},"670ebd05-3bce-4da7-a39b-20cf9ba67abe":{name:`description`,type:`string`},"8e60fb23-0e3f-4ae5-bfff-fd20da1da756":{name:`metadata`,type:`record`}}},"019d7775-6565-794d-8aba-bdc5e3cbe97e":{name:`ConstraintUnion`,type:`union`,schema:[{id:`019d7775-6565-703f-95cf-e7511d33c91b`},{id:`019d7775-6565-7ea4-a820-e8bcdbc690bc`}]},"019d7775-6566-7786-8769-c13eb5a883d9":{name:`InlineTypeDescriptor`,fields:{"019d7775-6568-713f-867f-93dde123ccfd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e05-ab1b-6f768f459927`}},"019d7775-6568-7ae5-95b7-56c97722c732":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"347a2348-014b-41a7-80ad-a4e8d364ef5d":{name:`description`,type:`string`},"7d0f2347-3dfe-4e37-8235-1f9122e045dc":{name:`metadata`,type:`record`}}},"019d7775-6566-7e3b-860a-c4aa4adf5d6d":{name:`LogicalOperatorEnum`,type:`string`,values:[`and`,`or`,`not`,`nor`,`xor`,`nand`,`xnor`]},"019d7775-6566-7f2f-a009-0af144502aac":{name:`IndexOrderEnum`,type:`string`,values:[`asc`,`desc`]},"019d7775-6565-7f15-b0f4-27a4f40cbf5c":{name:`Field`,fields:{"019d7775-6566-727e-b4ff-4e6e8b800808":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6566-79e9-a60b-1c15e2154580":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-7a4c-877d-262c18adb028":{name:`name`,required:!0,type:`string`},"019d7775-6566-7b93-aa4c-b1e3869512d8":{name:`description`,type:`string`},"019d7775-6566-7ed5-9fb3-33a67b90dbbe":{name:`default`,type:`unknown`},"019d7775-6566-722b-8132-d5d8c4c9b2b6":{name:`required`,type:`boolean`},"a1000000-0000-0000-0000-000000000001":{name:`nullable`,type:`boolean`},"019d7775-6566-7367-9664-adb25994bf41":{name:`deprecated`,type:`boolean`},"019d7775-6566-7fc8-82c6-735838cdfe21":{name:`unique`,type:`boolean`},"95ab0d3b-352c-4aff-a43a-81ab889aaa42":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-7605-8bfb-c27365b73581":{name:`enum_fields_valid`,predicate:`enum_fields_valid`},"019d7775-6563-74fe-a5ae-4eef62a128d9":{name:`arrays_require_schema`,predicate:`collection_requires_schema`},"019d7775-6563-7080-ae67-bad5cd864f07":{name:`objects_require_schema`,predicate:`object_requires_schema`},"019d7775-6563-7e83-92c4-0258a8fc0d3a":{name:`unions_require_multiple_schemas`,predicate:`union_requires_multiple_schemas`},"019d7775-6564-7c60-9f9f-aeeabe05c1b5":{name:`default_value_type_match`,predicate:`default_matches_type`},"019d7775-6564-77ca-94bc-30a07da76c3d":{name:`records_allow_optional_schema`,predicate:`record_schema_cardinality`},"019d7775-6564-7768-8995-72c282cfe69f":{name:`inline_type_descriptor_valid`,predicate:`inline_type_descriptor_valid`},"019d7775-6563-7c55-a6f3-ac8f087d89d1":{name:`primitives_no_schema`,predicate:`primitives_prohibit_schema`},"019d7775-6563-73ce-aef1-86943b54a1ca":{name:`composites_require_multiple_schemas`,predicate:`composite_requires_multiple_schemas`},"019d7775-6564-719d-9948-67dd885e7632":{name:`object_referenced_schema_has_fields`,predicate:`object_referenced_schema_has_fields`},"019d7775-6564-75ce-a807-02396e0b8394":{name:`composite_referenced_schemas_must_be_objects`,predicate:`composite_referenced_schemas_must_be_objects`},"019d7775-6564-75bc-abf5-6ddd81b7d2d4":{name:`schema_reference_form_correct`,predicate:`schema_reference_form_correct`}}},"019d7775-6565-7ab6-9c65-ab60632aeca8":{name:`Constraint`,type:`composite`,schema:[{id:`019d7775-6565-7068-9cb9-28460e0368f0`},{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}]},"019d7775-6566-765a-b142-157d1d6367b7":{name:`FieldTypeEnum`,type:`string`,values:[`unknown`,`string`,`number`,`integer`,`decimal`,`boolean`,`array`,`set`,`enum`,`object`,`record`,`union`,`composite`,`geometry`,`bytes`]},"019d7775-6566-7a70-8b56-e55704397fc2":{name:`ComparisonOperatorEnum`,type:`string`,values:[`eq`,`neq`,`lt`,`lte`,`gt`,`gte`,`in`,`nin`,`contains`,`ncontains`,`exists`,`nexists`]},"019d7775-6565-74a3-a6e0-881a0148fc59":{name:`IndexConditionUnion`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"019d7775-6565-7068-9cb9-28460e0368f0":{name:`ConstraintMetadata`,fields:{"019d7775-6567-7017-9068-5bfcd1f9fef0":{name:`name`,required:!0,type:`string`},"019d7775-6568-7b0d-ad28-391874b5e3de":{name:`description`,type:`string`},"ee8ea6de-95f9-48e5-a047-4744b39e1c8f":{name:`metadata`,type:`record`}}},"019d7775-6565-703f-95cf-e7511d33c91b":{name:`ConstraintRule`,fields:{"019d7775-6568-7ad6-9e45-7fdbdef3fc7e":{name:`predicate`,required:!0,type:`string`},"019d7775-6568-7221-a4de-7e82d6c9f6bc":{name:`parameters`,type:`unknown`},"019d7775-6568-7d8b-a230-328fe5d352e8":{name:`fields`,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`},"019d7775-6563-752b-bf9d-bf8936472250":{name:`constraint_rule_requires_predicate`,predicate:`constraint_rule_requires_predicate`},"019d7775-6564-7fc2-bbdb-0fc59e567f2b":{name:`constraint_fields_exist`,predicate:`constraint_fields_exist`}}},"019d7775-6565-7ea4-a820-e8bcdbc690bc":{name:`ConstraintGroup`,fields:{"019d7775-6568-7ebf-a1c3-829de95d9216":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6568-76d9-a21c-fb289c36cc70":{name:`rules`,required:!0,type:`array`,schema:{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`}}},"019d7775-6565-7f2a-956b-35b40caa8ec7":{name:`SchemaReference`,fields:{"019d7775-6568-762c-932e-89b85f08a951":{name:`id`,required:!0,type:`string`},"019d7775-6568-77eb-bd9d-d874a2354fd5":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6568-7ed9-99b3-90cca439ee2e":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"abb42217-0f28-4501-ab12-c54bfe5d1492":{name:`description`,type:`string`},"ed9a369c-1c98-4bc7-845b-92fa81be31f8":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-79f2-9d64-d6b8c2696deb":{name:`schema_reference_id_required`,predicate:`schema_reference_id_required`}}},"019d7775-6565-71bc-84f3-044dc089d50c":{name:`SchemaReferenceArray`,type:`array`,schema:{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`}},"019d7775-6566-7e05-ab1b-6f768f459927":{name:`InlineTypeEnum`,type:`string`,values:[`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]},"019d7775-6566-7df4-bc09-67956251c4a2":{name:`IndexTypeEnum`,type:`string`,values:[`normal`,`unique`,`primary`,`spatial`,`fulltext`]},"019d7775-6566-7a63-9e10-5d11c8eb607c":{name:`String`,type:`string`},"019d7775-6565-74f6-9c72-9e35d3d16b0a":{name:`Index`,fields:{"019d7775-6567-7390-96c6-8154ccc6d1ad":{name:`name`,required:!0,type:`string`},"019d7775-6567-7568-98c8-76d64b745bc7":{name:`description`,type:`string`},"019d7775-6567-7a3d-b154-1199406b7851":{name:`unique`,type:`boolean`},"019d7775-6567-7e59-a2f4-ffc431e24fdd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7df4-bc09-67956251c4a2`}},"019d7775-6567-787d-921c-fa28f53cedcd":{name:`fields`,required:!0,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}},"019d7775-6567-76c9-869a-c4e25c154583":{name:`order`,type:`enum`,schema:{id:`019d7775-6566-7f2f-a009-0af144502aac`}},"019d7775-6567-7bc2-b870-6e1ae4faa558":{name:`condition`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"75f044b7-253c-4f48-93b7-692b9626c0ea":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-7c2d-b06d-82971c0b1fa9":{name:`index_fields_exist`,predicate:`index_fields_reference_valid`},"019d7775-6564-7660-adc8-3f1ab037d695":{name:`spatial_index_on_geometry_field`,predicate:`spatial_index_on_geometry_field`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`},"019d7775-6564-7285-a734-f7eed15d25e4":{name:`index_fields_not_empty`,predicate:`index_fields_not_empty`}}}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`},"019d7775-6564-7565-b53c-49667d23c237":{name:`global_field_id_uniqueness`,predicate:`global_field_id_uniqueness`},"019d7775-6564-7a5d-bb7e-d2e9e804123f":{name:`schema_reference_integrity`,predicate:`schema_reference_exists`},"019d7775-6563-7f89-8368-395733efec86":{name:`schema_name_required`,predicate:`schema_name_required`}}};const jn=new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`geometry`,`bytes`,`unknown`]),Mn=new Set([`array`,`set`]),Nn=[`fields`],Pn=[`type`,`default`,`values`,`schema`],Fn=new Set([`number`,`integer`,`decimal`,`string`]);function Q(e){let t=e.type;return t==null?[``,!1]:[String(t),!0]}function In(e){return jn.has(e)}function $(e){return Fn.has(e)}function Ln(e){return Mn.has(e)}function Rn(e,t){let n=e.schemas;return n&&n[t]||null}function zn(e){if(e.fields){let t=e.fields;if(Object.keys(t).length>0)return!0}let t=e.type;return t===`object`||t===`record`}function Bn(e){let t=e.schema;return t==null?[[],!1]:Array.isArray(t)?[t,!0]:[[t],!1]}function Vn(e,t,n){let r=Rn(e,t);if(!r)return[W(`ENUM_NAMED_SCHEMA_NOT_FOUND`,`Enum referenced schema '${t}' does not exist`,n)];let i=[],a=r.type;if(a==null)return i.push(W(`ENUM_NAMED_SCHEMA_NO_TYPE`,`Enum referenced schema '${t}' must have a 'type' (string or numeric)`,n)),i;let o=String(a);if(!(o===`string`||$(o)))return i.push(W(`ENUM_NAMED_SCHEMA_INVALID_TYPE`,`Enum referenced schema '${t}' type must be string or numeric, got '${a}'`,n)),i;let s=r.values;if(s==null)return i.push(W(`ENUM_NAMED_SCHEMA_MISSING_VALUES`,`Enum schema '${t}' must have a 'values' array`,n)),i;let c=s;if(!Array.isArray(c)||c.length===0)return i.push(W(`ENUM_NAMED_SCHEMA_EMPTY_VALUES`,`Enum schema '${t}' 'values' must be a non-empty array`,n)),i;let l=o===`string`;return c.forEach((e,r)=>{e!=null&&(l?typeof e!=`string`&&i.push(W(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be string, got ${typeof e}`,`${n} (values[${r}])`)):$(o)&&(Y(e)||i.push(W(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be numeric (type '${o}'), got ${typeof e}`,`${n} (values[${r}])`))))}),Nn.forEach(e=>{let a=r[e];a!=null&&Object.keys(a).length>0&&i.push(W(`ENUM_NAMED_SCHEMA_HAS_FIELDS`,`Enum referenced schema '${t}' must not have '${e}' (must be a simple value schema)`,n))}),i}function Hn(e,t){let n=[],r=e.type;if(r==null)return n.push(W(`ENUM_INLINE_NO_TYPE`,`Inline enum descriptor must have a 'type' (string or numeric)`,t)),n;let i=String(r);if(!(i===`string`||$(i)))return n.push(W(`ENUM_INLINE_INVALID_TYPE`,`Inline enum descriptor type must be string or numeric, got '${r}'`,t)),n;let a=e.values;if(a==null)return n.push(W(`ENUM_INLINE_MISSING_VALUES`,`Inline enum descriptor must have a 'values' array`,t)),n;let o=a;if(!Array.isArray(o)||o.length===0)return n.push(W(`ENUM_INLINE_EMPTY_VALUES`,`Inline enum descriptor 'values' must be a non-empty array`,t)),n;let s=i===`string`;return o.forEach((e,r)=>{e!=null&&(s?typeof e!=`string`&&n.push(W(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be string, got ${typeof e}`,`${t}.values[${r}]`)):$(i)&&(Y(e)||n.push(W(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be numeric (type '${i}'), got ${typeof e}`,`${t}.values[${r}]`))))}),n}function Un(e,t,n){if(typeof e!=`object`||!e)return[W(`ENUM_REF_INVALID`,`Enum schema reference must be an object`,t)];let r=e,i=`id`in r,a=`type`in r;if(i&&a)return[W(`ENUM_REF_AMBIGUOUS`,`Enum schema reference cannot have both 'id' (named) and 'type' (inline)`,t)];if(i){let e=r.id;return e==null||e===``?[W(`ENUM_NAMED_REF_INVALID_ID`,`Named enum reference 'id' must be a non-empty string`,t)]:Vn(n,String(e),t)}return a?Hn(r,t):[W(`ENUM_REF_NO_ID_OR_TYPE`,`Enum schema reference must have either 'id' (named) or 'type' (inline)`,t)]}function Wn(e,t){let n=t.split(`.`),r=e.fields&&typeof e.fields==`object`?e.fields:null,i=null;for(let t=0;t<n.length;t++){if(!r)return null;let a=null;for(let e of Object.values(r))if(e&&typeof e==`object`&&e.name===n[t]){a=e;break}if(!a)return null;if(i=a,t<n.length-1){let t=a.schema;if(t&&typeof t==`object`&&!Array.isArray(t)){if(typeof t.id==`string`){let n=t.id,i=e.schemas?.[n];r=i&&typeof i==`object`?i.fields??null:null}else r=t.fields?t.fields:null}else r=null}}return i?{def:i}:null}const Gn={primitives_prohibit_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;return r&&In(n)&&i!=null?[W(`PRIMITIVE_HAS_SCHEMA`,`Primitive type '${n}' cannot have a schema reference`,``)]:[]},enum_fields_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`enum`)return[];if(t.schema==null)return[W(`ENUM_MISSING_SCHEMA`,`Enum field must have a schema reference`,``)];let[i,a]=Bn(t);if(i.length===0)return[W(`ENUM_NO_VALID_REFS`,`Enum field has no valid schema references`,``)];let o=[];return i.forEach((t,n)=>{let r=a?`schema[${n}]`:`schema`;o.push(...Un(t,r,e.root))}),o},composite_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;if(r&&n===`composite`){if(i==null)return[W(`COMPOSITE_MISSING_SCHEMA`,`Composite type must have schema references`,``)];if(Array.isArray(i)){if(i.length<2)return[W(`COMPOSITE_INSUFFICIENT_SCHEMAS`,`Composite must have at least 2 schema references`,``)]}else return[W(`COMPOSITE_SCHEMA_NOT_ARRAY`,`Composite 'schema' must be an array of references`,``)]}return[]},composite_referenced_schemas_must_be_objects:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`composite`)return[];let i=t.schema;if(!Array.isArray(i)||i.length===0)return[];let a=[],o=e.root,s=(e,t)=>{let n=Rn(o,e);if(!n)return[];let r=[],i=n.type;if(i!=null){let e=String(i);if(e===`composite`)return[];if(e===`union`){let e=n.schema;return Array.isArray(e)&&e.forEach((e,n)=>{typeof e==`object`&&e&&`id`in e&&r.push(...s(String(e.id),`${t}.schema[${n}]`))}),r}}return zn(n)||r.push(W(`COMPOSITE_REF_NOT_OBJECT`,`Composite schema '${e}' must effectively represent an object type (has fields, or type 'object'/'record')`,t)),r};return i.forEach((e,t)=>{typeof e==`object`&&e&&`id`in e&&a.push(...s(String(e.id),`schema[${t}]`))}),a},object_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;return r&&n===`object`&&i==null?[W(`OBJECT_MISSING_SCHEMA`,`Object type must have a schema reference`,``)]:[]},object_referenced_schema_has_fields:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let[r,i]=Q(n);if(!i||r!==`object`)return[];let a=n.schema;if(typeof a!=`object`||!a||Array.isArray(a))return[];let o=a.id;if(!o)return[];let s=Rn(t,String(o));if(!s)return[];let c=s.fields;return c==null?[W(`OBJECT_REF_NO_FIELDS`,`Object field references schema '${o}' which has no 'fields' (must be Schema mode)`,``)]:typeof c!=`object`||Object.keys(c).length===0?[W(`OBJECT_REF_EMPTY_FIELDS`,`Object field references schema '${o}' which has empty fields`,``)]:[]},spatial_index_on_geometry_field:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n||n.type!==`spatial`)return[];let r=n.fields;if(!Array.isArray(r))return[];for(let e of r){let n=String(e),r=Wn(t,n);if(!r)continue;let i=r.def.type;if(i!==`geometry`)return[W(`SPATIAL_INDEX_NON_GEOMETRY`,`Spatial index can only reference geometry fields, but field '${n}' has type '${i}'`,``)]}return[]},index_condition_value_matches_field_type:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let r=n.field;if(r==null)return[];let i=n.value,a=Wn(t,String(r));if(!a)return[];let o=a.def.type,s=!0;switch(o){case`string`:typeof i!=`string`&&i!==null&&(s=!1);break;case`boolean`:typeof i!=`boolean`&&i!==null&&(s=!1);break;case`integer`:!Qt(i)&&i!==null&&(s=!1);break;case`number`:case`decimal`:!Y(i)&&i!==null&&(s=!1)}return s?[]:[W(`INDEX_CONDITION_VALUE_TYPE_MISMATCH`,`Value for field '${r}' must be ${o}`,``)]},schema_reference_exists:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schemas||{},r=[],i=(e,t)=>{if(e&&typeof e==`object`&&`id`in e){let i=e.id;typeof i==`string`&&!n[i]&&r.push(W(`SCHEMA_REFERENCE_NOT_FOUND`,`Referenced schema '${i}' does not exist`,t))}},a=(e,t)=>{if(!(typeof e!=`object`||!e)){if(e.hasOwnProperty(`schema`)){let n=e.schema;Array.isArray(n)?n.forEach((e,n)=>i(e,`${t}.schema[${n}]`)):i(n,`${t}.schema`)}for(let[n,r]of Object.entries(e))typeof r==`object`&&r&&a(r,t?`${t}.${n}`:n)}};return a(t,``),r},default_matches_type:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.type,r=t.default;if(n===void 0||r==null)return[];let i=String(n),a=``;switch(i){case`string`:typeof r!=`string`&&(a=`must be string`);break;case`boolean`:typeof r!=`boolean`&&(a=`must be boolean`);break;case`integer`:Qt(r)||(a=`must be integer`);break;case`number`:case`decimal`:Y(r)||(a=`must be numeric`);break;case`array`:case`set`:Array.isArray(r)||(a=`must be array`);break;case`object`:case`record`:(typeof r!=`object`||Array.isArray(r))&&(a=`must be object`);break;case`geometry`:Array.isArray(r)||(a=`must be array of coordinate arrays`)}return a?[W(`DEFAULT_VALUE_TYPE_MISMATCH`,`Default value ${JSON.stringify(r)} ${a} for type ${i}`,``)]:[]},global_field_id_uniqueness:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=new Map,r=[],i=(e,t)=>{if(!(typeof e!=`object`||!e))for(let i of Object.keys(e)){let e=n.get(i);e?r.push(W(`DUPLICATE_FIELD_ID`,`Field ID '${i}' is not unique; already used at ${e}`,`${t}.${i}`)):n.set(i,`${t}.${i}`)}};t.fields&&i(t.fields,`fields`);let a=t.schemas;if(a&&typeof a==`object`){for(let[e,t]of Object.entries(a))if(typeof t==`object`&&t){let n=t;n.fields&&i(n.fields,`schemas.${e}.fields`)}}return r},constraint_fields_exist:e=>{let t=e.root,n=[],r=(e,n)=>{let r=e.split(`.`),i=n;for(let e=0;e<r.length;e++){let n=r[e],a=i[n];if(a===void 0)return!1;if(e===r.length-1)return!0;let o=a;if(o.type===`object`){let e=o.schema;if(e&&e.id){let n=Rn(t,String(e.id));if(n&&n.fields){i=n.fields;continue}}}return!1}return!1},i=(e,t,i)=>{for(let[a,o]of Object.entries(e)){if(typeof o!=`object`||!o)continue;let e=o;if(e.predicate&&e.fields){let o=e.fields;Array.isArray(o)&&o.forEach((e,o)=>{typeof e==`string`&&(r(e,t)||n.push(W(`CONSTRAINT_FIELD_NOT_FOUND`,`Constraint '${a}' references non-existent field path '${e}'`,`${i}constraints.${a}.fields[${o}]`)))})}}},a=t.fields,o=t.constraints;o&&a&&i(o,a,``);let s=t.schemas;if(s){for(let[e,t]of Object.entries(s))if(typeof t==`object`&&t){let n=t,r=n.constraints,a=n.fields;r&&a&&i(r,a,`schemas.${e}.`)}}return n},inline_type_descriptor_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schema;if(!n||typeof n!=`object`||Array.isArray(n))return[];let r=t.type,i=n.type;if(r===void 0||i===void 0)return[];let a=new Set([`record`,`array`,`set`,`enum`]),o=String(r);if(!a.has(o))return[W(`INLINE_DESCRIPTOR_NOT_ALLOWED_FOR_FIELD_TYPE`,`Inline type descriptors are only allowed for fields of type record, array, set, or enum, but got '${o}'`,``)];let s=String(i);if(!new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]).has(s))return[W(`INLINE_DESCRIPTOR_INVALID_TYPE`,`Inline descriptor type '${s}' is not allowed (only primitives and 'record')`,``)];let c=n.values;if(c!=null){if(s!==`string`&&!$(s))return[W(`INLINE_DESCRIPTOR_VALUES_WITHOUT_ENUM`,`'values' can only be used with string or numeric inline types`,``)];if(!Array.isArray(c)||c.length===0)return[W(`INLINE_DESCRIPTOR_VALUES_NOT_ARRAY`,`'values' must be a non‑empty array`,``)]}return[]},schema_reference_form_correct:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r)return[];let i=t.schema;if(i==null)return[];let a=e=>({isNamed:`id`in e,isInline:`type`in e});if(Array.isArray(i)){if(i.length===0)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires at least one reference`,``)];if(n!==`union`&&n!==`composite`&&n!==`enum`)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' cannot use an array of references`,``)];for(let e=0;e<i.length;e++){let t=i[e];if(typeof t!=`object`||!t)return[W(`SCHEMA_REF_FORM_INVALID`,`Element ${e} of schema array is not an object`,``)];let{isNamed:r,isInline:o}=a(t);if(n===`enum`){if(!r&&!o)return[W(`SCHEMA_REF_FORM_INVALID`,`Enum array element ${e} must be a named reference or inline descriptor`,``)]}else if(!r)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' array element ${e} must be a named schema reference (with 'id')`,``)]}}else if(typeof i==`object`){let{isNamed:e,isInline:t}=a(i);switch(n){case`array`:case`set`:case`record`:case`enum`:if(!e&&!t)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires a single named reference or an inline descriptor`,``)];break;case`object`:if(!e)return[W(`SCHEMA_REF_FORM_INVALID`,`Object field requires a single named schema reference`,``)];break;case`union`:case`composite`:return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires an array of references`,``)]}}return[]},collection_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r)return[];let i=t.schema;return Ln(n)&&i==null?[W(`COLLECTION_MISSING_SCHEMA`,`Collection type '${n}' must have a schema reference`,``)]:[]},union_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`union`)return[];let i=t.schema;return i==null?[W(`UNION_MISSING_SCHEMA`,`Union type must have schema references`,``)]:Array.isArray(i)?i.length<2?[W(`UNION_INSUFFICIENT_SCHEMAS`,`Union type must have at least 2 schema references, got ${i.length}`,``)]:[]:[W(`UNION_SCHEMA_NOT_ARRAY`,`Union type schema must be an array of schema references`,``)]},record_schema_cardinality:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`record`)return[];let i=t.schema;return Array.isArray(i)?[W(`RECORD_SCHEMA_ARRAY`,`Record type must have zero or one schema reference, not an array`,``)]:[]},nested_schema_exclusive_mode:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!1;for(let e of Nn){let r=t[e];if(typeof r==`object`&&r&&Object.keys(r).length>0){n=!0;break}}let r=!1;for(let e of Pn)if(t[e]!=null){r=!0;break}return n&&r?[W(`NESTED_SCHEMA_MIXED_MODE`,`NestedSchema cannot mix BaseSchema fields (fields/indexes/constraints) with FieldProperties (type/values/schema)`,``)]:!n&&!r?[W(`NESTED_SCHEMA_NO_MODE`,`NestedSchema must have either BaseSchema fields or FieldProperties, not neither`,``)]:[]},constraint_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=String(t.name||`UNNAMED`),r=!!(t.predicate&&String(t.predicate)!==``),i=!!(t.operator&&Array.isArray(t.rules)&&t.rules.length>0);return r&&i?[W(`CONSTRAINT_MIXED_TYPE`,`Constraint cannot have both predicate (rule) and operator+rules (group)`,n)]:!r&&!i?[W(`CONSTRAINT_NO_TYPE`,`Constraint must have either predicate (rule) or operator+rules (group)`,n)]:[]},constraint_rule_requires_predicate:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.predicate;return n===void 0?[W(`REQUIRED_FIELD_MISSING`,`Required field 'predicate' is missing`,`predicate`)]:n===null?[W(`REQUIRED_FIELD_NULL`,`Required field 'predicate' cannot be null`,`predicate`)]:typeof n==`string`?n.trim()===``?[W(`REQUIRED_FIELD_EMPTY`,`Required field 'predicate' cannot be empty`,`predicate`)]:[]:[W(`REQUIRED_FIELD_WRONG_TYPE`,`Required field 'predicate' must be a string`,`predicate`)]},index_condition_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!!(t.field&&t.operator&&t.value!==void 0),r=!!(t.operator&&Array.isArray(t.conditions)&&t.conditions.length>0);return n&&r?[W(`INDEX_CONDITION_MIXED_TYPE`,`IndexCondition cannot have both single condition fields and group fields`,``)]:!n&&!r?[W(`INDEX_CONDITION_NO_TYPE`,`IndexCondition must be either a single condition or a group`,``)]:[]},schema_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[W(`SCHEMA_NAME_MISSING`,`Schema name is required`,`name`)]:[]},field_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[W(`FIELD_NAME_MISSING`,`Field name is required`,`name`)]:[]},index_fields_not_empty:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.fields;return!Array.isArray(n)||n.length===0?[W(`INDEX_FIELDS_EMPTY`,`Index must reference at least one field`,`fields`)]:[]},schema_reference_id_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.id;return n==null||String(n).trim()===``?[W(`SCHEMA_REFERENCE_ID_MISSING`,`SchemaReference ID is required`,`id`)]:[]},index_fields_reference_valid:e=>[]};var Kn=class{static validator=null;static async validate(e){return this.validator||=await kn.create(An,Gn),await this.validator.validate(e)}};exports.AnansiCodec=Ot,exports.Compiler=c,exports.DataTypes=u,exports.DocumentValidator=kn,exports.FD_NO_CHILD=63,exports.FLAG_COMPRESSED=4,exports.FLAG_ENCRYPTED=64,exports.FLAG_HASH_PRESENT=128,exports.Literal=n,exports.MAX_SCHEMA_SLOTS=63,exports.MULTI_STEP_BASE=f,exports.MigrationEngine=Ut,exports.MigrationError=L,exports.MigrationErrorCode=It,exports.SchemaValidator=Kn,exports.addressForSteps=oe,exports.buildEnum=l,exports.buildManifest=de,exports.bumpVersion=Mt,exports.calculateNextBump=jt,exports.classifyChangeImpact=At,exports.decodeAnansiBatch=Dt,exports.decodeAnansiPacket=Ct,exports.decodeBatch=st,exports.decodeDocument=rt,exports.deepMerge=Pt,exports.defaultValidationConfig=Wt,exports.encodeAnansiBatchColumnar=Et,exports.encodeAnansiBatchRows=Tt,exports.encodeAnansiPacket=St,exports.encodeBatchColumnar=at,exports.encodeBatchRows=it,exports.encodeDocument=nt,exports.internalDP=p,exports.link=ce,exports.makeDescriptor=m,exports.metaSchemaPredicateMap=Gn,exports.parseSchema=r,exports.schemaChangeToPatch=kt,exports.sha256=Ft,exports.unpackDescriptor=h,exports.userDataDP=se;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("hash-wasm"),t=require("fzstd");var n=class e{kind;value;constructor(e,t){this.kind=e,this.value=t}isZero(){return this.kind===`zero`}isNull(){return this.kind===`null`}static fromJSON(t){if(t===void 0)return new e(`zero`,void 0);if(t===null)return new e(`null`,void 0);if(typeof t==`string`)return new e(`string`,t);if(typeof t==`boolean`)return new e(`boolean`,t);if(typeof t==`number`)return Number.isInteger(t)?new e(`integer`,t):new e(`float`,t);if(Array.isArray(t))return new e(`array`,t);if(typeof t==`object`)return new e(`object`,t);throw Error(`anansi: unsupported literal ${typeof t}`)}};function r(e){let t=typeof e==`string`?JSON.parse(e):e;if(!t.name)throw Error(`anansi: schema requires a name`);return t}function i(e){return!!e&&!Array.isArray(e)&&typeof e.id==`string`}function a(e){return Array.isArray(e)}function o(e){return!!e&&!Array.isArray(e)&&typeof e.id!=`string`}function s(e,t){return e?`${e}.${t}`:t}var c=class{source;nested=new Map;building=new Set;constructor(e){this.source=e}compile(){for(let e of Object.keys(this.source.schemas??{}))this.compileNested(e);return{root:this.compileFields(this.source.fields??{},[],``),schemas:this.nested}}compileNested(e){let t=this.nested.get(e);if(t)return t;if(this.building.has(e))return null;let n=(this.source.schemas??{})[e];if(!n)throw Error(`anansi: schema '${e}' not found`);this.building.add(e);try{let t=n.values&&n.values.length>0?`enum`:n.type??`unknown`,r={id:e,name:n.name,effectiveType:t,fields:[],isRecursive:!1,values:n.values,enumDef:t===`enum`?l(n.values??[],n.type):void 0};return this.nested.set(e,r),n.fields&&(r.fields=this.compileFields(n.fields,[e],``)),r.isRecursive=r.fields.some(e=>e.kind.tag===`recursive`),r}finally{this.building.delete(e)}}compileFields(e,t,n){let r=[];for(let[i,a]of Object.entries(e))r.push(this.compileField(i,a,t,n));return r}compileField(e,t,r,c){let u=t.type??`unknown`,d={id:e,name:t.name,path:s(c,t.name),type:u,required:!!t.required,deprecated:!!t.deprecated,unique:!!t.unique,nullable:t.nullable!==!1,hasDefault:!n.fromJSON(t.default).isZero(),kind:{tag:`scalar`}},f=t.schema;switch(u){case`geometry`:break;case`record`:d.kind=i(f)?{tag:`container`,c:{itemSchema:this.resolve(f.id)??void 0,record:!0}}:{tag:`container`,c:{record:!0}};break;case`array`:if(a(f))throw Error(`anansi: ${d.path}: array cannot reference multiple schemas`);if(i(f))d.kind={tag:`container`,c:{itemSchema:this.require(f.id,d.path),record:!1}};else if(o(f)){let e=f.type??`unknown`,t={itemType:e,record:!1};(f.values?.length??0)>0&&(t.itemEnum=l(f.values,e)),d.kind={tag:`container`,c:t}}else d.kind={tag:`container`,c:{record:!1}};break;case`object`:d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:o(f)?{tag:`object`,schema:this.synthesizeInline(f,e,r)}:{tag:`scalar`};break;case`union`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`union`,variants:e}}else d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:{tag:`scalar`};break;case`composite`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`composite`,parts:e}}else d.kind={tag:`scalar`};break;case`enum`:if((t.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(t.values,u)};else if(o(f)&&(f.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(f.values,f.type??`string`)};else if(i(f)){let e=this.resolve(f.id);d.kind=e&&e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}else d.kind={tag:`scalar`};break;default:if(i(f)){let e=this.resolve(f.id);d.kind=e===null?{tag:`recursive`,schemaId:f.id}:e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}}return d}resolve(e){return this.nested.has(e)?this.nested.get(e):this.building.has(e)?null:this.compileNested(e)}require(e,t){let n=this.resolve(e);if(!n)throw Error(`anansi: ${t}: unresolvable schema reference '${e}'`);return n}synthesizeInline(e,t,n){let r=`inline:${[...n,t].join(`/`)}`,i=this.nested.get(r);if(i)return i;let a={id:r,name:r,effectiveType:e.type??`object`,fields:[],isRecursive:!1};this.nested.set(r,a);let o=e.fields??{};return a.fields=this.compileFields(o,[...n,r],``),a}};function l(e,t){let n=new Map,r=[];for(let t of e)t===null||[`string`,`number`,`boolean`].includes(typeof t)?n.set(JSON.stringify(t),t):r.push(t);return{lookup:n,complex:r,expectNumeric:t===`integer`||t===`number`}}const u={TypeUnknown:0,TypeInt:1,TypeFloat:2,TypeString:3,TypeBool:4,TypeBytes:5,TypeGeometry:6,TypeRecord:7,TypeArrayUnknown:8,TypeArrayInt:9,TypeArrayFloat:10,TypeArrayString:11,TypeArrayBool:12,TypeArrayBytes:13,TypeArrayObject:14,TypeArrayGeometry:15},d={[u.TypeUnknown]:`unknown`,[u.TypeInt]:`int`,[u.TypeFloat]:`float`,[u.TypeString]:`string`,[u.TypeBool]:`bool`,[u.TypeBytes]:`bytes`,[u.TypeGeometry]:`geometry`,[u.TypeRecord]:`record`,[u.TypeArrayUnknown]:`array_unknown`,[u.TypeArrayInt]:`array_int`,[u.TypeArrayFloat]:`array_float`,[u.TypeArrayString]:`array_string`,[u.TypeArrayBool]:`array_bool`,[u.TypeArrayBytes]:`array_bytes`,[u.TypeArrayObject]:`array_object`,[u.TypeArrayGeometry]:`array_geometry`},f=16384;function p(e){return(e.raw&4294967264|(e.raw>>>28&15)<<1)>>>0}function m(e,t,n,r,i){let a=0;return a|=(e&15)<<28,a|=(n&63)<<22,a|=(r&127)<<15,a|=(i.child&63)<<9,a|=(t&3)<<7,i.required&&(a|=64),i.hasDefault&&(a|=32),i.deprecated&&(a|=16),i.unique&&(a|=8),i.terminal&&(a|=4),i.nullable&&(a|=2),i.recursive&&(a|=1),h(a>>>0)}function h(e){return{raw:e,dt:e>>>28&15,schemaIdx:e>>>22&63,fieldIdx:e>>>15&127,childSchemaIdx:e>>>9&63,kind:e>>>7&3,required:!!(e&64),hasDefault:!!(e&32),deprecated:!!(e&16),unique:!!(e&8),terminal:!!(e&4),nullable:!!(e&2),recursive:!!(e&1)}}function ee(e){switch(e){case`string`:return u.TypeString;case`number`:return u.TypeFloat;case`decimal`:return u.TypeString;case`integer`:return u.TypeInt;case`boolean`:return u.TypeBool;case`bytes`:return u.TypeBytes;default:return u.TypeUnknown}}function g(e){return e?.expectNumeric?u.TypeInt:u.TypeString}function te(e,t){if(e)return u.TypeArrayObject;switch(t){case`string`:return u.TypeArrayString;case`number`:return u.TypeArrayFloat;case`decimal`:return u.TypeArrayString;case`integer`:return u.TypeArrayInt;case`boolean`:return u.TypeArrayBool;case`bytes`:return u.TypeArrayBytes;case`geometry`:return u.TypeArrayGeometry;case`unknown`:return u.TypeArrayUnknown;default:return u.TypeUnknown}}function ne(e){let t=e.kind;if(e.type===`geometry`)return[u.TypeGeometry,0,!0];if(t.tag===`scalar`)return[ee(e.type),0,!0];if(t.tag===`enum`)return[g(t.enumDef),0,!0];if(t.tag===`recursive`)return[u.TypeUnknown,1,!0];if(t.tag===`object`)return[u.TypeUnknown,1,!1];if(t.tag===`container`){let e=!t.c.itemSchema;return t.c.record?[u.TypeRecord,1,e]:[te(t.c.itemSchema,t.c.itemType),2,e]}return t.tag===`union`?[u.TypeUnknown,3,!1]:[u.TypeUnknown,1,!1]}var re=class{slots=[];descriptors=[];metas=[];localOffsets=[];fieldTypes=[];linkedSlots=[];assignSlot(e){if(this.slots.length>=63)throw Error(`anansi: exceeds maximum of 63 nested schemas while assigning '${e.name}'`);let t=this.slots.length;return this.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),this.linkedSlots.push({idx:t,slot:this.slots[t],fields:[]}),t}childSlotForField(e,t){let n=e.kind;return n.tag===`recursive`?{idx:t,kindTag:n}:n.tag===`object`?{idx:this.assignSlot(n.schema),kindTag:n}:n.tag===`composite`?{idx:this.assignSlot({id:`composite:${e.path}`,name:`composite:${e.name}`,effectiveType:`object`,fields:ie(n.parts),isRecursive:!1}),kindTag:n}:n.tag===`container`&&n.c.itemSchema?{idx:this.assignSlot(n.c.itemSchema),kindTag:n}:null}linkFields(e,t){let n=this.linkedSlots[t],r=[];for(let i=0;i<e.length;i++){if(e.length>128)throw Error(`anansi: slot ${t} exceeds 128 fields`);let a=e[i],[o,s,c]=ne(a),l=this.childSlotForField(a,t),u=l?l.idx:63,d=a.kind.tag===`recursive`,f=m(o,s,t,i,{required:a.required,hasDefault:a.hasDefault,deprecated:a.deprecated,unique:a.unique,terminal:c,nullable:a.nullable,recursive:d,child:u}),p=this.descriptors.length;this.descriptors.push(f),this.fieldTypes.push(a.type);let h={meta:{name:a.name,path:a.path},fd:f,dp:0,abs:p};n.fields.push(h),this.metas.push(h.meta),!c&&u!==63&&r.push({rf:a,fd:f,childIdx:u})}for(let e of r){let t=e.rf.kind;if(t.tag===`object`||t.tag===`composite`){let n=t.tag===`object`?t.schema.fields:ie(t.parts),r=this.linkFields(n,e.childIdx);this.setSlot(e.childIdx,r.count)}else if(t.tag===`container`&&t.c.itemSchema){let n=this.linkFields(t.c.itemSchema.fields,e.childIdx);this.setSlot(e.childIdx,n.count)}let r=n.fields.find(t=>t.fd===e.fd);r.child=this.linkedSlots[e.childIdx]}return{count:e.length,slot:n}}setSlot(e,t){let n=this.slots[e];n.fieldStart===-1&&(n.fieldStart=this.descriptors.length-t),n.fieldCount=t}};function ie(e){let t=[];for(let n of e)t.push(...n.fields);return t}function ae(e){for(let t=e.slots.length-1;t>=0;t--){let n=e.slots[t];n.fieldCount<0&&(n.fieldStart=0,n.fieldCount=0);let r=0;for(let t=0;t<n.fieldCount;t++){let i=n.fieldStart+t,a=e.descriptors[i];a.terminal?r+=1:a.childSchemaIdx!==63&&(r+=e.slots[a.childSchemaIdx].footprint)}n.footprint=r}for(let t of e.slots){let n=0;for(let r=0;r<t.fieldCount;r++){let i=t.fieldStart+r,a=e.descriptors[i];e.localOffsets[i]=n,a.terminal?n+=1:a.childSchemaIdx!==63&&(n+=e.slots[a.childSchemaIdx].footprint)}}}function oe(e,t,n,r){if(r.length===0)return 0;if(r.length===1){let[n,i]=r[0],a=e[n].fieldStart+i;return t[a].terminal?a+1>>>0:0}let i=f;for(let a=0;a<r.length;a++){let[o,s]=r[a],c=e[o].fieldStart+s,l=t[c];if(i+=n[c],a===r.length-1)return l.terminal?i>>>0:0;if(l.terminal)return 0}return i>>>0}function se(e,t){return(t<<5|e<<1)>>>0}function ce(e){let{root:t,schemas:n}=new c(e).compile(),r=new re;r.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),r.linkedSlots.push({idx:0,slot:r.slots[0],fields:[]});let i=r.linkFields(t,0);return r.slots[0].fieldStart=0,r.slots[0].fieldCount=i.count,ae(r),ue(r,r.linkedSlots[0],[]),{slots:r.slots,metas:r.metas,descriptors:r.descriptors,localOffsets:r.localOffsets,fieldTypes:r.fieldTypes,root:r.linkedSlots[0],childrenByPath:le(r.linkedSlots[0])}}function le(e){let t=new Map;for(let n of e.fields)n.child&&t.set(n.meta.path,n.child);return t}function ue(e,t,n){for(let r=0;r<t.slot.fieldCount;r++){let i=t.slot.fieldStart+r,a=e.descriptors[i],o=t.fields[r],s=[...n,[t.idx,r]],c=oe(e.slots,e.descriptors,e.localOffsets,s);o.dp=c===0?p(a):se(a.dt,c),!a.terminal&&o.child&&ue(e,o.child,s)}}function de(e){let t=(e,n,r)=>{let i=[];for(let a=0;a<e.slot.fieldCount;a++){let o=e.fields[a],s=o.fd,c=[...n,[e.idx,a]],l=r?`${r}.${o.meta.name}`:o.meta.name,f={name:o.meta.name,path:l,t:d[s.dt],dp:o.dp};if(!s.terminal&&o.child){s.dt===u.TypeArrayObject?(f.child=t(o.child,c,l),i.push(f)):i.push(...t(o.child,c,l));continue}i.push(f)}return i};return t(e.root,[],``)}function _(e,t){for(t>>>=0;t>=128;)e.push(t&127|128),t>>>=7;e.push(t)}function fe(e,t){if(!Number.isSafeInteger(t))throw Error(`anansi: integer ${t} exceeds Number.MAX_SAFE_INTEGER`);me(e,BigInt(t))}function pe(e,t){for(;t>=128n;)e.push(Number(t&127n)|128),t>>=7n;e.push(Number(t))}function me(e,t){pe(e,t<<1n^t>>63n)}var v=class{data;pos;constructor(e,t=0){this.data=e,this.pos=t}eof(){return this.pos>=this.data.length}remaining(){return this.data.length-this.pos}byte(){if(this.eof())throw Error(`anansi: unexpected end of buffer`);return this.data[this.pos++]}take(e){if(e<0||this.pos+e>this.data.length)throw Error(`anansi: unexpected end of buffer (need ${e}, have ${this.remaining()})`);let t=this.data.subarray(this.pos,this.pos+e);return this.pos+=e,t}uvarint(){let e=0,t=0;for(let n=0;n<10;n++){let r=this.byte();if(r<128){if(n===9&&r>1)throw Error(`anansi: varint overflow`);return(e|r<<t)>>>0}e|=(r&127)<<t,t+=7}throw Error(`anansi: varint overflow`)}uvarintBig(){let e=0n,t=0n,n=0;for(;n<10;){let r=this.byte();if(e|=BigInt(r&127)<<t,r<128)return e;t+=7n,n++}throw Error(`anansi: varint overflow`)}varint(){let e=this.uvarintBig(),t=e>>1n^-(e&1n),n=Number(t);if(!Number.isSafeInteger(n))throw Error(`anansi: decoded integer ${n} exceeds Number.MAX_SAFE_INTEGER`);return n}};const y={Unknown:0,Int:1,Float:2,String:3,Bool:4,Bytes:5,Geometry:6,Record:7,ArrayUnknown:8,ArrayInt:9,ArrayFloat:10,ArrayString:11,ArrayBool:12,ArrayBytes:13,ArrayObject:14,ArrayGeometry:15},he=new TextEncoder,ge=new TextDecoder;function _e(e){let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.subarray(r,r+n));return btoa(t)}function ve(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function ye(e,t){fe(e,t)}function be(e){return e.varint()}function b(e,t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t,!0);for(let t=0;t<8;t++)e.push(n.getUint8(t))}function x(e){let t=e.take(8);return new DataView(t.buffer,t.byteOffset,8).getFloat64(0,!0)}function S(e,t){let n=he.encode(t);_(e,n.length);for(let t of n)e.push(t)}function C(e){let t=e.uvarint();return ge.decode(e.take(t))}function xe(e,t){e.push(+!!t)}function Se(e){return e.byte()!==0}function w(e,t){let n=new Uint8Array(t.length+7>>3);t.forEach((e,t)=>{e&&(n[t>>3]|=1<<(t&7))});for(let t of n)e.push(t)}function Ce(e,t){let n=e.take(t+7>>3),r=[];for(let e=0;e<t;e++)r.push(!!(n[e>>3]&1<<(e&7)));return r}function we(e,t){let n=ve(t);_(e,n.length);for(let t of n)e.push(t)}function Te(e){let t=e.uvarint();return _e(e.take(t))}function Ee(e,t){_(e,t.length);for(let n of t){_(e,n.length/2);for(let t=0;t<n.length;t+=2)b(e,n[t]),b(e,n[t+1])}}function De(e){let t=e.uvarint(),n=[];for(let r=0;r<t;r++){let t=e.uvarint(),r=[];for(let n=0;n<t;n++)r.push(x(e),x(e));n.push(r)}return n}const T={Null:0,Int:y.Int,Float:y.Float,String:y.String,Bool:y.Bool,Bytes:y.Bytes,Geometry:y.Geometry,Record:y.Record,ArrUnknown:y.ArrayUnknown,ArrInt:y.ArrayInt,ArrFloat:y.ArrayFloat,ArrString:y.ArrayString,ArrBool:y.ArrayBool,ArrBytes:y.ArrayBytes};function Oe(e){return e.length>0&&Array.isArray(e[0])&&Array.isArray(e[0][0])}function E(e,t){if(t==null){e.push(T.Null);return}switch(typeof t){case`boolean`:e.push(T.Bool),xe(e,t);return;case`string`:e.push(T.String),S(e,t);return;case`number`:Number.isSafeInteger(t)?(e.push(T.Int),ye(e,t)):(e.push(T.Float),b(e,t));return;case`object`:if(Array.isArray(t)){if(Oe(t)){e.push(T.Geometry),Ee(e,t);return}if(t.every(e=>typeof e==`string`)){e.push(T.ArrString),_(e,t.length);for(let n of t)S(e,n);return}if(t.every(e=>Number.isSafeInteger(e))){e.push(T.ArrInt),_(e,t.length);for(let n of t)ye(e,n);return}if(t.every(e=>typeof e==`number`)){e.push(T.ArrFloat),_(e,t.length);for(let n of t)b(e,n);return}if(t.every(e=>typeof e==`boolean`)){e.push(T.ArrBool);let n=t;_(e,n.length),w(e,n);return}e.push(T.ArrUnknown),_(e,t.length);for(let n of t)E(e,n);return}e.push(T.Record),ke(e,t);return;default:throw Error(`anansi: unsupported any value ${typeof t}`)}}function D(e){let t=e.byte();switch(t){case T.Null:return null;case T.Bool:return Se(e);case T.String:return C(e);case T.Int:return be(e);case T.Float:return x(e);case T.Bytes:return Te(e);case T.Geometry:return De(e);case T.Record:return Ae(e);case T.ArrInt:{let t=e.uvarint();return Array.from({length:t},()=>be(e))}case T.ArrFloat:{let t=e.uvarint();return Array.from({length:t},()=>x(e))}case T.ArrString:{let t=e.uvarint();return Array.from({length:t},()=>C(e))}case T.ArrBool:return Ce(e,e.uvarint());case T.ArrUnknown:{let t=e.uvarint();return Array.from({length:t},()=>D(e))}default:throw Error(`anansi: unknown any tag ${t}`)}}function ke(e,t){let n=Object.keys(t).sort();_(e,n.length);for(let r of n)S(e,r),E(e,t[r])}function Ae(e){let t=e.uvarint(),n={};for(let r=0;r<t;r++){let t=C(e);n[t]=D(e)}return n}function je(e,t){_(e,t.length);for(let n of t)ye(e,n)}function Me(e){let t=e.uvarint();return Array.from({length:t},()=>be(e))}function Ne(e,t){_(e,t.length);for(let n of t)b(e,n)}function Pe(e){let t=e.uvarint();return Array.from({length:t},()=>x(e))}function Fe(e,t){_(e,t.length);for(let n of t)S(e,n)}function Ie(e){let t=e.uvarint();return Array.from({length:t},()=>C(e))}function Le(e,t){_(e,t.length),w(e,t)}function Re(e){return Ce(e,e.uvarint())}function ze(e,t){_(e,t.length);for(let n of t)we(e,n)}function Be(e){let t=e.uvarint();return Array.from({length:t},()=>Te(e))}function Ve(e,t){_(e,t.length);for(let n of t)Ee(e,n)}function He(e){let t=e.uvarint();return Array.from({length:t},()=>De(e))}function Ue(e,t){_(e,t.length);for(let n of t)E(e,n)}function We(e){let t=e.uvarint();return Array.from({length:t},()=>D(e))}const O={Dense:0,Sparse:1,Batch:2,Stream:3};function k(e,t){let n=t.split(`.`),r=e;for(let e=0;e<n.length;e++){if(typeof r!=`object`||!r)return[!1,void 0];let t=r;if(!(n[e]in t))return[!1,void 0];r=t[n[e]]}return r===void 0?[!1,void 0]:[!0,r]}function A(e,t){let[n,r]=k(e,t.path);return!n||r===void 0?`notSet`:r===null?`null`:`value`}function Ge(e,t){let n=t.length+1;return{...e,path:e.path.slice(n)}}function Ke(e,t){return e.map(e=>Ge(e,t))}function qe(e,t,n){switch(t.t){case`int`:ye(e,n);break;case`float`:b(e,n);break;case`string`:S(e,n);break;case`bool`:xe(e,n);break;case`bytes`:we(e,n);break;case`geometry`:Ee(e,n);break;case`record`:ke(e,n);break;case`unknown`:E(e,n);break;case`array_int`:je(e,n);break;case`array_float`:Ne(e,n);break;case`array_string`:Fe(e,n);break;case`array_bool`:Le(e,n);break;case`array_bytes`:ze(e,n);break;case`array_geometry`:Ve(e,n);break;case`array_unknown`:Ue(e,n);break;default:throw Error(`anansi: cannot write value for type ${t.t} field ${t.path}`)}}function Je(e,t){switch(t.t){case`int`:return e.varint();case`float`:return x(e);case`string`:return C(e);case`bool`:return Se(e);case`bytes`:return Te(e);case`geometry`:return De(e);case`record`:return Ae(e);case`unknown`:return D(e);case`array_int`:return Me(e);case`array_float`:return Pe(e);case`array_string`:return Ie(e);case`array_bool`:return Re(e);case`array_bytes`:return Be(e);case`array_geometry`:return He(e);case`array_unknown`:return We(e);default:throw Error(`anansi: cannot read value for type ${t.t} field ${t.path}`)}}function Ye(e,t,n,r,i){let a=Ke(t,n);_(e,r.length);for(let t of r){let n=0;for(let e of a)A(t,e)!==`notSet`&&n++;let r=a.length>0?n/a.length:1,o=!(a.length<=64||r>.25),s=o?O.Sparse:O.Dense,c=(i.flags&-200|s)>>>0,l=[],u={flags:i.flags&-4|s,version:i.version};o?N(l,a,t,u):j(l,a,t,u);let d=[c,i.version,...l];_(e,d.length);for(let t of d)e.push(t)}}function Xe(e,t,n,r,i){let a=e.uvarint();for(let o=0;o<a;o++){let a=e.uvarint(),o=new v(e.take(a)),s=o.byte();o.byte();let c=i?i():{},l=s&3;if(l===O.Dense)M(o,t,c,n);else if(l===O.Sparse)P(o,t,c,n);else throw Error(`anansi: unsupported nested packet type ${l}`);r.push(c)}}const Ze={flags:O.Dense,version:0};function j(e,t,n,r=Ze){let i=2*t.length,a=new Uint8Array(i+7>>3);t.forEach((e,t)=>{let r=A(n,e)===`notSet`?0:A(n,e)===`null`?1:2;a[t>>2]|=r<<((t&3)<<1)});for(let t of a)e.push(t);let o=$e(t);for(let t=0;t<=15;t++){let i=o[t];if(!(!i||i.length===0)){if(t===y.Bool){let t=[];for(let e of i)A(n,e)===`value`&&t.push(k(n,e.path)[1]);w(e,t);continue}for(let t of i)if(A(n,t)===`value`){if(t.t===`array_object`){Ye(e,t.child,t.name,k(n,t.path)[1]??[],r);continue}qe(e,t,k(n,t.path)[1])}}}}function M(e,t,n,r){let i=[],a=2*t.length,o=e.take(a+7>>3);for(let e=0;e<t.length;e++)i.push(o[e>>2]>>((e&3)<<1)&3);let s=$e(t);for(let a=0;a<=15;a++){let o=s[a];if(o){if(a===y.Bool){let a=o.filter((e,n)=>i[t.indexOf(o[n])]===2).length,s=Ce(e,a),c=0;o.forEach(e=>{let a=t.indexOf(e);i[a]===2&&F(n,e.path,r,s[c++])});continue}for(let a of o){let o=i[t.indexOf(a)];if(o!==0){if(o===1){F(n,a.path,r,null);continue}if(a.t===`array_object`){let t=[];Xe(e,a.child,a.name,t),F(n,a.path,r,t);continue}F(n,a.path,r,Je(e,a))}}}}}function N(e,t,n,r={flags:O.Sparse,version:0}){let i=t.filter(e=>A(n,e)!==`notSet`);_(e,i.length);for(let t of i){let i=A(n,t),a=t.dp;i===`null`?a|=1:a&=4294967294,_(e,a),i===`value`&&(t.t===`array_object`?Ye(e,t.child,t.name,k(n,t.path)[1]??[],r):qe(e,t,k(n,t.path)[1]))}}function P(e,t,n,r){let i=new Map;for(let e of t)i.set(e.dp&4294967294,e);let a=e.uvarint();for(let t=0;t<a;t++){let t=e.uvarint(),a=!!(t&1),o=i.get(t&4294967294);if(!o)throw Error(`anansi: sparse packet references unknown data point ${t}`);if(a){F(n,o.path,r,null);continue}if(o.t===`array_object`){let t=[];Xe(e,o.child,o.name,t),F(n,o.path,r,t);continue}F(n,o.path,r,Je(e,o))}}function F(e,t,n,r){let i=t;if(n&&i.startsWith(n+`.`)){i=i.slice(n.length+1),Qe(e,i.split(`.`),r);return}Qe(e,i.split(`.`),r)}function Qe(e,t,n){let r=e;for(let e=0;e<t.length-1;e++){let n=t[e];(typeof r[n]!=`object`||r[n]===null)&&(r[n]={}),r=r[n]}r[t[t.length-1]]=n}function $e(e){let t=[];for(let n of e){let e=et[n.t]??y.Unknown;(t[e]??=[]).push(n)}return t}const et=Object.fromEntries(Object.entries({unknown:y.Unknown,int:y.Int,float:y.Float,string:y.String,bool:y.Bool,bytes:y.Bytes,geometry:y.Geometry,record:y.Record,array_unknown:y.ArrayUnknown,array_int:y.ArrayInt,array_float:y.ArrayFloat,array_string:y.ArrayString,array_bool:y.ArrayBool,array_bytes:y.ArrayBytes,array_object:y.ArrayObject,array_geometry:y.ArrayGeometry}));function I(e){if(e<0||e>1023)throw Error(`anansi: schema version ${e} exceeds maximum of 1023`);return{flags:e>>8<<4,version:e&255}}function tt(e,t){return(e>>4&3)<<8|t}function nt(e,t,n=0,r=`auto`){let i=I(n),a=0;for(let n of e)A(t,n)!==`notSet`&&a++;let o=e.length?a/e.length:1,s=r===`sparse`||r===`auto`&&!(e.length<=64||o>.25),c=i.flags|(s?O.Sparse:O.Dense),l=[c,i.version],u={flags:c,version:i.version},d=[];return s?N(d,e,t,u):j(d,e,t,u),new Uint8Array([...l,...d])}function rt(e,t){let n=new v(e),r=n.byte(),i=n.byte();if((r>>4&3)===O.Stream)throw Error(`anansi: stream packets unsupported`);if(r&196)throw Error(`anansi: compressed/encrypted/hashed packets require transform support not enabled in this build`);let a=r&3,o={};if(a===O.Dense)M(n,t,o);else if(a===O.Sparse)P(n,t,o);else throw Error(`anansi: use batch API for packet type ${a}`);return{version:tt(r,i),doc:o}}function it(e,t,n=0){let r=I(n),i=0,a=0;for(let n of t){for(let t of e)A(n,t)!==`notSet`&&i++;a+=e.length}let o=a?i/a:1,s=e.length>64&&o<=.25,c=[r.flags|O.Batch,r.version],l=[];_(l,t.length),l.push(s?2:0);let u={flags:r.flags|O.Batch,version:r.version};for(let n of t)s?N(l,e,n,u):j(l,e,n,u);return new Uint8Array([...c,...l])}function at(e,t,n=0){let r=I(n),i=[r.flags|O.Batch|8,r.version],a=ot(e,t,{flags:i[0],version:r.version});return new Uint8Array([...i,...a])}function ot(e,t,n){let r=[];_(r,t.length),r.push(1);let i=$e(e);for(let e=0;e<=15;e++){let a=i[e];if(!a||a.length===0)continue;let o=2*a.length*t.length,s=new Uint8Array(o+7>>3),c=0;for(let e of a)for(let n of t){let t=A(n,e),r=t===`notSet`?0:t===`null`?1:2;s[c>>3]|=r<<(c&7),c+=2}for(let e of s)r.push(e);if(e===y.Bool){for(let e of a){let n=[];for(let r of t)A(r,e)===`value`&&n.push(k(r,e.path)[1]);w(r,n)}continue}for(let e of a)for(let i of t)if(A(i,e)===`value`){if(e.t===`array_object`){Ye(r,e.child,e.name,k(i,e.path)[1]??[],n);continue}qe(r,e,k(i,e.path)[1])}}return r}function st(e,t){let n=new v(e),r=n.byte(),i=n.byte();if((r&3)!==O.Batch)throw Error(`anansi: not a batch packet`);if(r&196)throw Error(`anansi: transformed batch packets unsupported in this build`);let a=n.uvarint(),o=n.byte(),s=Array.from({length:a},()=>({}));if(o&1||r&8)ct(n,t,s);else{let e=!!(o&2);for(let r of s)e?P(n,t,r):M(n,t,r)}return{version:tt(r,i),docs:s}}function ct(e,t,n){let r=$e(t);for(let t=0;t<=15;t++){let i=r[t];if(!i||i.length===0)continue;let a=2*i.length*n.length,o=e.take(a+7>>3),s=[],c=0;for(let e=0;e<i.length;e++){let e=[];for(let t=0;t<n.length;t++)e.push(o[c>>3]>>(c&7)&3),c+=2;s.push(e)}if(t===y.Bool){i.forEach((t,r)=>{let i=s[r].filter(e=>e===2).length,a=Ce(e,i),o=0;s[r].forEach((e,r)=>{if(e!==0){if(e===1){F(n[r],t.path,void 0,null);return}F(n[r],t.path,void 0,a[o++])}})});continue}i.forEach((t,r)=>{s[r].forEach((r,i)=>{if(r!==0){if(r===1){F(n[i],t.path,void 0,null);return}if(t.t===`array_object`){let r=[];Xe(e,t.child,t.name,r),F(n[i],t.path,void 0,r);return}F(n[i],t.path,void 0,Je(e,t))}})})}}const lt=e=>e&3;let ut,dt=!1;function ft(){if(!dt){dt=!0;try{ut=globalThis.process?.getBuiltinModule?.(`node:zlib`)??void 0}catch{ut=void 0}}return ut}function pt(e){let t=ft()?.zstdCompressSync?.(e);if(!t)throw Error(`anansi: zstd compression is unavailable in this runtime (no node:zlib). Send plain packets, or provide a WASM compressor.`);return t}function mt(e,n){return ft()?.zstdDecompressSync?.(e)||(0,t.decompress)(e)}async function ht(t){let n=await(0,e.blake3)(t,128),r=new Uint8Array(16);for(let e=0;e<16;e++)r[e]=parseInt(n.slice(e*2,e*2+2),16);return r}async function gt(e){if(e.length!==32)throw Error(`anansi: AES-256-GCM requires a 32-byte key, got ${e.length}`);return crypto.subtle.importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`,`decrypt`])}async function _t(e,t,n,r){let i=e;r.integrity&&(i|=128),r.compression&&(i|=4),r.encryptionKey&&(i|=64);let a=[i,t];if(r.integrity)for(let e of await ht(n))a.push(e);let o=[];if(r.compression){_(o,n.length);for(let e of pt(n))o.push(e)}else for(let e of n)o.push(e);if(!r.encryptionKey)return new Uint8Array([...a,...o]);let s=await gt(r.encryptionKey),c=crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await crypto.subtle.encrypt({name:`AES-GCM`,iv:c},s,new Uint8Array(o))),u=new Uint8Array(a.length+12+l.length);return u.set(Uint8Array.from(a),0),u.set(c,a.length),u.set(l,a.length+12),u}async function vt(e,t,n){let r;t&128&&(r=e.take(16));let i=e.data.subarray(e.pos);if(t&64){if(!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);if(i.length<12)throw Error(`anansi: truncated nonce`);let e=i.subarray(0,12);i=i.subarray(12);let t=await gt(n.decryptionKey),r=await crypto.subtle.decrypt({name:`AES-GCM`,iv:e},t,i);i=new Uint8Array(r)}let a=i.length,o=new v(i);if(t&4){if(a=o.uvarint(),a>536870912)throw Error(`anansi: declared uncompressed size ${a} exceeds limit`);let e=mt(i.subarray(o.pos),a);if(e.length!==a)throw Error(`anansi: decompressed ${e.length} bytes, header declared ${a}`);i=e}if(r){let e=await ht(i);for(let t=0;t<16;t++)if(r[t]!==e[t])throw Error(`anansi: integrity check failed (packet digest mismatch)`)}return new v(i)}function yt(e,t){let n=0;for(let r of e){let[e,i]=bt(t,r.path);e&&i!=null&&n++}return e.length<=64||e.length===0||n/e.length>.25?`dense`:`sparse`}function bt(e,t){let n=e;for(let e of t.split(`.`)){if(typeof n!=`object`||!n)return[!1,void 0];let t=n;if(!(e in t))return[!1,void 0];n=t[e]}return[n!==void 0,n]}function xt(e,t,n){let r=n===`sparse`||n===`auto`&&yt(e,t)===`sparse`,i=[];return r?N(i,e,t):j(i,e,t),new Uint8Array(i)}async function St(e,t,n=0,r={}){let i=I(n),a=r.kind??`auto`,o=a===`sparse`||a===`auto`&&yt(e,t)===`sparse`,s=i.flags|(o?O.Sparse:O.Dense),c=xt(e,t,o?`sparse`:`dense`);return _t(s,i.version,c,r)}async function Ct(e,t,n={}){let r=new v(e),i=r.byte(),a=r.byte();if(i&64&&!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);let o=await vt(r,i,n),s={},c=lt(i);if(c===O.Dense)M(o,t,s);else if(c===O.Sparse)P(o,t,s);else throw Error(`anansi: use batch API for packet type ${c}`);return{version:tt(i,a),doc:s}}function wt(e,t){let n=0,r=0;for(let i of t)for(let t of e){let[e,a]=bt(i,t.path);e&&a!=null&&n++,r++}let i=r>0?n/r:1,a=e.length>64&&i<=.25,o=[];_(o,t.length),o.push(a?2:0);for(let n of t)a?N(o,e,n):j(o,e,n);return{body:new Uint8Array(o),sparse:a}}async function Tt(e,t,n=0,r={}){let i=I(n),{body:a,sparse:o}=wt(e,t);return _t(i.flags|O.Batch|(o?2:0),i.version,a,r)}async function Et(e,t,n=0,r={}){let i=I(n),a=i.flags|O.Batch|8,o=new Uint8Array(ot(e,t,{flags:a,version:i.version}));return _t(a,i.version,o,r)}async function Dt(e,t,n={}){let r=new v(e),i=r.byte(),a=r.byte();if(lt(i)!==O.Batch)throw Error(`anansi: not a batch packet`);let o=await vt(r,i,n),s=o.uvarint(),c=o.byte(),l=Array.from({length:s},()=>({}));if(c&1||i&8)ct(o,t,l);else for(let e of l)c&2?P(o,t,e):M(o,t,e);return{version:tt(i,a),docs:l}}var Ot=class e{linked;fields;fullVersion;kind;enc;dec;constructor(e,t,n,r,i,a){this.linked=e,this.fields=t,this.fullVersion=n,this.kind=r,this.enc=i,this.dec=a}static async create(t,n={}){let i=ce(r(t));return new e(i,de(i),n.fullVersion??0,n.kind??`auto`,{compression:n.compression,integrity:n.integrity,encryptionKey:n.encryptionKey},{decryptionKey:n.decryptionKey})}async encode(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?St(this.fields,e,this.fullVersion,{...this.enc,kind:this.kind}):nt(this.fields,e,this.fullVersion,this.kind)}async decode(e){return Ct(e,this.fields,this.dec)}async encodeBatch(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Tt(this.fields,e,this.fullVersion,this.enc):it(this.fields,e,this.fullVersion)}async decodeBatch(e){return Dt(e,this.fields,this.dec)}async encodeColumnar(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Et(this.fields,e,this.fullVersion,this.enc):Et(this.fields,e,this.fullVersion,{})}};function kt(e,t){let n=[];switch(e.type){case`modifyProperty`:n.push({op:`replace`,path:`/${e.id}`,value:e.value});break;case`addField`:n.push({op:`add`,path:`/fields/${e.id}`,value:e.definition});break;case`removeField`:n.push({op:`remove`,path:`/fields/${e.id}`});break;case`modifyField`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/fields/${e.id}/${t}`,value:r});break;case`addIndex`:t.indexes||n.push({op:`add`,path:`/indexes`,value:{}}),n.push({op:`add`,path:`/indexes/${e.id}`,value:e.definition});break;case`removeIndex`:n.push({op:`remove`,path:`/indexes/${e.id}`});break;case`modifyIndex`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/indexes/${e.id}/${t}`,value:r});break;case`addConstraint`:t.constraints||n.push({op:`add`,path:`/constraints`,value:{}}),n.push({op:`add`,path:`/constraints/${e.id}`,value:e.constraint});break;case`removeConstraint`:n.push({op:`remove`,path:`/constraints/${e.id}`});break;case`modifyConstraint`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/constraints/${e.id}/${t}`,value:r});break;case`addSchema`:t.schemas||n.push({op:`add`,path:`/schemas`,value:{}}),n.push({op:`add`,path:`/schemas/${e.id}`,value:e.definition});break;case`removeSchema`:n.push({op:`remove`,path:`/schemas/${e.id}`});break;case`modifySchema`:for(let r of e.changes)n.push(...kt(r,t))}return n}function At(e,t){switch(e.type){case`removeField`:case`removeIndex`:case`removeConstraint`:return`major`;case`modifyField`:{let t=e.changes;return t.required===!0||t.type!==void 0||t.schema!==void 0||t.unique===!0?`major`:t.deprecated?`minor`:`patch`}case`modifyIndex`:{let t=e.changes;return t.unique!==void 0||t.fields!==void 0?`major`:`minor`}case`modifyConstraint`:{let n=t?.constraints?.[e.id];return n&&`predicate`in e.changes&&e.changes.predicate!==void 0||n&&`parameters`in e.changes&&e.changes.parameters!==void 0?`major`:`minor`}case`addField`:case`addIndex`:case`addConstraint`:case`addSchema`:case`deprecateField`:return`minor`;case`modifySchema`:{let n=`patch`;for(let r of e.changes){let e=At(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}default:return`patch`}}function jt(e,t){let n=`patch`;for(let r of e){let e=At(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}function Mt(e,t){let n=e.replace(/^v/,``).match(/^(\d+)\.(\d+)\.(\d+)/);if(!n)throw Error(`anansi: invalid version format "${e}"`);let[,r,i,a]=n.map(Number);switch(t){case`major`:r++,i=0,a=0;break;case`minor`:i++,a=0;break;case`patch`:a++}return`${r}.${i}.${a}`}function Nt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function Pt(e,t){let n={...e};for(let e of Object.keys(t)){let r=t[e];Nt(r)&&Nt(n[e])?n[e]=Pt(n[e],r):r!==void 0&&(n[e]=r)}return n}async function Ft(e){if(typeof globalThis.crypto?.subtle?.digest==`function`){let t=new TextEncoder().encode(e),n=await globalThis.crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}let{createHash:t}=await import(`node:crypto`);return t(`sha256`).update(e).digest(`hex`)}let It=function(e){return e.INVALID_SCHEMA=`INVALID_SCHEMA`,e.INVALID_MIGRATION=`INVALID_MIGRATION`,e.CHECKSUM_MISMATCH=`CHECKSUM_MISMATCH`,e.TIMEOUT=`TIMEOUT`,e.MEMORY_LIMIT=`MEMORY_LIMIT`,e.CONCURRENT_OPERATION=`CONCURRENT_OPERATION`,e.TRANSFORM_ERROR=`TRANSFORM_ERROR`,e.VERSION_NOT_FOUND=`VERSION_NOT_FOUND`,e.CIRCULAR_DEPENDENCY=`CIRCULAR_DEPENDENCY`,e.STREAM_ERROR=`STREAM_ERROR`,e.ROLLBACK_ERROR=`ROLLBACK_ERROR`,e.MISSING_TRANSFORM=`MISSING_TRANSFORM`,e}({});var L=class extends Error{code;migrationId;cause;constructor(e,t,n,r){super(e),this.code=t,this.migrationId=n,this.cause=r,this.name=`MigrationError`}};function Lt(e){return e===``||e===`/`?[]:e.slice(1).split(`/`).map(e=>e.replace(/~1/g,`/`).replace(/~0/g,`~`))}function R(e,t){let n=e;for(let e=0;e<t.length-1;e++){let r=t[e];if(typeof n!=`object`||!n)throw Error(`anansi/engine: path "${r}" not found`);n=Array.isArray(n)?n[parseInt(r,10)]:n[r]}return{parent:n,key:t[t.length-1]}}function Rt(e,t){let n=JSON.parse(JSON.stringify(e));for(let e of t){let t=Lt(e.path);switch(e.op){case`add`:{if(t.length===0){n=e.value;break}let{parent:r,key:i}=R(n,t);if(Array.isArray(r)){let t=i===`-`?r.length:parseInt(i,10);r.splice(t,0,e.value)}else r[i]=e.value;break}case`remove`:{let{parent:e,key:r}=R(n,t);Array.isArray(e)?e.splice(parseInt(r,10),1):delete e[r];break}case`replace`:{let{parent:r,key:i}=R(n,t);r[i]=e.value;break}case`move`:{let t=Lt(e.from),r=R(n,t),i=Array.isArray(r.parent)?r.parent.splice(parseInt(r.key,10),1)[0]:(()=>{let e=r.parent[r.key];return delete r.parent[r.key],e})(),a=Lt(e.path),o=R(n,a);Array.isArray(o.parent)?o.parent.splice(parseInt(o.key,10),0,i):o.parent[o.key]=i;break}case`test`:{let{parent:r,key:i}=R(n,t);if(JSON.stringify(r[i])!==JSON.stringify(e.value))throw Error(`anansi/engine: test failed at ${e.path}`);break}}}return n}async function zt(e){return Ft(JSON.stringify({id:e.id,schemaVersion:e.schemaVersion,changes:e.changes,description:e.description,rollback:e.rollback,createdAt:e.createdAt}))}async function Bt(e,t){return e.transform?typeof e.transform==`string`?e.transform.startsWith(`http://`)||e.transform.startsWith(`https://`)?Vt(e.transform,t):Ht(e.transform,t):e.transform[t]:null}async function Vt(e,t){try{let n=await fetch(e);if(!n.ok)throw new L(`Failed to fetch transform module: ${e}`,`TRANSFORM_ERROR`);let r=await n.text();if(typeof window<`u`){let e=new Blob([r],{type:`application/javascript`});return(await import(URL.createObjectURL(e))).default[t]}{let{runInNewContext:n}=await import(`node:vm`),i={module:{exports:{}},console};return n(r,i,e),i.module.exports[t]}}catch(t){throw t instanceof L?t:new L(`Failed to load remote transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}async function Ht(e,t){try{return(await import(e)).default[t]}catch(t){throw t instanceof L?t:new L(`Failed to import local transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}var Ut=class e{currentSchema;history=[];migrations=[];isProcessing=!1;constructor(e,t,n){this.currentSchema=e,t&&(this.migrations=t.sort((e,t)=>e.schemaVersion.localeCompare(t.schemaVersion))),n&&(this.history=n.sort((e,t)=>e.version.localeCompare(t.version)))}data(){return{schema:this.currentSchema,history:[...this.history],migrations:[...this.migrations]}}async add(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);if(!e.changes?.length)throw new L(`Migration must include changes`,`INVALID_MIGRATION`);let t={id:Date.now().toString(),schemaVersion:this.currentSchema.version,changes:e.changes,description:e.description,status:`pending`,rollback:e.rollback,transform:e.transform,createdAt:new Date().toISOString(),checksum:``};t.checksum=await zt(t),this.migrations.push(t)}async dryRun(t,n,r){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{this.isProcessing=!0;let i={...this.currentSchema},a=this.getRelevantMigrations(n,r),o;if(n===`forward`)o=a.reduce((e,t)=>this.applySchemaChanges(e,t.changes,t.id),i);else{let e=[...this.history];for(let t=0;t<a.length;t++){let t=e.pop();t&&(i={...t})}o=i}let s=await e.processMigrationList(t,n,a);return{newSchema:o,dataPreview:s}}catch(e){throw e instanceof L?e:new L(`Dry run failed`,`INVALID_SCHEMA`,void 0,e)}finally{this.isProcessing=!1}}async prepareMigration(){let e=this.migrations.filter(e=>e.status===`pending`);return await this.validateMigrations(e),e}async migrate(t){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);let n=await this.prepareMigration();try{this.isProcessing=!0,this.transformSchema(`forward`);let r=await e.processMigrationList(t,`forward`,n);return this.markMigrationsApplied(n),r}finally{this.isProcessing=!1}}async rollback(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);return this.migrations.filter(e=>e.status===`applied`).slice(-1)[0]?this.rollbackToVersion(this.history[this.history.length-1]?.version||this.currentSchema.version,e):e}async rollbackToVersion(t,n){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{let r=this.history.findIndex(e=>e.version===t);if(r===-1)throw new L(`Version ${t} not found in history`,`VERSION_NOT_FOUND`);let i=this.migrations.filter(e=>e.schemaVersion===t&&e.status===`applied`).sort((e,t)=>t.id.localeCompare(e.id)),a=this.history.length-r;if(a<0)return n;for(let e=0;e<a;e++)this.transformSchema(`backward`);let o=await e.processMigrationList(n,`backward`,i);return this.migrations=this.migrations.map(e=>e.schemaVersion>=t&&e.status===`applied`?{...e,status:`rolled_back`}:e),o}finally{this.isProcessing=!1}}static async processMigrationList(e,t,n){return(await Promise.all(n.map(async e=>{try{return{migration:e,transform:await Bt(e,t)}}catch(t){throw new L(`Failed to resolve transform for migration ${e.id}`,`TRANSFORM_ERROR`,e.id,t)}}))).filter(e=>!!e.transform).reduce((e,{migration:t,transform:n})=>e.pipeThrough(new TransformStream({async transform(e,r){try{let t=await n(e);r.enqueue(t)}catch(e){r.error(new L(`Data transformation failed for migration ${t.id}`,`TRANSFORM_ERROR`,t.id,e))}}})),e)}getRelevantMigrations(e,t){return[...this.migrations].filter(n=>{let r=e===`forward`?`pending`:`applied`,i=!t||n.schemaVersion.localeCompare(t)>=0;return n.status===r&&i}).sort((t,n)=>e===`forward`?t.id.localeCompare(n.id):n.id.localeCompare(t.id))}applySchemaChanges(e,t,n){let r=jt(t,e),i=Mt(e.version,r);return t.map(t=>{try{return kt(t,e)}catch(e){throw new L(`Invalid schema change`,`INVALID_SCHEMA`,n,e)}}).reduce((e,t)=>{try{return Rt(e,t)}catch(e){throw new L(`Failed to apply patch`,`INVALID_SCHEMA`,n,e)}},{...e,version:i})}async validateMigrations(e){await Promise.all(e.map(async e=>{let t=await zt(e);if(e.checksum!==t)throw new L(`Checksum mismatch`,`CHECKSUM_MISMATCH`,e.id)}))}markMigrationsApplied(e){this.migrations=this.migrations.map(t=>e.some(e=>e.id===t.id)?{...t,status:`applied`}:t)}transformSchema(e){try{if(e===`backward`){let e=this.history.pop();if(!e)throw Error(`No previous version`);this.currentSchema=e;return}let t=this.migrations.filter(e=>e.status===`pending`).flatMap(e=>e.changes);if(!t.length)return;this.history.push(structuredClone(this.currentSchema)),this.currentSchema=t.reduce((e,t)=>this.applySchemaChanges(e,[t]),this.currentSchema)}catch(e){throw e instanceof L?e:new L(`Schema transformation failed`,`INVALID_SCHEMA`,void 0,e)}}};function Wt(){return{maxDepth:20,mode:`strict`}}var z=class e{raw;constructor(e){this.raw=e}static zero(){return new e({kind:`zero`})}static fromSingle(t){return new e({kind:`single`,value:t})}static fromMultiple(t){return new e({kind:`multiple`,value:t})}static fromInline(t){return new e({kind:`inline`,value:t})}static fromFieldSchema(t){if(t==null)return e.zero();if(Array.isArray(t))return e.fromMultiple(t);let n=t;return`id`in n&&typeof n.id==`string`?e.fromSingle(t):`type`in n?e.fromInline(t):e.zero()}isZero(){return this.raw.kind===`zero`}isSingle(){return this.raw.kind===`single`}isMultiple(){return this.raw.kind===`multiple`}isInline(){return this.raw.kind===`inline`}asSingle(){if(this.raw.kind!==`single`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'single'`);return this.raw.value}asMultiple(){if(this.raw.kind!==`multiple`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'multiple'`);return this.raw.value}asInline(){if(this.raw.kind!==`inline`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'inline'`);return this.raw.value}};function Gt(e){let t=e;if(`predicate`in t&&typeof t.predicate==`string`){let t=e;return{kind:`rule`,name:t.name,description:t.description??void 0,predicate:t.predicate,parameters:t.parameters??void 0,fields:t.fields??[]}}let n=e;return{kind:`group`,name:n.name,description:n.description??void 0,operator:n.operator,rules:(n.rules??[]).map(e=>Gt(e))}}function B(e){if(!e)return{};let t={};for(let[n,r]of Object.entries(e))t[n]=Gt(r);return t}const V={issues:[],success:!0,skipped:!1},H={issues:[],success:!0,skipped:!0};function U(e){return{issues:e,success:!1,skipped:!1}}function W(e,t,n){return{code:e,message:t,path:n}}function G(e,t){return e===``?t:`${e}.${t}`}function Kt(e,t,n){return e===``?[n,[n]]:[`${e}.${n}`,[...t,n]]}function qt(e){return e===``?[]:e.split(`.`)}function K(e){let t=e.length;for(let n of e)for(let e of n)e===`[`&&t++;return t}function Jt(e,t,n){let r=[],i=[];for(let a of n){let n=qt(a);e===``?(r.push(a),i.push(n)):(r.push(G(e,a)),i.push([...t,...n]))}return[r,i]}function Yt(e,t){if(t.length===0)return[e,!0];let n=e;for(let e of t){if(n==null)return[void 0,!1];let t=e.indexOf(`[`);if(t===-1){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let t=n;if(!(e in t))return[void 0,!1];n=t[e]}else{let r=e.slice(0,t);if(r!==``){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let e=n;if(!(r in e))return[void 0,!1];n=e[r]}let i=e.slice(t).matchAll(/\[(\d+)\]/g);for(let e of i){let t=e[1];if(t===void 0)return[void 0,!1];let r=parseInt(t,10);if(!Array.isArray(n)||r>=n.length)return[void 0,!1];n=n[r]}}}return[n,!0]}function q(e,t){return Yt(e.rootData,t)}function J(e){return typeof e==`object`&&e&&!Array.isArray(e)?e:null}function Xt(e){if(e==null)return!0;let t=typeof e;return t===`string`||t===`number`||t===`boolean`||t===`bigint`||t===`symbol`}function Zt(e,t,n=!1){if(e===t)return!0;if(e===null||t===null||e===void 0||t===void 0)return e===t;let r=typeof e,i=typeof t;if(r===`string`&&i===`string`||r===`boolean`&&i===`boolean`||r===`number`&&i===`number`||n&&r===`number`&&i===`number`)return e===t;if(r!==`object`||i!==`object`)return!1;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!Zt(e[r],t[r],n))return!1;return!0}if(Array.isArray(e)!==Array.isArray(t))return!1;let a=e,o=t,s=Object.keys(a),c=Object.keys(o);if(s.length!==c.length)return!1;for(let e of s)if(!(e in o)||!Zt(a[e],o[e],n))return!1;return!0}function Qt(e){return typeof e==`number`&&Number.isInteger(e)}function $t(e){return typeof e==`number`&&!Number.isInteger(e)}function Y(e){return typeof e==`number`}var X=class{id;path;pathParts;deps;constructor(e,t,n,r=[]){this.id=e,this.path=t,this.pathParts=n,this.deps=r}getID(){return this.id}getPath(){return this.path}getPathParts(){return this.pathParts}getDependencies(){return this.deps}},en=class extends X{expectedFields;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expectedFields=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n)return V;let r=J(t);if(!r)return U([W(`TYPE_MISMATCH`,`Expected object for unexpected field check`,this.path)]);let i=[];for(let e of Object.keys(r))this.expectedFields.has(e)||i.push(W(`UNEXPECTED_FIELD`,`Unexpected field '${e}'`,G(this.path,e)));return i.length>0?U(i):V}},tn=class extends X{fieldName;parentPath;parentPathParts;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.fieldName=r,this.parentPath=i,this.parentPathParts=a}async execute(e){let[t,n]=q(e,this.parentPathParts);if(!n&&this.parentPath!==``)return V;if(t==null)return H;let r=J(t);return r?!(this.fieldName in r)||r[this.fieldName]===void 0?U([W(`REQUIRED_FIELD_MISSING`,`Required field '${this.fieldName}' is missing`,this.path)]):V:U([W(`INVALID_DATA_STRUCTURE`,`Cannot check for required fields on non-object parent`,this.parentPath)])}},nn=class extends X{expected;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expected=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;switch(this.expected){case`string`:if(typeof t==`string`)return V;break;case`boolean`:if(typeof t==`boolean`)return V;break;case`number`:if(Y(t))return V;break;case`integer`:if(Qt(t))return V;break;case`decimal`:if($t(t))return V;break;case`bytes`:if(t instanceof Uint8Array||Array.isArray(t)&&t.every(e=>typeof e==`number`))return V;break;case`record`:if(J(t)!==null)return V;break;case`unknown`:return V;default:return V}return U([W(`TYPE_MISMATCH`,`Expected ${this.expected}, got ${typeof t} (${JSON.stringify(t)})`,this.path)])}},rn=class extends X{lookup;complex;expectNumeric;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.lookup=r,this.complex=i,this.expectNumeric=a}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(typeof t!=`string`&&typeof t!=`number`&&typeof t!=`boolean`)return U([W(`TYPE_MISMATCH`,`Expected scalar for enum, got ${typeof t}`,this.path)]);if(this.lookup.has(t))return V;for(let e of this.complex)if(Zt(t,e,this.expectNumeric))return V;return U([W(`ENUM_VIOLATION`,`Value ${JSON.stringify(t)} is not in the allowed list`,this.path)])}},an=class extends X{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return U([W(`ARRAY_TYPE_MISMATCH`,`Expected array`,this.path)]);let r=K(this.pathParts);if(r>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let i=[],a=e.maxDepth-r;for(let n=0;n<t.length;n++){let r=t[n],o=`${this.path}[${n}]`,s=await this.graph.traverse(e.functionMap,{item:r},e.mode,a,e.originalRoot);for(let e of s)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,o):e.path})}return i.length>0?U(i):V}},on=class extends X{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;let r=J(t);if(!r)return U([W(`OBJECT_TYPE_MISMATCH`,`Expected object for record, got ${typeof t}`,this.path)]);let i=K(this.pathParts);if(i>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let a=[],o=e.maxDepth-i;for(let[t,n]of Object.entries(r)){let r=G(this.path,t),i=await this.graph.traverse(e.functionMap,{item:n},e.mode,o,e.originalRoot);for(let e of i)a.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,r):e.path})}return a.length>0?U(a):V}},sn=class extends X{itemGraph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.itemGraph=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return U([W(`SET_TYPE_MISMATCH`,`Expected array for set validation`,this.path)]);if(this.itemGraph){let n=K(this.pathParts);if(n>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=e.maxDepth-n,i=[];for(let n=0;n<t.length;n++){let a=`${this.path}[${n}]`,o=await this.itemGraph.traverse(e.functionMap,{item:t[n]},e.mode,r,e.originalRoot);for(let e of o)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,a):e.path})}if(i.length>0)return U(i)}if(t.length<=1)return V;let r=new Map,i=[];for(let e=0;e<t.length;e++){let n=t[e];if(Xt(n)){let t=r.get(n);if(t!==void 0)return U([W(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t})`,this.path)]);r.set(n,e)}else{let t=i.find(e=>Zt(n,e.val,!1));if(t)return U([W(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t.index})`,this.path)]);i.push({val:n,index:e})}}return V}},cn=class extends X{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){let[t,n]=q(e,this.pathParts);if(!n)return V;if(!Array.isArray(t))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry must be an array of coordinate arrays`,this.path)]);for(let e=0;e<t.length;e++){let n=t[e];if(!Array.isArray(n))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry inner element at index ${e} must be an array`,`${this.path}[${e}]`)]);for(let t=0;t<n.length;t++)if(!Y(n[t]))return U([W(`GEOMETRY_TYPE_MISMATCH`,`Geometry element at [${e}][${t}] is not a valid number`,`${this.path}[${e}][${t}]`)])}return V}},ln=class extends X{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){return V}},un=class extends X{validationGraph;schemaName;constructor(e,t,n,r,i,a=[]){super(e,t,n,a),this.validationGraph=r,this.schemaName=i}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;let r=J(t);if(!r)return U([W(`TYPE_MISMATCH`,`Expected object for recursive schema ${this.schemaName}, got ${typeof t}`,this.path)]);if(K(this.pathParts)>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Recursive schema '${this.schemaName}' exceeds maximum depth of ${e.maxDepth}`,this.path)]);let i=(await this.validationGraph.traverse(e.functionMap,r,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path===``?this.path:G(this.path,e.path)}));return i.length>0?U(i):V}},dn=class extends X{graphs;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graphs=r}async execute(e){let[t,n]=q(e,this.pathParts);if(!n||t===void 0)return V;if(K(this.pathParts)>=e.maxDepth)return U([W(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=await Promise.all(this.graphs.map(async(n,r)=>{let i={root:t},a=(await n.traverse(e.functionMap,i,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path.startsWith(`root`)?e.path.replace(`root`,this.path):e.path,message:`[variant ${r}] ${e.message}`})),o=a.length===0;return!o&&a.length===0&&a.push(W(`INTERNAL_ERROR`,`Variant ${r} failed without reporting any issues`,this.path)),{ok:o,issues:a}}));return r.some(e=>e.ok)?V:U([W(`UNION_MISMATCH`,`Value at '${this.path}' did not match any variant of the union`,this.path),...r.flatMap(e=>e.issues)])}},fn=class extends X{constraint;fieldPaths;fieldPathParts;constraintPath;constraintPathParts;scope;constructor(e,t,n,r,i,a,o,s,c,l=[]){super(e,t,n,l),this.constraint=r,this.fieldPaths=i,this.fieldPathParts=a,this.constraintPath=o,this.constraintPathParts=s,this.scope=c}async execute(e){if(this.constraint.kind!==`rule`)return U([W(`INTERNAL_ERROR`,`ConstraintNode '${this.constraint.name}': expected a rule, got a group`,this.constraintPath)]);switch(this.scope){case`global`:return this.executeGlobalConstraint(e,this.constraint);case`recursive`:return this.executeRecursiveConstraint(e,this.constraint)}}executeGlobalConstraint(e,t){return gn(e,t.name,this.constraintPath,this.fieldPaths,this.fieldPathParts,()=>_n({...e,data:e.rootData},t,this.constraintPath))}executeRecursiveConstraint(e,t){let[n,r]=q(e,this.constraintPathParts);if(!r)return Promise.resolve(H);let i=this.fieldPaths.map(e=>this.constraintPath!==``&&e.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPath.length+1):e),a=this.fieldPathParts.map((e,t)=>{let n=this.fieldPaths[t]??``;return this.constraintPath!==``&&n.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPathParts.length):e});return gn(e,t.name,this.constraintPath,i,a,()=>_n({...e,data:n},t,this.constraintPath))}},pn=class extends X{group;name;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.group=r,this.name=i}async execute(e){let{allPaths:t,allParts:n}=hn(this.group,this.path,this.pathParts);return gn(e,this.name,this.path,t,n,()=>this.executeGroup(e,this.group,this.path,this.name))}async executeGroup(e,t,n,r){let i=[],a=[];for(let o of t.rules){let t;if(o.kind===`rule`)t=await _n(e,o,n);else if(o.kind===`group`)t=await this.executeGroup(e,o,n,o.name);else return U([W(`INTERNAL_ERROR`,`Unknown constraint kind in group '${r}'`,n)]);i.push(t.success),t.success||a.push(...t.issues)}return mn(t.operator,i)?V:U([W(`CONSTRAINT_GROUP_VIOLATION`,`Constraint group '${r}' failed`,n),...a])}};function mn(e,t){switch(e){case`and`:return t.every(Boolean);case`or`:return t.some(Boolean);case`not`:return t.length===1&&!t[0];case`nor`:return t.every(e=>!e);case`xor`:return t.filter(Boolean).length===1;case`nand`:return!t.every(Boolean);case`xnor`:return t.filter(Boolean).length!==1;default:return!1}}function hn(e,t,n){let r=new Map;function i(e){for(let a of e.rules)if(a.kind===`rule`){let[e,i]=Jt(t,n,a.fields);for(let t=0;t<e.length;t++){let n=e[t],a=i[t];r.set(n,a)}}else a.kind===`group`&&i(a)}i(e);let a=Array.from(r.keys());return{allPaths:a,allParts:a.map(e=>r.get(e))}}async function gn(e,t,n,r,i,a){let o=[],s=[];for(let t=0;t<r.length;t++){let n=i[t]??[],a=r[t]??``,[,c]=q(e,n);c?o.push(a):s.push(a)}if(s.length===0)return a();if(o.length===0)switch(e.mode){case`strict`:return U([W(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:case`loose`:return H}switch(e.mode){case`strict`:return U([W(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:return U([W(`CONSTRAINT_PARTIAL_UPDATE`,`Constraint '${t}' couples fields ${JSON.stringify(r)}. Cannot update only ${JSON.stringify(o)} - all coupled fields must be updated together`,n)]);case`loose`:return H}}async function _n(e,t,n){let r=e.functionMap[t.predicate];if(!r)return U([W(`MISSING_PREDICATE`,`Predicate '${t.predicate}' not found`,n)]);let i=await r({root:e.originalRoot,data:e.data,keys:t.fields,parameters:t.parameters});return i.length>0?U(i.map(e=>({...e,path:e.path===``?n:G(n,e.path)}))):V}var vn=class{constraints=new Map;add(e,t,n,r,i){let a=this.constraints.get(e);(!a||n>=a.specificity)&&this.constraints.set(e,{constraint:t,specificity:n,basePath:r,scope:i})}getEffective(){return Array.from(this.constraints.values())}};function yn(){return new vn}var bn=class{buildingSchemas=new Map;recursiveGraphCache=new Map;isRecursive(e){return(this.buildingSchemas.get(e)??0)>0}markBuilding(e){this.buildingSchemas.set(e,(this.buildingSchemas.get(e)??0)+1)}unmarkBuilding(e){let t=(this.buildingSchemas.get(e)??0)-1;t<=0?this.buildingSchemas.delete(e):this.buildingSchemas.set(e,t)}makeGraphCacheKey(e,t){let n=Object.keys(t).sort();return n.length===0?e:`${e}:${n.join(`,`)}`}async getOrBuildRecursiveGraph(e,t,n,r){let i=this.makeGraphCacheKey(e,n),a=this.recursiveGraphCache.get(i);if(a)return a;let o=new On;this.recursiveGraphCache.set(i,o),this.markBuilding(e);try{let e=xn(t,r);await o.buildFromSchema(e,``,[],new Map,t,n,r,this,!1,!1),o.finalize()}catch(e){throw this.recursiveGraphCache.delete(i),e}finally{this.unmarkBuilding(e)}return o}};function xn(e,t){let n=`fields`in e?e.fields:{},r=e.constraints??{},i={name:e.name,version:`0`,fields:n,constraints:r};return e.description!==void 0&&(i.description=e.description),t.schemas!==void 0&&(i.schemas=t.schemas),i}function Z(e){return`type`in e&&e.type?e.type:`fields`in e&&e.fields&&Object.keys(e.fields).length>0?`object`:`unknown`}function Sn(e){switch(e){case`object`:case`array`:case`set`:case`record`:case`union`:case`composite`:case`enum`:case`geometry`:return!0;default:return!1}}function Cn(e){let t=[];for(let n of e.rules)n.kind===`rule`?t.push(...n.fields):n.kind===`group`&&t.push(...Cn(n));return t}function wn(e,t){let n={},r=e.constraints??{},i=e=>e===t||e.startsWith(t+`.`);for(let[e,t]of Object.entries(r)){let r=Gt(t);r.kind===`rule`?r.fields.some(i)&&(n[e]=r):r.kind===`group`&&Cn(r).some(i)&&(n[e]=r)}return n}function Tn(e,t,n,r){let i=yn(),a=r===``?`global`:`recursive`;for(let[,t]of Object.entries(e))i.add(t.name,t,1,r,a);for(let[,e]of Object.entries(t))i.add(e.name,e,2,r,a);for(let[,e]of Object.entries(n))i.add(e.name,e,3,r,a);return i.getEffective()}function En(e){let t=new Map,n=[];for(let r of e)r!=null&&(typeof r==`string`||typeof r==`number`||typeof r==`boolean`?t.set(r,!0):n.push(r));return{lookup:t,complex:n}}function Dn(e,t){let n=new Set;function r(e){let i=Z(e);if(i===`object`||i===`unknown`){if(`fields`in e&&e.fields)for(let t of Object.values(e.fields))n.add(t.name)}else if(i===`union`||i===`composite`){if(!(`schema`in e)||!e.schema)return;let n=z.fromFieldSchema(e.schema);if(!n.isMultiple())return;for(let e of n.asMultiple()){let n=t.schemas?.[e.id];n&&r(n)}}}for(let n of e){let e=t.schemas?.[n.id];e&&r(e)}return n}var On=class e{nodes=new Map;dependencies=new Map;visitedState=new Map;executionOrder=[];nextNodeID=1;nullablePaths=new Set;buildNodeID(){return this.nextNodeID++}addNode(e){let t=e.getID();this.nodes.has(t)||(this.nodes.set(t,e),this.dependencies.set(t,e.getDependencies()))}createUnexpectedFieldsNode(e,t,n,r=[]){return new en(this.buildNodeID(),e,t,n,r)}createRequiredFieldNode(e,t,n,r,i,a=[]){return new tn(this.buildNodeID(),e,t,n,r,i,a)}createTypeCheckNode(e,t,n,r=[]){return new nn(this.buildNodeID(),e,t,n,r)}createCompletionNode(e,t,n){return new ln(this.buildNodeID(),e,t,n)}finalize(){this.visitedState=new Map;let e=[],t=!1,n=r=>{if(t)return;let i=this.visitedState.get(r)??0;if(i===1){t=!0;return}if(i!==2){this.visitedState.set(r,1);for(let e of this.dependencies.get(r)??[])n(e);this.visitedState.set(r,2),e.push(r)}};for(let e of this.nodes.keys())if((this.visitedState.get(e)??0)===0&&(n(e),t))throw Error(`Circular dependency detected in validation graph at node ${e}`);this.executionOrder=e}async traverse(e,t,n,r,i){let a={originalRoot:i,rootData:t,data:t,functionMap:e,maxDepth:r,mode:n,visited:new Map,issues:[]};for(let e of this.executionOrder){let t=this.nodes.get(e),r=this.dependencies.get(e)??[],i=!1;for(let t of r){let n=a.visited.get(t);if(n!==void 0&&!n){a.visited.set(e,!0),i=!0;break}}if(i)continue;let o=t.getPathParts(),s=t.getPath(),[c,l]=q(a,o),u=t instanceof tn,d=this.nullablePaths.has(s);if((!l||c===void 0)&&!u){a.visited.set(e,!0);continue}if(c===null){if(u&&d)continue;if(d){a.visited.set(e,!0);continue}}let f=await t.execute(a);if(a.visited.set(e,f.success),f&&!f.skipped&&f.issues.length>0)for(let e of f.issues)switch(n){case`strict`:a.issues.push(e);break;case`partialStrict`:e.code!==`REQUIRED_FIELD_MISSING`&&a.issues.push(e);break;case`loose`:e.code!==`REQUIRED_FIELD_MISSING`&&e.code!==`UNEXPECTED_FIELD`&&a.issues.push(e)}}return a.issues}async buildFromSchema(e,t,n,r,i,a,o,s,c,l){let u=[],d=i&&`fields`in i&&i.fields?i.fields:e.fields,f=null;if(!c){let e=new Set(Object.values(d).map(e=>e.name));f=this.createUnexpectedFieldsNode(t,n,e),this.addNode(f),u.push({id:f.getID(),deps:[]})}let p=[],m=[];for(let i of Object.values(d)){let a=await this.buildFieldNodes(i,t,n,p,e,r,o,s,l);m.push(...a)}let h=Tn(B(i?i.constraints:e.constraints),a,o&&t!==``?wn(o,t):{},t),ee=m.map(e=>e.id),g=this.buildFromEffectiveConstraints(h,n,ee,r);if(g.length>0){let e=this.createCompletionNode(t,n,g);this.addNode(e),u.push({id:e.getID(),deps:g})}return u}async buildFieldNodes(e,t,n,r,i,a,o,s,c){let[l,u]=Kt(t,n,e.name);e.nullable!==!1&&this.nullablePaths.add(l);let d=r,f=[];if(e.required){let r=this.createRequiredFieldNode(l,u,e.name,t,n,d);this.addNode(r),d=[r.getID()],f.push({id:r.getID(),deps:d})}if(!Sn(e.type)){let t=this.createTypeCheckNode(l,u,e.type,d);this.addNode(t),d=[t.getID()],f.push({id:t.getID(),deps:d})}let p=await this.buildFieldTypeNodes(e,l,u,d,i,a,o,s,c);return f.push(...p),f}async buildFieldTypeNodes(e,t,n,r,i,a,o,s,c){let l=[],u=null;switch(e.type){case`enum`:u=await this.buildEnumNode(e,t,n,r,o);break;case`array`:u=await this.buildContainerNode(e,t,n,r,o,s,`array`,`array items`);break;case`set`:{let i=await this.buildContainerNode(e,t,n,r,o,s,`array`,`set elements`),a=i instanceof an?i.graph:null;u=new sn(this.buildNodeID(),t,n,a,r);break}case`record`:u=await this.buildContainerNode(e,t,n,r,o,s,`record`,`record items`);break;case`union`:u=await this.buildUnionNode(e,t,n,r,o,s);break;case`composite`:{let i=await this.buildCompositeNode(e,t,n,r,o,s);return l.push(...i),l}case`geometry`:u=new cn(this.buildNodeID(),t,n,r);break;case`object`:{let r=await this.buildObjectFieldNodes(e,t,n,i,a,o,s,c);return l.push(...r),l}default:return l}return u&&(this.addNode(u),l.push({id:u.getID(),deps:u.getDependencies()})),l}async buildObjectFieldNodes(e,t,n,r,i,a,o,s){let c=[],l=z.fromFieldSchema(e.schema);if(l.isZero())throw Error(`FieldSchemaReference is zero/uninitialized at path '${t}'`);if(!l.isSingle())throw Error(`Object field must use a single schema reference at path '${t}'`);let u=l.asSingle(),d=a.schemas?.[u.id];if(!d)throw Error(`Nested schema '${u.id}' not found for field '${e.name}' at path '${t}'`);if(o.isRecursive(u.id)){let e=B(u.constraints),r=await o.getOrBuildRecursiveGraph(u.id,d,e,a),i=new un(this.buildNodeID(),t,n,r,u.id);return this.addNode(i),c.push({id:i.getID(),deps:[]}),c}let f=xn(d,a);o.markBuilding(u.id);try{let e=await this.buildFromSchema(f,t,n,i,d,B(u.constraints),a,o,s,!1),r=new ln(this.buildNodeID(),t,n,e.map(e=>e.id));this.addNode(r),c.push({id:r.getID(),deps:e.map(e=>e.id)})}finally{o.unmarkBuilding(u.id)}return c}async buildContainerNode(t,n,r,i,a,o,s,c){let l=e=>s===`array`?new an(this.buildNodeID(),n,r,e,i):new on(this.buildNodeID(),n,r,e,i),u=z.fromFieldSchema(t.schema);if(u.isZero())return l(null);if(u.isSingle()){let e=u.asSingle();if(e.id!==``){let t=a.schemas?.[e.id];if(!t)throw Error(`Nested schema '${e.id}' not found for ${c} at path '${n}'`);let r=Z(t),i=r===`composite`||r===`union`,s;s=r===`object`?z.fromSingle({id:e.id}):`schema`in t&&t.schema?z.fromFieldSchema(t.schema):z.zero();let u=s.isZero()?void 0:s.isSingle()?s.asSingle():s.isMultiple()?s.asMultiple():s.asInline(),d={name:`item`,type:r};u!==void 0&&(d.schema=u);let f=`default`in t?t.default:void 0;return f!==void 0&&(d.default=f),l(await this.createSubGraph(`item`,d,n,a,o,i,!1))}}if(u.isInline()){let t=u.asInline();if(!t.type)throw Error(`Inline descriptor missing 'type' field at path '${n}'`);let r=new e,i=new Map,s=t.type;if(s===`object`){let e={name:`__inline_record`,version:`0`,fields:{}};a.schemas!==void 0&&(e.schemas=a.schemas),await r.buildFromSchema(e,``,[],i,null,{},a,o,!0,!1)}else{let e=r.createTypeCheckNode(`item`,[`item`],s);r.addNode(e)}return r.finalize(),l(r)}throw Error(`Invalid schema reference for ${c} at path '${n}'`)}async buildEnumNode(e,t,n,r,i){let a=z.fromFieldSchema(e.schema);if(a.isZero())throw Error(`Enum field must have schema reference at path '${t}'`);let o=a.isSingle()?[a.asSingle()]:a.isMultiple()?a.asMultiple():[];if(o.length===0&&!a.isInline())throw Error(`Enum schema reference must be single, multiple, or inline at path '${t}'`);if(a.isInline()){let e=a.asInline();if(!e.type)throw Error(`Inline enum descriptor missing 'type' at path '${t}'`);if(!e.values||e.values.length===0)throw Error(`Inline enum descriptor missing 'values' at path '${t}'`);let{lookup:i,complex:o}=En(e.values),s=e.type===`number`||e.type===`integer`||e.type===`decimal`;return new rn(this.buildNodeID(),t,n,i,o,s,r)}let s=new Map,c=[],l=null;for(let e of o){let n,r,a;if(e.id!==``){let o=i.schemas?.[e.id];if(!o)throw Error(`Enum schema '${e.id}' not found at path '${t}'`);if(!(`values`in o)||!o.values?.length)throw Error(`Enum schema '${e.id}' has no values defined at path '${t}'`);a=o.type,{lookup:n,complex:r}=En(o.values)}else throw Error(`Inline descriptor in named-reference array is not permitted at path '${t}'`);for(let[e]of n)s.set(e,!0);c=c.concat(r),l||=a}let u=l===`number`||l===`integer`||l===`decimal`;return new rn(this.buildNodeID(),t,n,s,c,u,r)}async buildUnionNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Union field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Nested schema '${e.id}' not found for union at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:Z(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!1);c.push(o)}return new dn(this.buildNodeID(),t,n,c,r)}async buildCompositeNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Composite field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=Dn(s,i),l=this.createUnexpectedFieldsNode(t,n,c);this.addNode(l);let u=[l.getID()],d=[];for(let e of s){let r=i.schemas?.[e.id];if(!r)throw Error(`Nested schema '${e.id}' not found for composite at path '${t}'`);let o=Z(r);if(o===`object`||o===`unknown`){let o=xn(r,i);a.markBuilding(e.id);let s;try{s=await this.buildFromSchema(o,t,n,new Map,r,B(e.constraints),i,a,!0,!0)}finally{a.unmarkBuilding(e.id)}for(let e of s)e.deps.includes(l.getID())||(e.deps=[l.getID(),...e.deps],this.dependencies.set(e.id,e.deps));d.push(...s)}else if(o===`union`){if(!(`schema`in r)||!r.schema)throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let o=z.fromFieldSchema(r.schema);if(!o.isMultiple())throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Union variant schema '${e.id}' not found at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:Z(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!0);c.push(o)}let l=new dn(this.buildNodeID(),t,n,c,u);this.addNode(l),d.push({id:l.getID(),deps:u})}else throw Error(`Composite part '${e.id}' has unsupported effective type '${o}'; composite parts must be object or union schemas at path '${t}'`)}return[{id:l.getID(),deps:[]},...d]}buildFromEffectiveConstraints(e,t,n,r){let i=[];for(let a of e){let e=this.buildFromConstraintRuleWithScope(a.constraint,a.basePath,t,n,r,a.scope);i.push(...e)}return i}buildFromConstraintRuleWithScope(e,t,n,r,i,a){let o=`${t}:${e.name}`;if(i.get(o))return[];if(i.set(o,!0),e.kind===`rule`){let[i,o]=Jt(t,n,e.fields),s=new fn(this.buildNodeID(),t,n,e,i,o,t,n,a,r);return this.addNode(s),[s.getID()]}if(e.kind===`group`){let i=new pn(this.buildNodeID(),t,n,e,e.name,a,r);return this.addNode(i),[i.getID()]}return[]}async createSubGraph(t,n,r,i,a,o,s){let c={name:`subgraph_${r}`,version:`0`,fields:{[t]:n}};i.schemas!==void 0&&(c.schemas=i.schemas);let l=new e,u=new Map;return await l.buildFromSchema(c,``,[],u,null,{},i,a,o,s),l.finalize(),l}},kn=class e{fmap;graph;config;constructor(e,t,n){this.graph=e,this.fmap=t,this.config=n}async validate(e){return this.graph.traverse(this.fmap,e,`strict`,this.config.maxDepth,e)}async validatePartial(e){return this.graph.traverse(this.fmap,e,`partialStrict`,this.config.maxDepth,e)}async validateLoose(e){return this.graph.traverse(this.fmap,e,`loose`,this.config.maxDepth,e)}static async create(t,n,r=Wt()){let i=new On,a=new bn,o=new Map;return await i.buildFromSchema(t,``,[],o,null,{},t,a,!1,!1),i.finalize(),new e(i,n,r)}},An={version:`1.0.0`,name:`Schema`,description:`Meta-schema defining the structure of schema definitions`,fields:{"019d7775-6565-7755-904d-2d0cdf0e1b86":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6565-73d6-a7be-130b3c1e93c8":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6565-722f-9a5a-2810f537a61c":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6565-7fe8-86f6-57a8a305a314":{name:`schemas`,type:`record`,schema:{id:`019d7775-6565-7db3-9bb0-be9fc50550bd`}},"019d7775-6565-7c1f-a6f7-aeb5c4eb35e4":{name:`name`,required:!0,type:`string`},"019d7775-6565-7545-9e25-fc471f57bcfb":{name:`description`,type:`string`},"019d7775-6565-7bdc-a9d4-776b1f163346":{name:`version`,required:!0,type:`string`},"019d7775-6565-74ed-8a52-12027f3c577d":{name:`metadata`,type:`record`}},schemas:{"019d7775-6566-7da1-b24a-67bd81dd2b0e":{name:`Unknown`,type:`unknown`},"019d7775-6565-7db3-9bb0-be9fc50550bd":{name:`NestedSchema`,fields:{"019d7775-6566-77a1-9b36-bbd57f927d11":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6567-7931-b2d6-fb52039c28b5":{name:`type`,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6567-7742-90ae-a96178ae505d":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"019d7775-6567-7709-a7e3-ed75f833da15":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-748e-b099-50c76a5b79c2":{name:`name`,required:!0,type:`string`},"019d7775-6566-74df-a8df-038cc26af034":{name:`description`,type:`string`},"019d7775-6567-748e-bcad-76068cc4739f":{name:`metadata`,type:`record`},"019d7775-6567-7022-b7fb-3975ccae2b72":{name:`concrete`,type:`boolean`},"019d7775-6567-7f25-b7e2-acc72435964f":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6567-750b-a618-5bcdc4e5d04a":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6567-75c4-b399-eb4764617462":{name:`default`,type:`unknown`}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`}}},"019d7775-6565-71ef-a08f-cdcdb116f947":{name:`IndexCondition`,fields:{"019d7775-6567-75ef-8e82-347da30f06fd":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7a70-8b56-e55704397fc2`}},"019d7775-6567-7466-a873-e7866e9d031f":{name:`field`,required:!0,type:`string`},"019d7775-6567-7526-9704-d19fb4eb8d8d":{name:`value`,required:!0,type:`unknown`},"a2f03bc4-e620-4b7f-a2ad-8271471ac1e4":{name:`description`,type:`string`},"8782f46d-5f1f-4102-9c3c-917752d8c9d1":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-713e-8355-df812c6bb8c9":{name:`index_condition_exclusive_type`,predicate:`index_condition_type_exclusive`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`}}},"019d7775-6565-7d6a-a531-47e7ea838bc4":{name:`IndexConditionGroup`,fields:{"019d7775-6567-7b4e-893f-ee7afb0c4be1":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6567-7af2-817f-bc5b8732c198":{name:`conditions`,required:!0,type:`array`,schema:{id:`019d7775-6565-74a3-a6e0-881a0148fc59`}},"670ebd05-3bce-4da7-a39b-20cf9ba67abe":{name:`description`,type:`string`},"8e60fb23-0e3f-4ae5-bfff-fd20da1da756":{name:`metadata`,type:`record`}}},"019d7775-6565-794d-8aba-bdc5e3cbe97e":{name:`ConstraintUnion`,type:`union`,schema:[{id:`019d7775-6565-703f-95cf-e7511d33c91b`},{id:`019d7775-6565-7ea4-a820-e8bcdbc690bc`}]},"019d7775-6566-7786-8769-c13eb5a883d9":{name:`InlineTypeDescriptor`,fields:{"019d7775-6568-713f-867f-93dde123ccfd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e05-ab1b-6f768f459927`}},"019d7775-6568-7ae5-95b7-56c97722c732":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"347a2348-014b-41a7-80ad-a4e8d364ef5d":{name:`description`,type:`string`},"7d0f2347-3dfe-4e37-8235-1f9122e045dc":{name:`metadata`,type:`record`}}},"019d7775-6566-7e3b-860a-c4aa4adf5d6d":{name:`LogicalOperatorEnum`,type:`string`,values:[`and`,`or`,`not`,`nor`,`xor`,`nand`,`xnor`]},"019d7775-6566-7f2f-a009-0af144502aac":{name:`IndexOrderEnum`,type:`string`,values:[`asc`,`desc`]},"019d7775-6565-7f15-b0f4-27a4f40cbf5c":{name:`Field`,fields:{"019d7775-6566-727e-b4ff-4e6e8b800808":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6566-79e9-a60b-1c15e2154580":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-7a4c-877d-262c18adb028":{name:`name`,required:!0,type:`string`},"019d7775-6566-7b93-aa4c-b1e3869512d8":{name:`description`,type:`string`},"019d7775-6566-7ed5-9fb3-33a67b90dbbe":{name:`default`,type:`unknown`},"019d7775-6566-722b-8132-d5d8c4c9b2b6":{name:`required`,type:`boolean`},"a1000000-0000-0000-0000-000000000001":{name:`nullable`,type:`boolean`},"019d7775-6566-7367-9664-adb25994bf41":{name:`deprecated`,type:`boolean`},"019d7775-6566-7fc8-82c6-735838cdfe21":{name:`unique`,type:`boolean`},"95ab0d3b-352c-4aff-a43a-81ab889aaa42":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-7605-8bfb-c27365b73581":{name:`enum_fields_valid`,predicate:`enum_fields_valid`},"019d7775-6563-74fe-a5ae-4eef62a128d9":{name:`arrays_require_schema`,predicate:`collection_requires_schema`},"019d7775-6563-7080-ae67-bad5cd864f07":{name:`objects_require_schema`,predicate:`object_requires_schema`},"019d7775-6563-7e83-92c4-0258a8fc0d3a":{name:`unions_require_multiple_schemas`,predicate:`union_requires_multiple_schemas`},"019d7775-6564-7c60-9f9f-aeeabe05c1b5":{name:`default_value_type_match`,predicate:`default_matches_type`},"019d7775-6564-77ca-94bc-30a07da76c3d":{name:`records_allow_optional_schema`,predicate:`record_schema_cardinality`},"019d7775-6564-7768-8995-72c282cfe69f":{name:`inline_type_descriptor_valid`,predicate:`inline_type_descriptor_valid`},"019d7775-6563-7c55-a6f3-ac8f087d89d1":{name:`primitives_no_schema`,predicate:`primitives_prohibit_schema`},"019d7775-6563-73ce-aef1-86943b54a1ca":{name:`composites_require_multiple_schemas`,predicate:`composite_requires_multiple_schemas`},"019d7775-6564-719d-9948-67dd885e7632":{name:`object_referenced_schema_has_fields`,predicate:`object_referenced_schema_has_fields`},"019d7775-6564-75ce-a807-02396e0b8394":{name:`composite_referenced_schemas_must_be_objects`,predicate:`composite_referenced_schemas_must_be_objects`},"019d7775-6564-75bc-abf5-6ddd81b7d2d4":{name:`schema_reference_form_correct`,predicate:`schema_reference_form_correct`}}},"019d7775-6565-7ab6-9c65-ab60632aeca8":{name:`Constraint`,type:`composite`,schema:[{id:`019d7775-6565-7068-9cb9-28460e0368f0`},{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}]},"019d7775-6566-765a-b142-157d1d6367b7":{name:`FieldTypeEnum`,type:`string`,values:[`unknown`,`string`,`number`,`integer`,`decimal`,`boolean`,`array`,`set`,`enum`,`object`,`record`,`union`,`composite`,`geometry`,`bytes`]},"019d7775-6566-7a70-8b56-e55704397fc2":{name:`ComparisonOperatorEnum`,type:`string`,values:[`eq`,`neq`,`lt`,`lte`,`gt`,`gte`,`in`,`nin`,`contains`,`ncontains`,`exists`,`nexists`]},"019d7775-6565-74a3-a6e0-881a0148fc59":{name:`IndexConditionUnion`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"019d7775-6565-7068-9cb9-28460e0368f0":{name:`ConstraintMetadata`,fields:{"019d7775-6567-7017-9068-5bfcd1f9fef0":{name:`name`,required:!0,type:`string`},"019d7775-6568-7b0d-ad28-391874b5e3de":{name:`description`,type:`string`},"ee8ea6de-95f9-48e5-a047-4744b39e1c8f":{name:`metadata`,type:`record`}}},"019d7775-6565-703f-95cf-e7511d33c91b":{name:`ConstraintRule`,fields:{"019d7775-6568-7ad6-9e45-7fdbdef3fc7e":{name:`predicate`,required:!0,type:`string`},"019d7775-6568-7221-a4de-7e82d6c9f6bc":{name:`parameters`,type:`unknown`},"019d7775-6568-7d8b-a230-328fe5d352e8":{name:`fields`,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`},"019d7775-6563-752b-bf9d-bf8936472250":{name:`constraint_rule_requires_predicate`,predicate:`constraint_rule_requires_predicate`},"019d7775-6564-7fc2-bbdb-0fc59e567f2b":{name:`constraint_fields_exist`,predicate:`constraint_fields_exist`}}},"019d7775-6565-7ea4-a820-e8bcdbc690bc":{name:`ConstraintGroup`,fields:{"019d7775-6568-7ebf-a1c3-829de95d9216":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6568-76d9-a21c-fb289c36cc70":{name:`rules`,required:!0,type:`array`,schema:{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`}}},"019d7775-6565-7f2a-956b-35b40caa8ec7":{name:`SchemaReference`,fields:{"019d7775-6568-762c-932e-89b85f08a951":{name:`id`,required:!0,type:`string`},"019d7775-6568-77eb-bd9d-d874a2354fd5":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6568-7ed9-99b3-90cca439ee2e":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"abb42217-0f28-4501-ab12-c54bfe5d1492":{name:`description`,type:`string`},"ed9a369c-1c98-4bc7-845b-92fa81be31f8":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-79f2-9d64-d6b8c2696deb":{name:`schema_reference_id_required`,predicate:`schema_reference_id_required`}}},"019d7775-6565-71bc-84f3-044dc089d50c":{name:`SchemaReferenceArray`,type:`array`,schema:{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`}},"019d7775-6566-7e05-ab1b-6f768f459927":{name:`InlineTypeEnum`,type:`string`,values:[`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]},"019d7775-6566-7df4-bc09-67956251c4a2":{name:`IndexTypeEnum`,type:`string`,values:[`normal`,`unique`,`primary`,`spatial`,`fulltext`]},"019d7775-6566-7a63-9e10-5d11c8eb607c":{name:`String`,type:`string`},"019d7775-6565-74f6-9c72-9e35d3d16b0a":{name:`Index`,fields:{"019d7775-6567-7390-96c6-8154ccc6d1ad":{name:`name`,required:!0,type:`string`},"019d7775-6567-7568-98c8-76d64b745bc7":{name:`description`,type:`string`},"019d7775-6567-7a3d-b154-1199406b7851":{name:`unique`,type:`boolean`},"019d7775-6567-7e59-a2f4-ffc431e24fdd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7df4-bc09-67956251c4a2`}},"019d7775-6567-787d-921c-fa28f53cedcd":{name:`fields`,required:!0,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}},"019d7775-6567-76c9-869a-c4e25c154583":{name:`order`,type:`enum`,schema:{id:`019d7775-6566-7f2f-a009-0af144502aac`}},"019d7775-6567-7bc2-b870-6e1ae4faa558":{name:`condition`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"75f044b7-253c-4f48-93b7-692b9626c0ea":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-7c2d-b06d-82971c0b1fa9":{name:`index_fields_exist`,predicate:`index_fields_reference_valid`},"019d7775-6564-7660-adc8-3f1ab037d695":{name:`spatial_index_on_geometry_field`,predicate:`spatial_index_on_geometry_field`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`},"019d7775-6564-7285-a734-f7eed15d25e4":{name:`index_fields_not_empty`,predicate:`index_fields_not_empty`}}}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`},"019d7775-6564-7565-b53c-49667d23c237":{name:`global_field_id_uniqueness`,predicate:`global_field_id_uniqueness`},"019d7775-6564-7a5d-bb7e-d2e9e804123f":{name:`schema_reference_integrity`,predicate:`schema_reference_exists`},"019d7775-6563-7f89-8368-395733efec86":{name:`schema_name_required`,predicate:`schema_name_required`}}};const jn=new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`geometry`,`bytes`,`unknown`]),Mn=new Set([`array`,`set`]),Nn=[`fields`],Pn=[`type`,`default`,`values`,`schema`],Fn=new Set([`number`,`integer`,`decimal`,`string`]);function Q(e){let t=e.type;return t==null?[``,!1]:[String(t),!0]}function In(e){return jn.has(e)}function $(e){return Fn.has(e)}function Ln(e){return Mn.has(e)}function Rn(e,t){let n=e.schemas;return n&&n[t]||null}function zn(e){if(e.fields){let t=e.fields;if(Object.keys(t).length>0)return!0}let t=e.type;return t===`object`||t===`record`}function Bn(e){let t=e.schema;return t==null?[[],!1]:Array.isArray(t)?[t,!0]:[[t],!1]}function Vn(e,t,n){let r=Rn(e,t);if(!r)return[W(`ENUM_NAMED_SCHEMA_NOT_FOUND`,`Enum referenced schema '${t}' does not exist`,n)];let i=[],a=r.type;if(a==null)return i.push(W(`ENUM_NAMED_SCHEMA_NO_TYPE`,`Enum referenced schema '${t}' must have a 'type' (string or numeric)`,n)),i;let o=String(a);if(!(o===`string`||$(o)))return i.push(W(`ENUM_NAMED_SCHEMA_INVALID_TYPE`,`Enum referenced schema '${t}' type must be string or numeric, got '${a}'`,n)),i;let s=r.values;if(s==null)return i.push(W(`ENUM_NAMED_SCHEMA_MISSING_VALUES`,`Enum schema '${t}' must have a 'values' array`,n)),i;let c=s;if(!Array.isArray(c)||c.length===0)return i.push(W(`ENUM_NAMED_SCHEMA_EMPTY_VALUES`,`Enum schema '${t}' 'values' must be a non-empty array`,n)),i;let l=o===`string`;return c.forEach((e,r)=>{e!=null&&(l?typeof e!=`string`&&i.push(W(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be string, got ${typeof e}`,`${n} (values[${r}])`)):$(o)&&(Y(e)||i.push(W(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be numeric (type '${o}'), got ${typeof e}`,`${n} (values[${r}])`))))}),Nn.forEach(e=>{let a=r[e];a!=null&&Object.keys(a).length>0&&i.push(W(`ENUM_NAMED_SCHEMA_HAS_FIELDS`,`Enum referenced schema '${t}' must not have '${e}' (must be a simple value schema)`,n))}),i}function Hn(e,t){let n=[],r=e.type;if(r==null)return n.push(W(`ENUM_INLINE_NO_TYPE`,`Inline enum descriptor must have a 'type' (string or numeric)`,t)),n;let i=String(r);if(!(i===`string`||$(i)))return n.push(W(`ENUM_INLINE_INVALID_TYPE`,`Inline enum descriptor type must be string or numeric, got '${r}'`,t)),n;let a=e.values;if(a==null)return n.push(W(`ENUM_INLINE_MISSING_VALUES`,`Inline enum descriptor must have a 'values' array`,t)),n;let o=a;if(!Array.isArray(o)||o.length===0)return n.push(W(`ENUM_INLINE_EMPTY_VALUES`,`Inline enum descriptor 'values' must be a non-empty array`,t)),n;let s=i===`string`;return o.forEach((e,r)=>{e!=null&&(s?typeof e!=`string`&&n.push(W(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be string, got ${typeof e}`,`${t}.values[${r}]`)):$(i)&&(Y(e)||n.push(W(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be numeric (type '${i}'), got ${typeof e}`,`${t}.values[${r}]`))))}),n}function Un(e,t,n){if(typeof e!=`object`||!e)return[W(`ENUM_REF_INVALID`,`Enum schema reference must be an object`,t)];let r=e,i=`id`in r,a=`type`in r;if(i&&a)return[W(`ENUM_REF_AMBIGUOUS`,`Enum schema reference cannot have both 'id' (named) and 'type' (inline)`,t)];if(i){let e=r.id;return e==null||e===``?[W(`ENUM_NAMED_REF_INVALID_ID`,`Named enum reference 'id' must be a non-empty string`,t)]:Vn(n,String(e),t)}return a?Hn(r,t):[W(`ENUM_REF_NO_ID_OR_TYPE`,`Enum schema reference must have either 'id' (named) or 'type' (inline)`,t)]}function Wn(e,t){let n=t.split(`.`),r=e.fields&&typeof e.fields==`object`?e.fields:null,i=null;for(let t=0;t<n.length;t++){if(!r)return null;let a=null;for(let e of Object.values(r))if(e&&typeof e==`object`&&e.name===n[t]){a=e;break}if(!a)return null;if(i=a,t<n.length-1){let t=a.schema;if(t&&typeof t==`object`&&!Array.isArray(t)){if(typeof t.id==`string`){let n=t.id,i=e.schemas?.[n];r=i&&typeof i==`object`?i.fields??null:null}else r=t.fields?t.fields:null}else r=null}}return i?{def:i}:null}const Gn={primitives_prohibit_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;return r&&In(n)&&i!=null?[W(`PRIMITIVE_HAS_SCHEMA`,`Primitive type '${n}' cannot have a schema reference`,``)]:[]},enum_fields_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`enum`)return[];if(t.schema==null)return[W(`ENUM_MISSING_SCHEMA`,`Enum field must have a schema reference`,``)];let[i,a]=Bn(t);if(i.length===0)return[W(`ENUM_NO_VALID_REFS`,`Enum field has no valid schema references`,``)];let o=[];return i.forEach((t,n)=>{let r=a?`schema[${n}]`:`schema`;o.push(...Un(t,r,e.root))}),o},composite_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;if(r&&n===`composite`){if(i==null)return[W(`COMPOSITE_MISSING_SCHEMA`,`Composite type must have schema references`,``)];if(Array.isArray(i)){if(i.length<2)return[W(`COMPOSITE_INSUFFICIENT_SCHEMAS`,`Composite must have at least 2 schema references`,``)]}else return[W(`COMPOSITE_SCHEMA_NOT_ARRAY`,`Composite 'schema' must be an array of references`,``)]}return[]},composite_referenced_schemas_must_be_objects:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`composite`)return[];let i=t.schema;if(!Array.isArray(i)||i.length===0)return[];let a=[],o=e.root,s=(e,t)=>{let n=Rn(o,e);if(!n)return[];let r=[],i=n.type;if(i!=null){let e=String(i);if(e===`composite`)return[];if(e===`union`){let e=n.schema;return Array.isArray(e)&&e.forEach((e,n)=>{typeof e==`object`&&e&&`id`in e&&r.push(...s(String(e.id),`${t}.schema[${n}]`))}),r}}return zn(n)||r.push(W(`COMPOSITE_REF_NOT_OBJECT`,`Composite schema '${e}' must effectively represent an object type (has fields, or type 'object'/'record')`,t)),r};return i.forEach((e,t)=>{typeof e==`object`&&e&&`id`in e&&a.push(...s(String(e.id),`schema[${t}]`))}),a},object_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t),i=t.schema;return r&&n===`object`&&i==null?[W(`OBJECT_MISSING_SCHEMA`,`Object type must have a schema reference`,``)]:[]},object_referenced_schema_has_fields:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let[r,i]=Q(n);if(!i||r!==`object`)return[];let a=n.schema;if(typeof a!=`object`||!a||Array.isArray(a))return[];let o=a.id;if(!o)return[];let s=Rn(t,String(o));if(!s)return[];let c=s.fields;return c==null?[W(`OBJECT_REF_NO_FIELDS`,`Object field references schema '${o}' which has no 'fields' (must be Schema mode)`,``)]:typeof c!=`object`||Object.keys(c).length===0?[W(`OBJECT_REF_EMPTY_FIELDS`,`Object field references schema '${o}' which has empty fields`,``)]:[]},spatial_index_on_geometry_field:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n||n.type!==`spatial`)return[];let r=n.fields;if(!Array.isArray(r))return[];for(let e of r){let n=String(e),r=Wn(t,n);if(!r)continue;let i=r.def.type;if(i!==`geometry`)return[W(`SPATIAL_INDEX_NON_GEOMETRY`,`Spatial index can only reference geometry fields, but field '${n}' has type '${i}'`,``)]}return[]},index_condition_value_matches_field_type:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let r=n.field;if(r==null)return[];let i=n.value,a=Wn(t,String(r));if(!a)return[];let o=a.def.type,s=!0;switch(o){case`string`:typeof i!=`string`&&i!==null&&(s=!1);break;case`boolean`:typeof i!=`boolean`&&i!==null&&(s=!1);break;case`integer`:!Qt(i)&&i!==null&&(s=!1);break;case`number`:case`decimal`:!Y(i)&&i!==null&&(s=!1)}return s?[]:[W(`INDEX_CONDITION_VALUE_TYPE_MISMATCH`,`Value for field '${r}' must be ${o}`,``)]},schema_reference_exists:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schemas||{},r=[],i=(e,t)=>{if(e&&typeof e==`object`&&`id`in e){let i=e.id;typeof i==`string`&&!n[i]&&r.push(W(`SCHEMA_REFERENCE_NOT_FOUND`,`Referenced schema '${i}' does not exist`,t))}},a=(e,t)=>{if(!(typeof e!=`object`||!e)){if(e.hasOwnProperty(`schema`)){let n=e.schema;Array.isArray(n)?n.forEach((e,n)=>i(e,`${t}.schema[${n}]`)):i(n,`${t}.schema`)}for(let[n,r]of Object.entries(e))typeof r==`object`&&r&&a(r,t?`${t}.${n}`:n)}};return a(t,``),r},default_matches_type:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.type,r=t.default;if(n===void 0||r==null)return[];let i=String(n),a=``;switch(i){case`string`:typeof r!=`string`&&(a=`must be string`);break;case`boolean`:typeof r!=`boolean`&&(a=`must be boolean`);break;case`integer`:Qt(r)||(a=`must be integer`);break;case`number`:case`decimal`:Y(r)||(a=`must be numeric`);break;case`array`:case`set`:Array.isArray(r)||(a=`must be array`);break;case`object`:case`record`:(typeof r!=`object`||Array.isArray(r))&&(a=`must be object`);break;case`geometry`:Array.isArray(r)||(a=`must be array of coordinate arrays`)}return a?[W(`DEFAULT_VALUE_TYPE_MISMATCH`,`Default value ${JSON.stringify(r)} ${a} for type ${i}`,``)]:[]},global_field_id_uniqueness:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=new Map,r=[],i=(e,t)=>{if(!(typeof e!=`object`||!e))for(let i of Object.keys(e)){let e=n.get(i);e?r.push(W(`DUPLICATE_FIELD_ID`,`Field ID '${i}' is not unique; already used at ${e}`,`${t}.${i}`)):n.set(i,`${t}.${i}`)}};t.fields&&i(t.fields,`fields`);let a=t.schemas;if(a&&typeof a==`object`){for(let[e,t]of Object.entries(a))if(typeof t==`object`&&t){let n=t;n.fields&&i(n.fields,`schemas.${e}.fields`)}}return r},constraint_fields_exist:e=>{let t=e.root,n=[],r=(e,n)=>{let r=e.split(`.`),i=n;for(let e=0;e<r.length;e++){let n=r[e],a=i[n];if(a===void 0)return!1;if(e===r.length-1)return!0;let o=a;if(o.type===`object`){let e=o.schema;if(e&&e.id){let n=Rn(t,String(e.id));if(n&&n.fields){i=n.fields;continue}}}return!1}return!1},i=(e,t,i)=>{for(let[a,o]of Object.entries(e)){if(typeof o!=`object`||!o)continue;let e=o;if(e.predicate&&e.fields){let o=e.fields;Array.isArray(o)&&o.forEach((e,o)=>{typeof e==`string`&&(r(e,t)||n.push(W(`CONSTRAINT_FIELD_NOT_FOUND`,`Constraint '${a}' references non-existent field path '${e}'`,`${i}constraints.${a}.fields[${o}]`)))})}}},a=t.fields,o=t.constraints;o&&a&&i(o,a,``);let s=t.schemas;if(s){for(let[e,t]of Object.entries(s))if(typeof t==`object`&&t){let n=t,r=n.constraints,a=n.fields;r&&a&&i(r,a,`schemas.${e}.`)}}return n},inline_type_descriptor_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schema;if(!n||typeof n!=`object`||Array.isArray(n))return[];let r=t.type,i=n.type;if(r===void 0||i===void 0)return[];let a=new Set([`record`,`array`,`set`,`enum`]),o=String(r);if(!a.has(o))return[W(`INLINE_DESCRIPTOR_NOT_ALLOWED_FOR_FIELD_TYPE`,`Inline type descriptors are only allowed for fields of type record, array, set, or enum, but got '${o}'`,``)];let s=String(i);if(!new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]).has(s))return[W(`INLINE_DESCRIPTOR_INVALID_TYPE`,`Inline descriptor type '${s}' is not allowed (only primitives and 'record')`,``)];let c=n.values;if(c!=null){if(s!==`string`&&!$(s))return[W(`INLINE_DESCRIPTOR_VALUES_WITHOUT_ENUM`,`'values' can only be used with string or numeric inline types`,``)];if(!Array.isArray(c)||c.length===0)return[W(`INLINE_DESCRIPTOR_VALUES_NOT_ARRAY`,`'values' must be a non‑empty array`,``)]}return[]},schema_reference_form_correct:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r)return[];let i=t.schema;if(i==null)return[];let a=e=>({isNamed:`id`in e,isInline:`type`in e});if(Array.isArray(i)){if(i.length===0)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires at least one reference`,``)];if(n!==`union`&&n!==`composite`&&n!==`enum`)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' cannot use an array of references`,``)];for(let e=0;e<i.length;e++){let t=i[e];if(typeof t!=`object`||!t)return[W(`SCHEMA_REF_FORM_INVALID`,`Element ${e} of schema array is not an object`,``)];let{isNamed:r,isInline:o}=a(t);if(n===`enum`){if(!r&&!o)return[W(`SCHEMA_REF_FORM_INVALID`,`Enum array element ${e} must be a named reference or inline descriptor`,``)]}else if(!r)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' array element ${e} must be a named schema reference (with 'id')`,``)]}}else if(typeof i==`object`){let{isNamed:e,isInline:t}=a(i);switch(n){case`array`:case`set`:case`record`:case`enum`:if(!e&&!t)return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires a single named reference or an inline descriptor`,``)];break;case`object`:if(!e)return[W(`SCHEMA_REF_FORM_INVALID`,`Object field requires a single named schema reference`,``)];break;case`union`:case`composite`:return[W(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires an array of references`,``)]}}return[]},collection_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r)return[];let i=t.schema;return Ln(n)&&i==null?[W(`COLLECTION_MISSING_SCHEMA`,`Collection type '${n}' must have a schema reference`,``)]:[]},union_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`union`)return[];let i=t.schema;return i==null?[W(`UNION_MISSING_SCHEMA`,`Union type must have schema references`,``)]:Array.isArray(i)?i.length<2?[W(`UNION_INSUFFICIENT_SCHEMAS`,`Union type must have at least 2 schema references, got ${i.length}`,``)]:[]:[W(`UNION_SCHEMA_NOT_ARRAY`,`Union type schema must be an array of schema references`,``)]},record_schema_cardinality:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Q(t);if(!r||n!==`record`)return[];let i=t.schema;return Array.isArray(i)?[W(`RECORD_SCHEMA_ARRAY`,`Record type must have zero or one schema reference, not an array`,``)]:[]},nested_schema_exclusive_mode:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!1;for(let e of Nn){let r=t[e];if(typeof r==`object`&&r&&Object.keys(r).length>0){n=!0;break}}let r=!1;for(let e of Pn)if(t[e]!=null){r=!0;break}return n&&r?[W(`NESTED_SCHEMA_MIXED_MODE`,`NestedSchema cannot mix BaseSchema fields (fields/indexes/constraints) with FieldProperties (type/values/schema)`,``)]:!n&&!r?[W(`NESTED_SCHEMA_NO_MODE`,`NestedSchema must have either BaseSchema fields or FieldProperties, not neither`,``)]:[]},constraint_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=String(t.name||`UNNAMED`),r=!!(t.predicate&&String(t.predicate)!==``),i=!!(t.operator&&Array.isArray(t.rules)&&t.rules.length>0);return r&&i?[W(`CONSTRAINT_MIXED_TYPE`,`Constraint cannot have both predicate (rule) and operator+rules (group)`,n)]:!r&&!i?[W(`CONSTRAINT_NO_TYPE`,`Constraint must have either predicate (rule) or operator+rules (group)`,n)]:[]},constraint_rule_requires_predicate:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.predicate;return n===void 0?[W(`REQUIRED_FIELD_MISSING`,`Required field 'predicate' is missing`,`predicate`)]:n===null?[W(`REQUIRED_FIELD_NULL`,`Required field 'predicate' cannot be null`,`predicate`)]:typeof n==`string`?n.trim()===``?[W(`REQUIRED_FIELD_EMPTY`,`Required field 'predicate' cannot be empty`,`predicate`)]:[]:[W(`REQUIRED_FIELD_WRONG_TYPE`,`Required field 'predicate' must be a string`,`predicate`)]},index_condition_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!!(t.field&&t.operator&&t.value!==void 0),r=!!(t.operator&&Array.isArray(t.conditions)&&t.conditions.length>0);return n&&r?[W(`INDEX_CONDITION_MIXED_TYPE`,`IndexCondition cannot have both single condition fields and group fields`,``)]:!n&&!r?[W(`INDEX_CONDITION_NO_TYPE`,`IndexCondition must be either a single condition or a group`,``)]:[]},schema_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[W(`SCHEMA_NAME_MISSING`,`Schema name is required`,`name`)]:[]},field_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[W(`FIELD_NAME_MISSING`,`Field name is required`,`name`)]:[]},index_fields_not_empty:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.fields;return!Array.isArray(n)||n.length===0?[W(`INDEX_FIELDS_EMPTY`,`Index must reference at least one field`,`fields`)]:[]},schema_reference_id_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.id;return n==null||String(n).trim()===``?[W(`SCHEMA_REFERENCE_ID_MISSING`,`SchemaReference ID is required`,`id`)]:[]},index_fields_reference_valid:e=>[]};var Kn=class{static validator=null;static async validate(e){return this.validator||=await kn.create(An,Gn),await this.validator.validate(e)}};exports.AnansiCodec=Ot,exports.Compiler=c,exports.DataTypes=u,exports.DocumentValidator=kn,exports.FD_NO_CHILD=63,exports.FLAG_COMPRESSED=4,exports.FLAG_ENCRYPTED=64,exports.FLAG_HASH_PRESENT=128,exports.Literal=n,exports.MAX_SCHEMA_SLOTS=63,exports.MULTI_STEP_BASE=f,exports.MigrationEngine=Ut,exports.MigrationError=L,exports.MigrationErrorCode=It,exports.SchemaValidator=Kn,exports.addressForSteps=oe,exports.buildEnum=l,exports.buildManifest=de,exports.bumpVersion=Mt,exports.calculateNextBump=jt,exports.classifyChangeImpact=At,exports.decodeAnansiBatch=Dt,exports.decodeAnansiPacket=Ct,exports.decodeBatch=st,exports.decodeDocument=rt,exports.deepMerge=Pt,exports.defaultValidationConfig=Wt,exports.encodeAnansiBatchColumnar=Et,exports.encodeAnansiBatchRows=Tt,exports.encodeAnansiPacket=St,exports.encodeBatchColumnar=at,exports.encodeBatchRows=it,exports.encodeDocument=nt,exports.internalDP=p,exports.link=ce,exports.makeDescriptor=m,exports.metaSchemaPredicateMap=Gn,exports.parseSchema=r,exports.schemaChangeToPatch=kt,exports.sha256=Ft,exports.unpackDescriptor=h,exports.userDataDP=se;
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{blake3 as e}from"hash-wasm";import{decompress as t}from"fzstd";var n=class e{kind;value;constructor(e,t){this.kind=e,this.value=t}isZero(){return this.kind===`zero`}isNull(){return this.kind===`null`}static fromJSON(t){if(t===void 0)return new e(`zero`,void 0);if(t===null)return new e(`null`,void 0);if(typeof t==`string`)return new e(`string`,t);if(typeof t==`boolean`)return new e(`boolean`,t);if(typeof t==`number`)return Number.isInteger(t)?new e(`integer`,t):new e(`float`,t);if(Array.isArray(t))return new e(`array`,t);if(typeof t==`object`)return new e(`object`,t);throw Error(`anansi: unsupported literal ${typeof t}`)}};function r(e){let t=typeof e==`string`?JSON.parse(e):e;if(!t.name)throw Error(`anansi: schema requires a name`);return t}function i(e){return!!e&&!Array.isArray(e)&&typeof e.id==`string`}function a(e){return Array.isArray(e)}function o(e){return!!e&&!Array.isArray(e)&&typeof e.id!=`string`}function s(e,t){return e?`${e}.${t}`:t}var c=class{source;nested=new Map;building=new Set;constructor(e){this.source=e}compile(){for(let e of Object.keys(this.source.schemas??{}))this.compileNested(e);return{root:this.compileFields(this.source.fields??{},[],``),schemas:this.nested}}compileNested(e){let t=this.nested.get(e);if(t)return t;if(this.building.has(e))return null;let n=(this.source.schemas??{})[e];if(!n)throw Error(`anansi: schema '${e}' not found`);this.building.add(e);try{let t=n.values&&n.values.length>0?`enum`:n.type??`unknown`,r={id:e,name:n.name,effectiveType:t,fields:[],isRecursive:!1,values:n.values,enumDef:t===`enum`?l(n.values??[],n.type):void 0};return this.nested.set(e,r),n.fields&&(r.fields=this.compileFields(n.fields,[e],``)),r.isRecursive=r.fields.some(e=>e.kind.tag===`recursive`),r}finally{this.building.delete(e)}}compileFields(e,t,n){let r=[];for(let[i,a]of Object.entries(e))r.push(this.compileField(i,a,t,n));return r}compileField(e,t,r,c){let u=t.type??`unknown`,d={id:e,name:t.name,path:s(c,t.name),type:u,required:!!t.required,deprecated:!!t.deprecated,unique:!!t.unique,nullable:t.nullable!==!1,hasDefault:!n.fromJSON(t.default).isZero(),kind:{tag:`scalar`}},f=t.schema;switch(u){case`geometry`:break;case`record`:d.kind=i(f)?{tag:`container`,c:{itemSchema:this.resolve(f.id)??void 0,record:!0}}:{tag:`container`,c:{record:!0}};break;case`array`:if(a(f))throw Error(`anansi: ${d.path}: array cannot reference multiple schemas`);if(i(f))d.kind={tag:`container`,c:{itemSchema:this.require(f.id,d.path),record:!1}};else if(o(f)){let e=f.type??`unknown`,t={itemType:e,record:!1};(f.values?.length??0)>0&&(t.itemEnum=l(f.values,e)),d.kind={tag:`container`,c:t}}else d.kind={tag:`container`,c:{record:!1}};break;case`object`:d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:o(f)?{tag:`object`,schema:this.synthesizeInline(f,e,r)}:{tag:`scalar`};break;case`union`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`union`,variants:e}}else d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:{tag:`scalar`};break;case`composite`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`composite`,parts:e}}else d.kind={tag:`scalar`};break;case`enum`:if((t.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(t.values,u)};else if(o(f)&&(f.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(f.values,f.type??`string`)};else if(i(f)){let e=this.resolve(f.id);d.kind=e&&e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}else d.kind={tag:`scalar`};break;default:if(i(f)){let e=this.resolve(f.id);d.kind=e===null?{tag:`recursive`,schemaId:f.id}:e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}}return d}resolve(e){return this.nested.has(e)?this.nested.get(e):this.building.has(e)?null:this.compileNested(e)}require(e,t){let n=this.resolve(e);if(!n)throw Error(`anansi: ${t}: unresolvable schema reference '${e}'`);return n}synthesizeInline(e,t,n){let r=`inline:${[...n,t].join(`/`)}`,i=this.nested.get(r);if(i)return i;let a={id:r,name:r,effectiveType:e.type??`object`,fields:[],isRecursive:!1};this.nested.set(r,a);let o=e.fields??{};return a.fields=this.compileFields(o,[...n,r],``),a}};function l(e,t){let n=new Map,r=[];for(let t of e)t===null||[`string`,`number`,`boolean`].includes(typeof t)?n.set(JSON.stringify(t),t):r.push(t);return{lookup:n,complex:r,expectNumeric:t===`integer`||t===`number`}}const u={TypeUnknown:0,TypeInt:1,TypeFloat:2,TypeString:3,TypeBool:4,TypeBytes:5,TypeGeometry:6,TypeRecord:7,TypeArrayUnknown:8,TypeArrayInt:9,TypeArrayFloat:10,TypeArrayString:11,TypeArrayBool:12,TypeArrayBytes:13,TypeArrayObject:14,TypeArrayGeometry:15},d={[u.TypeUnknown]:`unknown`,[u.TypeInt]:`int`,[u.TypeFloat]:`float`,[u.TypeString]:`string`,[u.TypeBool]:`bool`,[u.TypeBytes]:`bytes`,[u.TypeGeometry]:`geometry`,[u.TypeRecord]:`record`,[u.TypeArrayUnknown]:`array_unknown`,[u.TypeArrayInt]:`array_int`,[u.TypeArrayFloat]:`array_float`,[u.TypeArrayString]:`array_string`,[u.TypeArrayBool]:`array_bool`,[u.TypeArrayBytes]:`array_bytes`,[u.TypeArrayObject]:`array_object`,[u.TypeArrayGeometry]:`array_geometry`},f=63,p=63,m=16384;function ee(e){return(e.raw&4294967264|(e.raw>>>28&15)<<1)>>>0}function te(e,t,n,r,i){let a=0;return a|=(e&15)<<28,a|=(n&63)<<22,a|=(r&127)<<15,a|=(i.child&63)<<9,a|=(t&3)<<7,i.required&&(a|=64),i.hasDefault&&(a|=32),i.deprecated&&(a|=16),i.unique&&(a|=8),i.terminal&&(a|=4),i.nullable&&(a|=2),i.recursive&&(a|=1),h(a>>>0)}function h(e){return{raw:e,dt:e>>>28&15,schemaIdx:e>>>22&63,fieldIdx:e>>>15&127,childSchemaIdx:e>>>9&63,kind:e>>>7&3,required:!!(e&64),hasDefault:!!(e&32),deprecated:!!(e&16),unique:!!(e&8),terminal:!!(e&4),nullable:!!(e&2),recursive:!!(e&1)}}function ne(e){switch(e){case`string`:return u.TypeString;case`number`:return u.TypeFloat;case`decimal`:return u.TypeString;case`integer`:return u.TypeInt;case`boolean`:return u.TypeBool;case`bytes`:return u.TypeBytes;default:return u.TypeUnknown}}function re(e){return e?.expectNumeric?u.TypeInt:u.TypeString}function ie(e,t){if(e)return u.TypeArrayObject;switch(t){case`string`:return u.TypeArrayString;case`number`:return u.TypeArrayFloat;case`decimal`:return u.TypeArrayString;case`integer`:return u.TypeArrayInt;case`boolean`:return u.TypeArrayBool;case`bytes`:return u.TypeArrayBytes;case`geometry`:return u.TypeArrayGeometry;case`unknown`:return u.TypeArrayUnknown;default:return u.TypeUnknown}}function ae(e){let t=e.kind;if(e.type===`geometry`)return[u.TypeGeometry,0,!0];if(t.tag===`scalar`)return[ne(e.type),0,!0];if(t.tag===`enum`)return[re(t.enumDef),0,!0];if(t.tag===`recursive`)return[u.TypeUnknown,1,!0];if(t.tag===`object`)return[u.TypeUnknown,1,!1];if(t.tag===`container`){let e=!t.c.itemSchema;return t.c.record?[u.TypeRecord,1,e]:[ie(t.c.itemSchema,t.c.itemType),2,e]}return t.tag===`union`?[u.TypeUnknown,3,!1]:[u.TypeUnknown,1,!1]}var oe=class{slots=[];descriptors=[];metas=[];localOffsets=[];fieldTypes=[];linkedSlots=[];assignSlot(e){if(this.slots.length>=63)throw Error(`anansi: exceeds maximum of 63 nested schemas while assigning '${e.name}'`);let t=this.slots.length;return this.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),this.linkedSlots.push({idx:t,slot:this.slots[t],fields:[]}),t}childSlotForField(e,t){let n=e.kind;return n.tag===`recursive`?{idx:t,kindTag:n}:n.tag===`object`?{idx:this.assignSlot(n.schema),kindTag:n}:n.tag===`composite`?{idx:this.assignSlot({id:`composite:${e.path}`,name:`composite:${e.name}`,effectiveType:`object`,fields:se(n.parts),isRecursive:!1}),kindTag:n}:n.tag===`container`&&n.c.itemSchema?{idx:this.assignSlot(n.c.itemSchema),kindTag:n}:null}linkFields(e,t){let n=this.linkedSlots[t],r=[];for(let i=0;i<e.length;i++){if(e.length>128)throw Error(`anansi: slot ${t} exceeds 128 fields`);let a=e[i],[o,s,c]=ae(a),l=this.childSlotForField(a,t),u=l?l.idx:63,d=a.kind.tag===`recursive`,f=te(o,s,t,i,{required:a.required,hasDefault:a.hasDefault,deprecated:a.deprecated,unique:a.unique,terminal:c,nullable:a.nullable,recursive:d,child:u}),p=this.descriptors.length;this.descriptors.push(f),this.fieldTypes.push(a.type);let m={meta:{name:a.name,path:a.path},fd:f,dp:0,abs:p};n.fields.push(m),this.metas.push(m.meta),!c&&u!==63&&r.push({rf:a,fd:f,childIdx:u})}for(let e of r){let t=e.rf.kind;if(t.tag===`object`||t.tag===`composite`){let n=t.tag===`object`?t.schema.fields:se(t.parts),r=this.linkFields(n,e.childIdx);this.setSlot(e.childIdx,r.count)}else if(t.tag===`container`&&t.c.itemSchema){let n=this.linkFields(t.c.itemSchema.fields,e.childIdx);this.setSlot(e.childIdx,n.count)}let r=n.fields.find(t=>t.fd===e.fd);r.child=this.linkedSlots[e.childIdx]}return{count:e.length,slot:n}}setSlot(e,t){let n=this.slots[e];n.fieldStart===-1&&(n.fieldStart=this.descriptors.length-t),n.fieldCount=t}};function se(e){let t=[];for(let n of e)t.push(...n.fields);return t}function ce(e){for(let t=e.slots.length-1;t>=0;t--){let n=e.slots[t];n.fieldCount<0&&(n.fieldStart=0,n.fieldCount=0);let r=0;for(let t=0;t<n.fieldCount;t++){let i=n.fieldStart+t,a=e.descriptors[i];a.terminal?r+=1:a.childSchemaIdx!==63&&(r+=e.slots[a.childSchemaIdx].footprint)}n.footprint=r}for(let t of e.slots){let n=0;for(let r=0;r<t.fieldCount;r++){let i=t.fieldStart+r,a=e.descriptors[i];e.localOffsets[i]=n,a.terminal?n+=1:a.childSchemaIdx!==63&&(n+=e.slots[a.childSchemaIdx].footprint)}}}function le(e,t,n,r){if(r.length===0)return 0;if(r.length===1){let[n,i]=r[0],a=e[n].fieldStart+i;return t[a].terminal?a+1>>>0:0}let i=m;for(let a=0;a<r.length;a++){let[o,s]=r[a],c=e[o].fieldStart+s,l=t[c];if(i+=n[c],a===r.length-1)return l.terminal?i>>>0:0;if(l.terminal)return 0}return i>>>0}function ue(e,t){return(t<<5|e<<1)>>>0}function de(e){let{root:t,schemas:n}=new c(e).compile(),r=new oe;r.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),r.linkedSlots.push({idx:0,slot:r.slots[0],fields:[]});let i=r.linkFields(t,0);return r.slots[0].fieldStart=0,r.slots[0].fieldCount=i.count,ce(r),pe(r,r.linkedSlots[0],[]),{slots:r.slots,metas:r.metas,descriptors:r.descriptors,localOffsets:r.localOffsets,fieldTypes:r.fieldTypes,root:r.linkedSlots[0],childrenByPath:fe(r.linkedSlots[0])}}function fe(e){let t=new Map;for(let n of e.fields)n.child&&t.set(n.meta.path,n.child);return t}function pe(e,t,n){for(let r=0;r<t.slot.fieldCount;r++){let i=t.slot.fieldStart+r,a=e.descriptors[i],o=t.fields[r],s=[...n,[t.idx,r]],c=le(e.slots,e.descriptors,e.localOffsets,s);o.dp=c===0?ee(a):ue(a.dt,c),!a.terminal&&o.child&&pe(e,o.child,s)}}function me(e){let t=(e,n,r)=>{let i=[];for(let a=0;a<e.slot.fieldCount;a++){let o=e.fields[a],s=o.fd,c=[...n,[e.idx,a]],l=r?`${r}.${o.meta.name}`:o.meta.name,f={name:o.meta.name,path:l,t:d[s.dt],dp:o.dp};if(!s.terminal&&o.child){s.dt===u.TypeArrayObject?(f.child=t(o.child,c,l),i.push(f)):i.push(...t(o.child,c,l));continue}i.push(f)}return i};return t(e.root,[],``)}function g(e,t){for(t>>>=0;t>=128;)e.push(t&127|128),t>>>=7;e.push(t)}function he(e,t){if(!Number.isSafeInteger(t))throw Error(`anansi: integer ${t} exceeds Number.MAX_SAFE_INTEGER`);_e(e,BigInt(t))}function ge(e,t){for(;t>=128n;)e.push(Number(t&127n)|128),t>>=7n;e.push(Number(t))}function _e(e,t){ge(e,t<<1n^t>>63n)}var _=class{data;pos;constructor(e,t=0){this.data=e,this.pos=t}eof(){return this.pos>=this.data.length}remaining(){return this.data.length-this.pos}byte(){if(this.eof())throw Error(`anansi: unexpected end of buffer`);return this.data[this.pos++]}take(e){if(e<0||this.pos+e>this.data.length)throw Error(`anansi: unexpected end of buffer (need ${e}, have ${this.remaining()})`);let t=this.data.subarray(this.pos,this.pos+e);return this.pos+=e,t}uvarint(){let e=0,t=0;for(let n=0;n<10;n++){let r=this.byte();if(r<128){if(n===9&&r>1)throw Error(`anansi: varint overflow`);return(e|r<<t)>>>0}e|=(r&127)<<t,t+=7}throw Error(`anansi: varint overflow`)}uvarintBig(){let e=0n,t=0n,n=0;for(;n<10;){let r=this.byte();if(e|=BigInt(r&127)<<t,r<128)return e;t+=7n,n++}throw Error(`anansi: varint overflow`)}varint(){let e=this.uvarintBig(),t=e>>1n^-(e&1n),n=Number(t);if(!Number.isSafeInteger(n))throw Error(`anansi: decoded integer ${n} exceeds Number.MAX_SAFE_INTEGER`);return n}};const v={Unknown:0,Int:1,Float:2,String:3,Bool:4,Bytes:5,Geometry:6,Record:7,ArrayUnknown:8,ArrayInt:9,ArrayFloat:10,ArrayString:11,ArrayBool:12,ArrayBytes:13,ArrayObject:14,ArrayGeometry:15},ve=new TextEncoder,ye=new TextDecoder;function be(e){let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.subarray(r,r+n));return btoa(t)}function xe(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function Se(e,t){he(e,t)}function Ce(e){return e.varint()}function y(e,t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t,!0);for(let t=0;t<8;t++)e.push(n.getUint8(t))}function b(e){let t=e.take(8);return new DataView(t.buffer,t.byteOffset,8).getFloat64(0,!0)}function x(e,t){let n=ve.encode(t);g(e,n.length);for(let t of n)e.push(t)}function S(e){let t=e.uvarint();return ye.decode(e.take(t))}function we(e,t){e.push(+!!t)}function Te(e){return e.byte()!==0}function C(e,t){let n=new Uint8Array(t.length+7>>3);t.forEach((e,t)=>{e&&(n[t>>3]|=1<<(t&7))});for(let t of n)e.push(t)}function w(e,t){let n=e.take(t+7>>3),r=[];for(let e=0;e<t;e++)r.push(!!(n[e>>3]&1<<(e&7)));return r}function Ee(e,t){let n=xe(t);g(e,n.length);for(let t of n)e.push(t)}function De(e){let t=e.uvarint();return be(e.take(t))}function Oe(e,t){g(e,t.length);for(let n of t){g(e,n.length/2);for(let t=0;t<n.length;t+=2)y(e,n[t]),y(e,n[t+1])}}function ke(e){let t=e.uvarint(),n=[];for(let r=0;r<t;r++){let t=e.uvarint(),r=[];for(let n=0;n<t;n++)r.push(b(e),b(e));n.push(r)}return n}const T={Null:0,Int:v.Int,Float:v.Float,String:v.String,Bool:v.Bool,Bytes:v.Bytes,Geometry:v.Geometry,Record:v.Record,ArrUnknown:v.ArrayUnknown,ArrInt:v.ArrayInt,ArrFloat:v.ArrayFloat,ArrString:v.ArrayString,ArrBool:v.ArrayBool,ArrBytes:v.ArrayBytes};function Ae(e){return e.length>0&&Array.isArray(e[0])&&Array.isArray(e[0][0])}function je(e,t){if(t==null){e.push(T.Null);return}switch(typeof t){case`boolean`:e.push(T.Bool),we(e,t);return;case`string`:e.push(T.String),x(e,t);return;case`number`:Number.isSafeInteger(t)?(e.push(T.Int),Se(e,t)):(e.push(T.Float),y(e,t));return;case`object`:if(Array.isArray(t)){if(Ae(t)){e.push(T.Geometry),Oe(e,t);return}if(t.every(e=>typeof e==`string`)){e.push(T.ArrString),g(e,t.length);for(let n of t)x(e,n);return}if(t.every(e=>Number.isSafeInteger(e))){e.push(T.ArrInt),g(e,t.length);for(let n of t)Se(e,n);return}if(t.every(e=>typeof e==`number`)){e.push(T.ArrFloat),g(e,t.length);for(let n of t)y(e,n);return}if(t.every(e=>typeof e==`boolean`)){e.push(T.ArrBool);let n=t;g(e,n.length),C(e,n);return}e.push(T.ArrUnknown),g(e,t.length);for(let n of t)je(e,n);return}e.push(T.Record),Ne(e,t);return;default:throw Error(`anansi: unsupported any value ${typeof t}`)}}function Me(e){let t=e.byte();switch(t){case T.Null:return null;case T.Bool:return Te(e);case T.String:return S(e);case T.Int:return Ce(e);case T.Float:return b(e);case T.Bytes:return De(e);case T.Geometry:return ke(e);case T.Record:return Pe(e);case T.ArrInt:{let t=e.uvarint();return Array.from({length:t},()=>Ce(e))}case T.ArrFloat:{let t=e.uvarint();return Array.from({length:t},()=>b(e))}case T.ArrString:{let t=e.uvarint();return Array.from({length:t},()=>S(e))}case T.ArrBool:return w(e,e.uvarint());case T.ArrUnknown:{let t=e.uvarint();return Array.from({length:t},()=>Me(e))}default:throw Error(`anansi: unknown any tag ${t}`)}}function Ne(e,t){let n=Object.keys(t).sort();g(e,n.length);for(let r of n)x(e,r),je(e,t[r])}function Pe(e){let t=e.uvarint(),n={};for(let r=0;r<t;r++){let t=S(e);n[t]=Me(e)}return n}function Fe(e,t){g(e,t.length);for(let n of t)Se(e,n)}function Ie(e){let t=e.uvarint();return Array.from({length:t},()=>Ce(e))}function Le(e,t){g(e,t.length);for(let n of t)y(e,n)}function Re(e){let t=e.uvarint();return Array.from({length:t},()=>b(e))}function ze(e,t){g(e,t.length);for(let n of t)x(e,n)}function Be(e){let t=e.uvarint();return Array.from({length:t},()=>S(e))}function Ve(e,t){g(e,t.length),C(e,t)}function He(e){return w(e,e.uvarint())}function Ue(e,t){g(e,t.length);for(let n of t)Ee(e,n)}function We(e){let t=e.uvarint();return Array.from({length:t},()=>De(e))}function Ge(e,t){g(e,t.length);for(let n of t)Oe(e,n)}function Ke(e){let t=e.uvarint();return Array.from({length:t},()=>ke(e))}function qe(e,t){g(e,t.length);for(let n of t)je(e,n)}function Je(e){let t=e.uvarint();return Array.from({length:t},()=>Me(e))}const E={Dense:0,Sparse:1,Batch:2,Stream:3},Ye=4,Xe=64,Ze=128;function D(e,t){let n=t.split(`.`),r=e;for(let e=0;e<n.length;e++){if(typeof r!=`object`||!r)return[!1,void 0];let t=r;if(!(n[e]in t))return[!1,void 0];r=t[n[e]]}return r===void 0?[!1,void 0]:[!0,r]}function O(e,t){let[n,r]=D(e,t.path);return!n||r===void 0?`notSet`:r===null?`null`:`value`}function Qe(e,t){let n=t.length+1;return{...e,path:e.path.slice(n)}}function $e(e,t){return e.map(e=>Qe(e,t))}function et(e,t,n){switch(t.t){case`int`:Se(e,n);break;case`float`:y(e,n);break;case`string`:x(e,n);break;case`bool`:we(e,n);break;case`bytes`:Ee(e,n);break;case`geometry`:Oe(e,n);break;case`record`:Ne(e,n);break;case`unknown`:je(e,n);break;case`array_int`:Fe(e,n);break;case`array_float`:Le(e,n);break;case`array_string`:ze(e,n);break;case`array_bool`:Ve(e,n);break;case`array_bytes`:Ue(e,n);break;case`array_geometry`:Ge(e,n);break;case`array_unknown`:qe(e,n);break;default:throw Error(`anansi: cannot write value for type ${t.t} field ${t.path}`)}}function tt(e,t){switch(t.t){case`int`:return e.varint();case`float`:return b(e);case`string`:return S(e);case`bool`:return Te(e);case`bytes`:return De(e);case`geometry`:return ke(e);case`record`:return Pe(e);case`unknown`:return Me(e);case`array_int`:return Ie(e);case`array_float`:return Re(e);case`array_string`:return Be(e);case`array_bool`:return He(e);case`array_bytes`:return We(e);case`array_geometry`:return Ke(e);case`array_unknown`:return Je(e);default:throw Error(`anansi: cannot read value for type ${t.t} field ${t.path}`)}}function nt(e,t,n,r,i){let a=$e(t,n);g(e,r.length);for(let t of r){let n=0;for(let e of a)O(t,e)!==`notSet`&&n++;let r=a.length>0?n/a.length:1,o=!(a.length<=64||r>.25),s=o?E.Sparse:E.Dense,c=(i.flags&-200|s)>>>0,l=[],u={flags:i.flags&-4|s,version:i.version};o?j(l,a,t,u):k(l,a,t,u);let d=[c,i.version,...l];g(e,d.length);for(let t of d)e.push(t)}}function rt(e,t,n,r,i){let a=e.uvarint();for(let o=0;o<a;o++){let a=e.uvarint(),o=new _(e.take(a)),s=o.byte();o.byte();let c=i?i():{},l=s&3;if(l===E.Dense)A(o,t,c,n);else if(l===E.Sparse)M(o,t,c,n);else throw Error(`anansi: unsupported nested packet type ${l}`);r.push(c)}}const it={flags:E.Dense,version:0};function k(e,t,n,r=it){let i=2*t.length,a=new Uint8Array(i+7>>3);t.forEach((e,t)=>{let r=O(n,e)===`notSet`?0:O(n,e)===`null`?1:2;a[t>>2]|=r<<((t&3)<<1)});for(let t of a)e.push(t);let o=P(t);for(let t=0;t<=15;t++){let i=o[t];if(!(!i||i.length===0)){if(t===v.Bool){let t=[];for(let e of i)O(n,e)===`value`&&t.push(D(n,e.path)[1]);C(e,t);continue}for(let t of i)if(O(n,t)===`value`){if(t.t===`array_object`){nt(e,t.child,t.name,D(n,t.path)[1]??[],r);continue}et(e,t,D(n,t.path)[1])}}}}function A(e,t,n,r){let i=[],a=2*t.length,o=e.take(a+7>>3);for(let e=0;e<t.length;e++)i.push(o[e>>2]>>((e&3)<<1)&3);let s=P(t);for(let a=0;a<=15;a++){let o=s[a];if(o){if(a===v.Bool){let a=o.filter((e,n)=>i[t.indexOf(o[n])]===2).length,s=w(e,a),c=0;o.forEach(e=>{let a=t.indexOf(e);i[a]===2&&N(n,e.path,r,s[c++])});continue}for(let a of o){let o=i[t.indexOf(a)];if(o!==0){if(o===1){N(n,a.path,r,null);continue}if(a.t===`array_object`){let t=[];rt(e,a.child,a.name,t),N(n,a.path,r,t);continue}N(n,a.path,r,tt(e,a))}}}}}function j(e,t,n,r={flags:E.Sparse,version:0}){let i=t.filter(e=>O(n,e)!==`notSet`);g(e,i.length);for(let t of i){let i=O(n,t),a=t.dp;i===`null`?a|=1:a&=4294967294,g(e,a),i===`value`&&(t.t===`array_object`?nt(e,t.child,t.name,D(n,t.path)[1]??[],r):et(e,t,D(n,t.path)[1]))}}function M(e,t,n,r){let i=new Map;for(let e of t)i.set(e.dp&4294967294,e);let a=e.uvarint();for(let t=0;t<a;t++){let t=e.uvarint(),a=!!(t&1),o=i.get(t&4294967294);if(!o)throw Error(`anansi: sparse packet references unknown data point ${t}`);if(a){N(n,o.path,r,null);continue}if(o.t===`array_object`){let t=[];rt(e,o.child,o.name,t),N(n,o.path,r,t);continue}N(n,o.path,r,tt(e,o))}}function N(e,t,n,r){let i=t;if(n&&i.startsWith(n+`.`)){i=i.slice(n.length+1),at(e,i.split(`.`),r);return}at(e,i.split(`.`),r)}function at(e,t,n){let r=e;for(let e=0;e<t.length-1;e++){let n=t[e];(typeof r[n]!=`object`||r[n]===null)&&(r[n]={}),r=r[n]}r[t[t.length-1]]=n}function P(e){let t=[];for(let n of e){let e=ot[n.t]??v.Unknown;(t[e]??=[]).push(n)}return t}const ot=Object.fromEntries(Object.entries({unknown:v.Unknown,int:v.Int,float:v.Float,string:v.String,bool:v.Bool,bytes:v.Bytes,geometry:v.Geometry,record:v.Record,array_unknown:v.ArrayUnknown,array_int:v.ArrayInt,array_float:v.ArrayFloat,array_string:v.ArrayString,array_bool:v.ArrayBool,array_bytes:v.ArrayBytes,array_object:v.ArrayObject,array_geometry:v.ArrayGeometry}));function F(e){if(e<0||e>1023)throw Error(`anansi: schema version ${e} exceeds maximum of 1023`);return{flags:e>>8<<4,version:e&255}}function I(e,t){return(e>>4&3)<<8|t}function st(e,t,n=0,r=`auto`){let i=F(n),a=0;for(let n of e)O(t,n)!==`notSet`&&a++;let o=e.length?a/e.length:1,s=r===`sparse`||r===`auto`&&!(e.length<=64||o>.25),c=i.flags|(s?E.Sparse:E.Dense),l=[c,i.version],u={flags:c,version:i.version},d=[];return s?j(d,e,t,u):k(d,e,t,u),new Uint8Array([...l,...d])}function ct(e,t){let n=new _(e),r=n.byte(),i=n.byte();if((r>>4&3)===E.Stream)throw Error(`anansi: stream packets unsupported`);if(r&196)throw Error(`anansi: compressed/encrypted/hashed packets require transform support not enabled in this build`);let a=r&3,o={};if(a===E.Dense)A(n,t,o);else if(a===E.Sparse)M(n,t,o);else throw Error(`anansi: use batch API for packet type ${a}`);return{version:I(r,i),doc:o}}function lt(e,t,n=0){let r=F(n),i=0,a=0;for(let n of t){for(let t of e)O(n,t)!==`notSet`&&i++;a+=e.length}let o=a?i/a:1,s=e.length>64&&o<=.25,c=[r.flags|E.Batch,r.version],l=[];g(l,t.length),l.push(s?2:0);let u={flags:r.flags|E.Batch,version:r.version};for(let n of t)s?j(l,e,n,u):k(l,e,n,u);return new Uint8Array([...c,...l])}function ut(e,t,n=0){let r=F(n),i=[r.flags|E.Batch|8,r.version],a=dt(e,t,{flags:i[0],version:r.version});return new Uint8Array([...i,...a])}function dt(e,t,n){let r=[];g(r,t.length),r.push(1);let i=P(e);for(let e=0;e<=15;e++){let a=i[e];if(!a||a.length===0)continue;let o=2*a.length*t.length,s=new Uint8Array(o+7>>3),c=0;for(let e of a)for(let n of t){let t=O(n,e),r=t===`notSet`?0:t===`null`?1:2;s[c>>3]|=r<<(c&7),c+=2}for(let e of s)r.push(e);if(e===v.Bool){for(let e of a){let n=[];for(let r of t)O(r,e)===`value`&&n.push(D(r,e.path)[1]);C(r,n)}continue}for(let e of a)for(let i of t)if(O(i,e)===`value`){if(e.t===`array_object`){nt(r,e.child,e.name,D(i,e.path)[1]??[],n);continue}et(r,e,D(i,e.path)[1])}}return r}function ft(e,t){let n=new _(e),r=n.byte(),i=n.byte();if((r&3)!==E.Batch)throw Error(`anansi: not a batch packet`);if(r&196)throw Error(`anansi: transformed batch packets unsupported in this build`);let a=n.uvarint(),o=n.byte(),s=Array.from({length:a},()=>({}));if(o&1||r&8)pt(n,t,s);else{let e=!!(o&2);for(let r of s)e?M(n,t,r):A(n,t,r)}return{version:I(r,i),docs:s}}function pt(e,t,n){let r=P(t);for(let t=0;t<=15;t++){let i=r[t];if(!i||i.length===0)continue;let a=2*i.length*n.length,o=e.take(a+7>>3),s=[],c=0;for(let e=0;e<i.length;e++){let e=[];for(let t=0;t<n.length;t++)e.push(o[c>>3]>>(c&7)&3),c+=2;s.push(e)}if(t===v.Bool){i.forEach((t,r)=>{let i=s[r].filter(e=>e===2).length,a=w(e,i),o=0;s[r].forEach((e,r)=>{if(e!==0){if(e===1){N(n[r],t.path,void 0,null);return}N(n[r],t.path,void 0,a[o++])}})});continue}i.forEach((t,r)=>{s[r].forEach((r,i)=>{if(r!==0){if(r===1){N(n[i],t.path,void 0,null);return}if(t.t===`array_object`){let r=[];rt(e,t.child,t.name,r),N(n[i],t.path,void 0,r);return}N(n[i],t.path,void 0,tt(e,t))}})})}}const mt=e=>e&3;let ht,gt=!1;function _t(){if(!gt){gt=!0;try{ht=globalThis.process?.getBuiltinModule?.(`node:zlib`)??void 0}catch{ht=void 0}}return ht}function vt(e){let t=_t()?.zstdCompressSync?.(e);if(!t)throw Error(`anansi: zstd compression is unavailable in this runtime (no node:zlib). Send plain packets, or provide a WASM compressor.`);return t}function yt(e,n){return _t()?.zstdDecompressSync?.(e)||t(e)}async function bt(t){let n=await e(t,128),r=new Uint8Array(16);for(let e=0;e<16;e++)r[e]=parseInt(n.slice(e*2,e*2+2),16);return r}async function xt(e){if(e.length!==32)throw Error(`anansi: AES-256-GCM requires a 32-byte key, got ${e.length}`);return crypto.subtle.importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`,`decrypt`])}async function St(e,t,n,r){let i=e;r.integrity&&(i|=128),r.compression&&(i|=4),r.encryptionKey&&(i|=64);let a=[i,t];if(r.integrity)for(let e of await bt(n))a.push(e);let o=[];if(r.compression){g(o,n.length);for(let e of vt(n))o.push(e)}else for(let e of n)o.push(e);if(!r.encryptionKey)return new Uint8Array([...a,...o]);let s=await xt(r.encryptionKey),c=crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await crypto.subtle.encrypt({name:`AES-GCM`,iv:c},s,new Uint8Array(o))),u=new Uint8Array(a.length+12+l.length);return u.set(Uint8Array.from(a),0),u.set(c,a.length),u.set(l,a.length+12),u}async function Ct(e,t,n){let r;t&128&&(r=e.take(16));let i=e.data.subarray(e.pos);if(t&64){if(!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);if(i.length<12)throw Error(`anansi: truncated nonce`);let e=i.subarray(0,12);i=i.subarray(12);let t=await xt(n.decryptionKey),r=await crypto.subtle.decrypt({name:`AES-GCM`,iv:e},t,i);i=new Uint8Array(r)}let a=i.length,o=new _(i);if(t&4){if(a=o.uvarint(),a>536870912)throw Error(`anansi: declared uncompressed size ${a} exceeds limit`);let e=yt(i.subarray(o.pos),a);if(e.length!==a)throw Error(`anansi: decompressed ${e.length} bytes, header declared ${a}`);i=e}if(r){let e=await bt(i);for(let t=0;t<16;t++)if(r[t]!==e[t])throw Error(`anansi: integrity check failed (packet digest mismatch)`)}return new _(i)}function wt(e,t){let n=0;for(let r of e){let[e,i]=Tt(t,r.path);e&&i!=null&&n++}return e.length<=64||e.length===0||n/e.length>.25?`dense`:`sparse`}function Tt(e,t){let n=e;for(let e of t.split(`.`)){if(typeof n!=`object`||!n)return[!1,void 0];let t=n;if(!(e in t))return[!1,void 0];n=t[e]}return[n!==void 0,n]}function Et(e,t,n){let r=n===`sparse`||n===`auto`&&wt(e,t)===`sparse`,i=[];return r?j(i,e,t):k(i,e,t),new Uint8Array(i)}async function Dt(e,t,n=0,r={}){let i=F(n),a=r.kind??`auto`,o=a===`sparse`||a===`auto`&&wt(e,t)===`sparse`,s=i.flags|(o?E.Sparse:E.Dense),c=Et(e,t,o?`sparse`:`dense`);return St(s,i.version,c,r)}async function Ot(e,t,n={}){let r=new _(e),i=r.byte(),a=r.byte();if(i&64&&!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);let o=await Ct(r,i,n),s={},c=mt(i);if(c===E.Dense)A(o,t,s);else if(c===E.Sparse)M(o,t,s);else throw Error(`anansi: use batch API for packet type ${c}`);return{version:I(i,a),doc:s}}function kt(e,t){let n=0,r=0;for(let i of t)for(let t of e){let[e,a]=Tt(i,t.path);e&&a!=null&&n++,r++}let i=r>0?n/r:1,a=e.length>64&&i<=.25,o=[];g(o,t.length),o.push(a?2:0);for(let n of t)a?j(o,e,n):k(o,e,n);return{body:new Uint8Array(o),sparse:a}}async function At(e,t,n=0,r={}){let i=F(n),{body:a,sparse:o}=kt(e,t);return St(i.flags|E.Batch|(o?2:0),i.version,a,r)}async function jt(e,t,n=0,r={}){let i=F(n),a=i.flags|E.Batch|8,o=new Uint8Array(dt(e,t,{flags:a,version:i.version}));return St(a,i.version,o,r)}async function Mt(e,t,n={}){let r=new _(e),i=r.byte(),a=r.byte();if(mt(i)!==E.Batch)throw Error(`anansi: not a batch packet`);let o=await Ct(r,i,n),s=o.uvarint(),c=o.byte(),l=Array.from({length:s},()=>({}));if(c&1||i&8)pt(o,t,l);else for(let e of l)c&2?M(o,t,e):A(o,t,e);return{version:I(i,a),docs:l}}var Nt=class e{linked;fields;fullVersion;kind;enc;dec;constructor(e,t,n,r,i,a){this.linked=e,this.fields=t,this.fullVersion=n,this.kind=r,this.enc=i,this.dec=a}static async create(t,n={}){let i=de(r(t));return new e(i,me(i),n.fullVersion??0,n.kind??`auto`,{compression:n.compression,integrity:n.integrity,encryptionKey:n.encryptionKey},{decryptionKey:n.decryptionKey})}async encode(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Dt(this.fields,e,this.fullVersion,{...this.enc,kind:this.kind}):st(this.fields,e,this.fullVersion,this.kind)}async decode(e){return Ot(e,this.fields,this.dec)}async encodeBatch(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?At(this.fields,e,this.fullVersion,this.enc):lt(this.fields,e,this.fullVersion)}async decodeBatch(e){return Mt(e,this.fields,this.dec)}async encodeColumnar(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?jt(this.fields,e,this.fullVersion,this.enc):jt(this.fields,e,this.fullVersion,{})}};function Pt(e,t){let n=[];switch(e.type){case`modifyProperty`:n.push({op:`replace`,path:`/${e.id}`,value:e.value});break;case`addField`:n.push({op:`add`,path:`/fields/${e.id}`,value:e.definition});break;case`removeField`:n.push({op:`remove`,path:`/fields/${e.id}`});break;case`modifyField`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/fields/${e.id}/${t}`,value:r});break;case`addIndex`:t.indexes||n.push({op:`add`,path:`/indexes`,value:{}}),n.push({op:`add`,path:`/indexes/${e.id}`,value:e.definition});break;case`removeIndex`:n.push({op:`remove`,path:`/indexes/${e.id}`});break;case`modifyIndex`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/indexes/${e.id}/${t}`,value:r});break;case`addConstraint`:t.constraints||n.push({op:`add`,path:`/constraints`,value:{}}),n.push({op:`add`,path:`/constraints/${e.id}`,value:e.constraint});break;case`removeConstraint`:n.push({op:`remove`,path:`/constraints/${e.id}`});break;case`modifyConstraint`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/constraints/${e.id}/${t}`,value:r});break;case`addSchema`:t.schemas||n.push({op:`add`,path:`/schemas`,value:{}}),n.push({op:`add`,path:`/schemas/${e.id}`,value:e.definition});break;case`removeSchema`:n.push({op:`remove`,path:`/schemas/${e.id}`});break;case`modifySchema`:for(let r of e.changes)n.push(...Pt(r,t))}return n}function Ft(e,t){switch(e.type){case`removeField`:case`removeIndex`:case`removeConstraint`:return`major`;case`modifyField`:{let t=e.changes;return t.required===!0||t.type!==void 0||t.schema!==void 0||t.unique===!0?`major`:t.deprecated?`minor`:`patch`}case`modifyIndex`:{let t=e.changes;return t.unique!==void 0||t.fields!==void 0?`major`:`minor`}case`modifyConstraint`:{let n=t?.constraints?.[e.id];return n&&`predicate`in e.changes&&e.changes.predicate!==void 0||n&&`parameters`in e.changes&&e.changes.parameters!==void 0?`major`:`minor`}case`addField`:case`addIndex`:case`addConstraint`:case`addSchema`:case`deprecateField`:return`minor`;case`modifySchema`:{let n=`patch`;for(let r of e.changes){let e=Ft(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}default:return`patch`}}function It(e,t){let n=`patch`;for(let r of e){let e=Ft(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}function Lt(e,t){let n=e.replace(/^v/,``).match(/^(\d+)\.(\d+)\.(\d+)/);if(!n)throw Error(`anansi: invalid version format "${e}"`);let[,r,i,a]=n.map(Number);switch(t){case`major`:r++,i=0,a=0;break;case`minor`:i++,a=0;break;case`patch`:a++}return`${r}.${i}.${a}`}function Rt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function zt(e,t){let n={...e};for(let e of Object.keys(t)){let r=t[e];Rt(r)&&Rt(n[e])?n[e]=zt(n[e],r):r!==void 0&&(n[e]=r)}return n}async function Bt(e){if(typeof globalThis.crypto?.subtle?.digest==`function`){let t=new TextEncoder().encode(e),n=await globalThis.crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}let{createHash:t}=await import(`node:crypto`);return t(`sha256`).update(e).digest(`hex`)}let Vt=function(e){return e.INVALID_SCHEMA=`INVALID_SCHEMA`,e.INVALID_MIGRATION=`INVALID_MIGRATION`,e.CHECKSUM_MISMATCH=`CHECKSUM_MISMATCH`,e.TIMEOUT=`TIMEOUT`,e.MEMORY_LIMIT=`MEMORY_LIMIT`,e.CONCURRENT_OPERATION=`CONCURRENT_OPERATION`,e.TRANSFORM_ERROR=`TRANSFORM_ERROR`,e.VERSION_NOT_FOUND=`VERSION_NOT_FOUND`,e.CIRCULAR_DEPENDENCY=`CIRCULAR_DEPENDENCY`,e.STREAM_ERROR=`STREAM_ERROR`,e.ROLLBACK_ERROR=`ROLLBACK_ERROR`,e.MISSING_TRANSFORM=`MISSING_TRANSFORM`,e}({});var L=class extends Error{code;migrationId;cause;constructor(e,t,n,r){super(e),this.code=t,this.migrationId=n,this.cause=r,this.name=`MigrationError`}};function Ht(e){return e===``||e===`/`?[]:e.slice(1).split(`/`).map(e=>e.replace(/~1/g,`/`).replace(/~0/g,`~`))}function R(e,t){let n=e;for(let e=0;e<t.length-1;e++){let r=t[e];if(typeof n!=`object`||!n)throw Error(`anansi/engine: path "${r}" not found`);n=Array.isArray(n)?n[parseInt(r,10)]:n[r]}return{parent:n,key:t[t.length-1]}}function Ut(e,t){let n=JSON.parse(JSON.stringify(e));for(let e of t){let t=Ht(e.path);switch(e.op){case`add`:{if(t.length===0){n=e.value;break}let{parent:r,key:i}=R(n,t);if(Array.isArray(r)){let t=i===`-`?r.length:parseInt(i,10);r.splice(t,0,e.value)}else r[i]=e.value;break}case`remove`:{let{parent:e,key:r}=R(n,t);Array.isArray(e)?e.splice(parseInt(r,10),1):delete e[r];break}case`replace`:{let{parent:r,key:i}=R(n,t);r[i]=e.value;break}case`move`:{let t=Ht(e.from),r=R(n,t),i=Array.isArray(r.parent)?r.parent.splice(parseInt(r.key,10),1)[0]:(()=>{let e=r.parent[r.key];return delete r.parent[r.key],e})(),a=Ht(e.path),o=R(n,a);Array.isArray(o.parent)?o.parent.splice(parseInt(o.key,10),0,i):o.parent[o.key]=i;break}case`test`:{let{parent:r,key:i}=R(n,t);if(JSON.stringify(r[i])!==JSON.stringify(e.value))throw Error(`anansi/engine: test failed at ${e.path}`);break}}}return n}async function Wt(e){return Bt(JSON.stringify({id:e.id,schemaVersion:e.schemaVersion,changes:e.changes,description:e.description,rollback:e.rollback,createdAt:e.createdAt}))}async function Gt(e,t){return e.transform?typeof e.transform==`string`?e.transform.startsWith(`http://`)||e.transform.startsWith(`https://`)?Kt(e.transform,t):qt(e.transform,t):e.transform[t]:null}async function Kt(e,t){try{let n=await fetch(e);if(!n.ok)throw new L(`Failed to fetch transform module: ${e}`,`TRANSFORM_ERROR`);let r=await n.text();if(typeof window<`u`){let e=new Blob([r],{type:`application/javascript`});return(await import(URL.createObjectURL(e))).default[t]}{let{runInNewContext:n}=await import(`node:vm`),i={module:{exports:{}},console};return n(r,i,e),i.module.exports[t]}}catch(t){throw t instanceof L?t:new L(`Failed to load remote transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}async function qt(e,t){try{return(await import(e)).default[t]}catch(t){throw t instanceof L?t:new L(`Failed to import local transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}var Jt=class e{currentSchema;history=[];migrations=[];isProcessing=!1;constructor(e,t,n){this.currentSchema=e,t&&(this.migrations=t.sort((e,t)=>e.schemaVersion.localeCompare(t.schemaVersion))),n&&(this.history=n.sort((e,t)=>e.version.localeCompare(t.version)))}data(){return{schema:this.currentSchema,history:[...this.history],migrations:[...this.migrations]}}async add(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);if(!e.changes?.length)throw new L(`Migration must include changes`,`INVALID_MIGRATION`);let t={id:Date.now().toString(),schemaVersion:this.currentSchema.version,changes:e.changes,description:e.description,status:`pending`,rollback:e.rollback,transform:e.transform,createdAt:new Date().toISOString(),checksum:``};t.checksum=await Wt(t),this.migrations.push(t)}async dryRun(t,n,r){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{this.isProcessing=!0;let i={...this.currentSchema},a=this.getRelevantMigrations(n,r),o;if(n===`forward`)o=a.reduce((e,t)=>this.applySchemaChanges(e,t.changes,t.id),i);else{let e=[...this.history];for(let t=0;t<a.length;t++){let t=e.pop();t&&(i={...t})}o=i}let s=await e.processMigrationList(t,n,a);return{newSchema:o,dataPreview:s}}catch(e){throw e instanceof L?e:new L(`Dry run failed`,`INVALID_SCHEMA`,void 0,e)}finally{this.isProcessing=!1}}async prepareMigration(){let e=this.migrations.filter(e=>e.status===`pending`);return await this.validateMigrations(e),e}async migrate(t){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);let n=await this.prepareMigration();try{this.isProcessing=!0,this.transformSchema(`forward`);let r=await e.processMigrationList(t,`forward`,n);return this.markMigrationsApplied(n),r}finally{this.isProcessing=!1}}async rollback(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);return this.migrations.filter(e=>e.status===`applied`).slice(-1)[0]?this.rollbackToVersion(this.history[this.history.length-1]?.version||this.currentSchema.version,e):e}async rollbackToVersion(t,n){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{let r=this.history.findIndex(e=>e.version===t);if(r===-1)throw new L(`Version ${t} not found in history`,`VERSION_NOT_FOUND`);let i=this.migrations.filter(e=>e.schemaVersion===t&&e.status===`applied`).sort((e,t)=>t.id.localeCompare(e.id)),a=this.history.length-r;if(a<0)return n;for(let e=0;e<a;e++)this.transformSchema(`backward`);let o=await e.processMigrationList(n,`backward`,i);return this.migrations=this.migrations.map(e=>e.schemaVersion>=t&&e.status===`applied`?{...e,status:`rolled_back`}:e),o}finally{this.isProcessing=!1}}static async processMigrationList(e,t,n){return(await Promise.all(n.map(async e=>{try{return{migration:e,transform:await Gt(e,t)}}catch(t){throw new L(`Failed to resolve transform for migration ${e.id}`,`TRANSFORM_ERROR`,e.id,t)}}))).filter(e=>!!e.transform).reduce((e,{migration:t,transform:n})=>e.pipeThrough(new TransformStream({async transform(e,r){try{let t=await n(e);r.enqueue(t)}catch(e){r.error(new L(`Data transformation failed for migration ${t.id}`,`TRANSFORM_ERROR`,t.id,e))}}})),e)}getRelevantMigrations(e,t){return[...this.migrations].filter(n=>{let r=e===`forward`?`pending`:`applied`,i=!t||n.schemaVersion.localeCompare(t)>=0;return n.status===r&&i}).sort((t,n)=>e===`forward`?t.id.localeCompare(n.id):n.id.localeCompare(t.id))}applySchemaChanges(e,t,n){let r=It(t,e),i=Lt(e.version,r);return t.map(t=>{try{return Pt(t,e)}catch(e){throw new L(`Invalid schema change`,`INVALID_SCHEMA`,n,e)}}).reduce((e,t)=>{try{return Ut(e,t)}catch(e){throw new L(`Failed to apply patch`,`INVALID_SCHEMA`,n,e)}},{...e,version:i})}async validateMigrations(e){await Promise.all(e.map(async e=>{let t=await Wt(e);if(e.checksum!==t)throw new L(`Checksum mismatch`,`CHECKSUM_MISMATCH`,e.id)}))}markMigrationsApplied(e){this.migrations=this.migrations.map(t=>e.some(e=>e.id===t.id)?{...t,status:`applied`}:t)}transformSchema(e){try{if(e===`backward`){let e=this.history.pop();if(!e)throw Error(`No previous version`);this.currentSchema=e;return}let t=this.migrations.filter(e=>e.status===`pending`).flatMap(e=>e.changes);if(!t.length)return;this.history.push(structuredClone(this.currentSchema)),this.currentSchema=t.reduce((e,t)=>this.applySchemaChanges(e,[t]),this.currentSchema)}catch(e){throw e instanceof L?e:new L(`Schema transformation failed`,`INVALID_SCHEMA`,void 0,e)}}};function Yt(){return{maxDepth:20,mode:`strict`}}var z=class e{raw;constructor(e){this.raw=e}static zero(){return new e({kind:`zero`})}static fromSingle(t){return new e({kind:`single`,value:t})}static fromMultiple(t){return new e({kind:`multiple`,value:t})}static fromInline(t){return new e({kind:`inline`,value:t})}static fromFieldSchema(t){if(t==null)return e.zero();if(Array.isArray(t))return e.fromMultiple(t);let n=t;return`id`in n&&typeof n.id==`string`?e.fromSingle(t):`type`in n?e.fromInline(t):e.zero()}isZero(){return this.raw.kind===`zero`}isSingle(){return this.raw.kind===`single`}isMultiple(){return this.raw.kind===`multiple`}isInline(){return this.raw.kind===`inline`}asSingle(){if(this.raw.kind!==`single`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'single'`);return this.raw.value}asMultiple(){if(this.raw.kind!==`multiple`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'multiple'`);return this.raw.value}asInline(){if(this.raw.kind!==`inline`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'inline'`);return this.raw.value}};function Xt(e){let t=e;if(`predicate`in t&&typeof t.predicate==`string`){let t=e;return{kind:`rule`,name:t.name,description:t.description??void 0,predicate:t.predicate,parameters:t.parameters??void 0,fields:t.fields??[]}}let n=e;return{kind:`group`,name:n.name,description:n.description??void 0,operator:n.operator,rules:(n.rules??[]).map(e=>Xt(e))}}function B(e){if(!e)return{};let t={};for(let[n,r]of Object.entries(e))t[n]=Xt(r);return t}const V={issues:[],success:!0,skipped:!1},Zt={issues:[],success:!0,skipped:!0};function H(e){return{issues:e,success:!1,skipped:!1}}function U(e,t,n){return{code:e,message:t,path:n}}function W(e,t){return e===``?t:`${e}.${t}`}function Qt(e,t,n){return e===``?[n,[n]]:[`${e}.${n}`,[...t,n]]}function $t(e){return e===``?[]:e.split(`.`)}function G(e){let t=e.length;for(let n of e)for(let e of n)e===`[`&&t++;return t}function en(e,t,n){let r=[],i=[];for(let a of n){let n=$t(a);e===``?(r.push(a),i.push(n)):(r.push(W(e,a)),i.push([...t,...n]))}return[r,i]}function tn(e,t){if(t.length===0)return[e,!0];let n=e;for(let e of t){if(n==null)return[void 0,!1];let t=e.indexOf(`[`);if(t===-1){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let t=n;if(!(e in t))return[void 0,!1];n=t[e]}else{let r=e.slice(0,t);if(r!==``){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let e=n;if(!(r in e))return[void 0,!1];n=e[r]}let i=e.slice(t).matchAll(/\[(\d+)\]/g);for(let e of i){let t=e[1];if(t===void 0)return[void 0,!1];let r=parseInt(t,10);if(!Array.isArray(n)||r>=n.length)return[void 0,!1];n=n[r]}}}return[n,!0]}function K(e,t){return tn(e.rootData,t)}function q(e){return typeof e==`object`&&e&&!Array.isArray(e)?e:null}function nn(e){if(e==null)return!0;let t=typeof e;return t===`string`||t===`number`||t===`boolean`||t===`bigint`||t===`symbol`}function rn(e,t,n=!1){if(e===t)return!0;if(e===null||t===null||e===void 0||t===void 0)return e===t;let r=typeof e,i=typeof t;if(r===`string`&&i===`string`||r===`boolean`&&i===`boolean`||r===`number`&&i===`number`||n&&r===`number`&&i===`number`)return e===t;if(r!==`object`||i!==`object`)return!1;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!rn(e[r],t[r],n))return!1;return!0}if(Array.isArray(e)!==Array.isArray(t))return!1;let a=e,o=t,s=Object.keys(a),c=Object.keys(o);if(s.length!==c.length)return!1;for(let e of s)if(!(e in o)||!rn(a[e],o[e],n))return!1;return!0}function an(e){return typeof e==`number`&&Number.isInteger(e)}function on(e){return typeof e==`number`&&!Number.isInteger(e)}function J(e){return typeof e==`number`}var Y=class{id;path;pathParts;deps;constructor(e,t,n,r=[]){this.id=e,this.path=t,this.pathParts=n,this.deps=r}getID(){return this.id}getPath(){return this.path}getPathParts(){return this.pathParts}getDependencies(){return this.deps}},sn=class extends Y{expectedFields;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expectedFields=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n)return V;let r=q(t);if(!r)return H([U(`TYPE_MISMATCH`,`Expected object for unexpected field check`,this.path)]);let i=[];for(let e of Object.keys(r))this.expectedFields.has(e)||i.push(U(`UNEXPECTED_FIELD`,`Unexpected field '${e}'`,W(this.path,e)));return i.length>0?H(i):V}},cn=class extends Y{fieldName;parentPath;parentPathParts;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.fieldName=r,this.parentPath=i,this.parentPathParts=a}async execute(e){let[t,n]=K(e,this.parentPathParts);if(!n&&this.parentPath!==``)return V;if(t==null)return Zt;let r=q(t);return r?this.fieldName in r?V:H([U(`REQUIRED_FIELD_MISSING`,`Required field '${this.fieldName}' is missing`,this.path)]):H([U(`INVALID_DATA_STRUCTURE`,`Cannot check for required fields on non-object parent`,this.parentPath)])}},ln=class extends Y{expected;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expected=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;switch(this.expected){case`string`:if(typeof t==`string`)return V;break;case`boolean`:if(typeof t==`boolean`)return V;break;case`number`:if(J(t))return V;break;case`integer`:if(an(t))return V;break;case`decimal`:if(on(t))return V;break;case`bytes`:if(t instanceof Uint8Array||Array.isArray(t)&&t.every(e=>typeof e==`number`))return V;break;case`record`:if(q(t)!==null)return V;break;case`unknown`:return V;default:return V}return H([U(`TYPE_MISMATCH`,`Expected ${this.expected}, got ${typeof t} (${JSON.stringify(t)})`,this.path)])}},un=class extends Y{lookup;complex;expectNumeric;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.lookup=r,this.complex=i,this.expectNumeric=a}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(typeof t!=`string`&&typeof t!=`number`&&typeof t!=`boolean`)return H([U(`TYPE_MISMATCH`,`Expected scalar for enum, got ${typeof t}`,this.path)]);if(this.lookup.has(t))return V;for(let e of this.complex)if(rn(t,e,this.expectNumeric))return V;return H([U(`ENUM_VIOLATION`,`Value ${JSON.stringify(t)} is not in the allowed list`,this.path)])}},dn=class extends Y{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return H([U(`ARRAY_TYPE_MISMATCH`,`Expected array`,this.path)]);let r=G(this.pathParts);if(r>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let i=[],a=e.maxDepth-r;for(let n=0;n<t.length;n++){let r=t[n],o=`${this.path}[${n}]`,s=await this.graph.traverse(e.functionMap,{item:r},e.mode,a,e.originalRoot);for(let e of s)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,o):e.path})}return i.length>0?H(i):V}},fn=class extends Y{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;let r=q(t);if(!r)return H([U(`OBJECT_TYPE_MISMATCH`,`Expected object for record, got ${typeof t}`,this.path)]);let i=G(this.pathParts);if(i>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let a=[],o=e.maxDepth-i;for(let[t,n]of Object.entries(r)){let r=W(this.path,t),i=await this.graph.traverse(e.functionMap,{item:n},e.mode,o,e.originalRoot);for(let e of i)a.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,r):e.path})}return a.length>0?H(a):V}},pn=class extends Y{itemGraph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.itemGraph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return H([U(`SET_TYPE_MISMATCH`,`Expected array for set validation`,this.path)]);if(this.itemGraph){let n=G(this.pathParts);if(n>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=e.maxDepth-n,i=[];for(let n=0;n<t.length;n++){let a=`${this.path}[${n}]`,o=await this.itemGraph.traverse(e.functionMap,{item:t[n]},e.mode,r,e.originalRoot);for(let e of o)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,a):e.path})}if(i.length>0)return H(i)}if(t.length<=1)return V;let r=new Map,i=[];for(let e=0;e<t.length;e++){let n=t[e];if(nn(n)){let t=r.get(n);if(t!==void 0)return H([U(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t})`,this.path)]);r.set(n,e)}else{let t=i.find(e=>rn(n,e.val,!1));if(t)return H([U(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t.index})`,this.path)]);i.push({val:n,index:e})}}return V}},mn=class extends Y{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){let[t,n]=K(e,this.pathParts);if(!n)return V;if(!Array.isArray(t))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry must be an array of coordinate arrays`,this.path)]);for(let e=0;e<t.length;e++){let n=t[e];if(!Array.isArray(n))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry inner element at index ${e} must be an array`,`${this.path}[${e}]`)]);for(let t=0;t<n.length;t++)if(!J(n[t]))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry element at [${e}][${t}] is not a valid number`,`${this.path}[${e}][${t}]`)])}return V}},hn=class extends Y{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){return V}},gn=class extends Y{validationGraph;schemaName;constructor(e,t,n,r,i,a=[]){super(e,t,n,a),this.validationGraph=r,this.schemaName=i}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;let r=q(t);if(!r)return H([U(`TYPE_MISMATCH`,`Expected object for recursive schema ${this.schemaName}, got ${typeof t}`,this.path)]);if(G(this.pathParts)>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Recursive schema '${this.schemaName}' exceeds maximum depth of ${e.maxDepth}`,this.path)]);let i=(await this.validationGraph.traverse(e.functionMap,r,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path===``?this.path:W(this.path,e.path)}));return i.length>0?H(i):V}},_n=class extends Y{graphs;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graphs=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(G(this.pathParts)>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=await Promise.all(this.graphs.map(async(n,r)=>{let i={root:t},a=(await n.traverse(e.functionMap,i,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path.startsWith(`root`)?e.path.replace(`root`,this.path):e.path,message:`[variant ${r}] ${e.message}`})),o=a.length===0;return!o&&a.length===0&&a.push(U(`INTERNAL_ERROR`,`Variant ${r} failed without reporting any issues`,this.path)),{ok:o,issues:a}}));return r.some(e=>e.ok)?V:H([U(`UNION_MISMATCH`,`Value at '${this.path}' did not match any variant of the union`,this.path),...r.flatMap(e=>e.issues)])}},vn=class extends Y{constraint;fieldPaths;fieldPathParts;constraintPath;constraintPathParts;scope;constructor(e,t,n,r,i,a,o,s,c,l=[]){super(e,t,n,l),this.constraint=r,this.fieldPaths=i,this.fieldPathParts=a,this.constraintPath=o,this.constraintPathParts=s,this.scope=c}async execute(e){if(this.constraint.kind!==`rule`)return H([U(`INTERNAL_ERROR`,`ConstraintNode '${this.constraint.name}': expected a rule, got a group`,this.constraintPath)]);switch(this.scope){case`global`:return this.executeGlobalConstraint(e,this.constraint);case`recursive`:return this.executeRecursiveConstraint(e,this.constraint)}}executeGlobalConstraint(e,t){return Sn(e,t.name,this.constraintPath,this.fieldPaths,this.fieldPathParts,()=>Cn({...e,data:e.rootData},t,this.constraintPath))}executeRecursiveConstraint(e,t){let[n,r]=K(e,this.constraintPathParts);if(!r)return Promise.resolve(Zt);let i=this.fieldPaths.map(e=>this.constraintPath!==``&&e.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPath.length+1):e),a=this.fieldPathParts.map((e,t)=>{let n=this.fieldPaths[t]??``;return this.constraintPath!==``&&n.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPathParts.length):e});return Sn(e,t.name,this.constraintPath,i,a,()=>Cn({...e,data:n},t,this.constraintPath))}},yn=class extends Y{group;name;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.group=r,this.name=i}async execute(e){let{allPaths:t,allParts:n}=xn(this.group,this.path,this.pathParts);return Sn(e,this.name,this.path,t,n,()=>this.executeGroup(e,this.group,this.path,this.name))}async executeGroup(e,t,n,r){let i=[],a=[];for(let o of t.rules){let t;if(o.kind===`rule`)t=await Cn(e,o,n);else if(o.kind===`group`)t=await this.executeGroup(e,o,n,o.name);else return H([U(`INTERNAL_ERROR`,`Unknown constraint kind in group '${r}'`,n)]);i.push(t.success),t.success||a.push(...t.issues)}return bn(t.operator,i)?V:H([U(`CONSTRAINT_GROUP_VIOLATION`,`Constraint group '${r}' failed`,n),...a])}};function bn(e,t){switch(e){case`and`:return t.every(Boolean);case`or`:return t.some(Boolean);case`not`:return t.length===1&&!t[0];case`nor`:return t.every(e=>!e);case`xor`:return t.filter(Boolean).length===1;case`nand`:return!t.every(Boolean);case`xnor`:return t.filter(Boolean).length!==1;default:return!1}}function xn(e,t,n){let r=new Map;function i(e){for(let a of e.rules)if(a.kind===`rule`){let[e,i]=en(t,n,a.fields);for(let t=0;t<e.length;t++){let n=e[t],a=i[t];r.set(n,a)}}else a.kind===`group`&&i(a)}i(e);let a=Array.from(r.keys());return{allPaths:a,allParts:a.map(e=>r.get(e))}}async function Sn(e,t,n,r,i,a){let o=[],s=[];for(let t=0;t<r.length;t++){let n=i[t]??[],a=r[t]??``,[,c]=K(e,n);c?o.push(a):s.push(a)}if(s.length===0)return a();if(o.length===0)switch(e.mode){case`strict`:return H([U(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:case`loose`:return Zt}switch(e.mode){case`strict`:return H([U(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:return H([U(`CONSTRAINT_PARTIAL_UPDATE`,`Constraint '${t}' couples fields ${JSON.stringify(r)}. Cannot update only ${JSON.stringify(o)} - all coupled fields must be updated together`,n)]);case`loose`:return Zt}}async function Cn(e,t,n){let r=e.functionMap[t.predicate];if(!r)return H([U(`MISSING_PREDICATE`,`Predicate '${t.predicate}' not found`,n)]);let i=await r({root:e.originalRoot,data:e.data,keys:t.fields,parameters:t.parameters});return i.length>0?H(i.map(e=>({...e,path:e.path===``?n:W(n,e.path)}))):V}var wn=class{constraints=new Map;add(e,t,n,r,i){let a=this.constraints.get(e);(!a||n>=a.specificity)&&this.constraints.set(e,{constraint:t,specificity:n,basePath:r,scope:i})}getEffective(){return Array.from(this.constraints.values())}};function Tn(){return new wn}var En=class{buildingSchemas=new Map;recursiveGraphCache=new Map;isRecursive(e){return(this.buildingSchemas.get(e)??0)>0}markBuilding(e){this.buildingSchemas.set(e,(this.buildingSchemas.get(e)??0)+1)}unmarkBuilding(e){let t=(this.buildingSchemas.get(e)??0)-1;t<=0?this.buildingSchemas.delete(e):this.buildingSchemas.set(e,t)}makeGraphCacheKey(e,t){let n=Object.keys(t).sort();return n.length===0?e:`${e}:${n.join(`,`)}`}async getOrBuildRecursiveGraph(e,t,n,r){let i=this.makeGraphCacheKey(e,n),a=this.recursiveGraphCache.get(i);if(a)return a;let o=new Pn;this.recursiveGraphCache.set(i,o),this.markBuilding(e);try{let e=Dn(t,r);await o.buildFromSchema(e,``,[],new Map,t,n,r,this,!1,!1),o.finalize()}catch(e){throw this.recursiveGraphCache.delete(i),e}finally{this.unmarkBuilding(e)}return o}};function Dn(e,t){let n=`fields`in e?e.fields:{},r=e.constraints??{},i={name:e.name,version:`0`,fields:n,constraints:r};return e.description!==void 0&&(i.description=e.description),t.schemas!==void 0&&(i.schemas=t.schemas),i}function X(e){return`type`in e&&e.type?e.type:`fields`in e&&e.fields&&Object.keys(e.fields).length>0?`object`:`unknown`}function On(e){switch(e){case`object`:case`array`:case`set`:case`record`:case`union`:case`composite`:case`enum`:case`geometry`:return!0;default:return!1}}function kn(e){let t=[];for(let n of e.rules)n.kind===`rule`?t.push(...n.fields):n.kind===`group`&&t.push(...kn(n));return t}function An(e,t){let n={},r=e.constraints??{},i=e=>e===t||e.startsWith(t+`.`);for(let[e,t]of Object.entries(r)){let r=Xt(t);r.kind===`rule`?r.fields.some(i)&&(n[e]=r):r.kind===`group`&&kn(r).some(i)&&(n[e]=r)}return n}function jn(e,t,n,r){let i=Tn(),a=r===``?`global`:`recursive`;for(let[,t]of Object.entries(e))i.add(t.name,t,1,r,a);for(let[,e]of Object.entries(t))i.add(e.name,e,2,r,a);for(let[,e]of Object.entries(n))i.add(e.name,e,3,r,a);return i.getEffective()}function Mn(e){let t=new Map,n=[];for(let r of e)r!=null&&(typeof r==`string`||typeof r==`number`||typeof r==`boolean`?t.set(r,!0):n.push(r));return{lookup:t,complex:n}}function Nn(e,t){let n=new Set;function r(e){let i=X(e);if(i===`object`||i===`unknown`){if(`fields`in e&&e.fields)for(let t of Object.values(e.fields))n.add(t.name)}else if(i===`union`||i===`composite`){if(!(`schema`in e)||!e.schema)return;let n=z.fromFieldSchema(e.schema);if(!n.isMultiple())return;for(let e of n.asMultiple()){let n=t.schemas?.[e.id];n&&r(n)}}}for(let n of e){let e=t.schemas?.[n.id];e&&r(e)}return n}var Pn=class e{nodes=new Map;dependencies=new Map;visitedState=new Map;executionOrder=[];nextNodeID=1;nullablePaths=new Set;buildNodeID(){return this.nextNodeID++}addNode(e){let t=e.getID();this.nodes.has(t)||(this.nodes.set(t,e),this.dependencies.set(t,e.getDependencies()))}createUnexpectedFieldsNode(e,t,n,r=[]){return new sn(this.buildNodeID(),e,t,n,r)}createRequiredFieldNode(e,t,n,r,i,a=[]){return new cn(this.buildNodeID(),e,t,n,r,i,a)}createTypeCheckNode(e,t,n,r=[]){return new ln(this.buildNodeID(),e,t,n,r)}createCompletionNode(e,t,n){return new hn(this.buildNodeID(),e,t,n)}finalize(){this.visitedState=new Map;let e=[],t=!1,n=r=>{if(t)return;let i=this.visitedState.get(r)??0;if(i===1){t=!0;return}if(i!==2){this.visitedState.set(r,1);for(let e of this.dependencies.get(r)??[])n(e);this.visitedState.set(r,2),e.push(r)}};for(let e of this.nodes.keys())if((this.visitedState.get(e)??0)===0&&(n(e),t))throw Error(`Circular dependency detected in validation graph at node ${e}`);this.executionOrder=e}async traverse(e,t,n,r,i){let a={originalRoot:i,rootData:t,data:t,functionMap:e,maxDepth:r,mode:n,visited:new Map,issues:[]};for(let e of this.executionOrder){let t=this.nodes.get(e),r=this.dependencies.get(e)??[],i=!1;for(let t of r){let n=a.visited.get(t);if(n!==void 0&&!n){a.visited.set(e,!0),i=!0;break}}if(i)continue;let o=t.getPathParts(),s=t.getPath(),[c,l]=K(a,o),u=t instanceof cn,d=this.nullablePaths.has(s);if((!l||c===void 0)&&!u){a.visited.set(e,!0);continue}if(c===null){if(u)continue;if(d){a.visited.set(e,!0);continue}}let f=await t.execute(a);if(a.visited.set(e,f.success),f&&!f.skipped&&f.issues.length>0)for(let e of f.issues)switch(n){case`strict`:a.issues.push(e);break;case`partialStrict`:e.code!==`REQUIRED_FIELD_MISSING`&&a.issues.push(e);break;case`loose`:e.code!==`REQUIRED_FIELD_MISSING`&&e.code!==`UNEXPECTED_FIELD`&&a.issues.push(e)}}return a.issues}async buildFromSchema(e,t,n,r,i,a,o,s,c,l){let u=[],d=i&&`fields`in i&&i.fields?i.fields:e.fields,f=null;if(!c){let e=new Set(Object.values(d).map(e=>e.name));f=this.createUnexpectedFieldsNode(t,n,e),this.addNode(f),u.push({id:f.getID(),deps:[]})}let p=[],m=[];for(let i of Object.values(d)){let a=await this.buildFieldNodes(i,t,n,p,e,r,o,s,l);m.push(...a)}let ee=jn(B(i?i.constraints:e.constraints),a,o&&t!==``?An(o,t):{},t),te=m.map(e=>e.id),h=this.buildFromEffectiveConstraints(ee,n,te,r);if(h.length>0){let e=this.createCompletionNode(t,n,h);this.addNode(e),u.push({id:e.getID(),deps:h})}return u}async buildFieldNodes(e,t,n,r,i,a,o,s,c){let[l,u]=Qt(t,n,e.name);e.nullable!==!1&&this.nullablePaths.add(l);let d=r,f=[];if(e.required){let r=this.createRequiredFieldNode(l,u,e.name,t,n,d);this.addNode(r),d=[r.getID()],f.push({id:r.getID(),deps:d})}if(!On(e.type)){let t=this.createTypeCheckNode(l,u,e.type,d);this.addNode(t),d=[t.getID()],f.push({id:t.getID(),deps:d})}let p=await this.buildFieldTypeNodes(e,l,u,d,i,a,o,s,c);return f.push(...p),f}async buildFieldTypeNodes(e,t,n,r,i,a,o,s,c){let l=[],u=null;switch(e.type){case`enum`:u=await this.buildEnumNode(e,t,n,r,o);break;case`array`:u=await this.buildContainerNode(e,t,n,r,o,s,`array`,`array items`);break;case`set`:{let i=await this.buildContainerNode(e,t,n,r,o,s,`array`,`set elements`),a=i instanceof dn?i.graph:null;u=new pn(this.buildNodeID(),t,n,a,r);break}case`record`:u=await this.buildContainerNode(e,t,n,r,o,s,`record`,`record items`);break;case`union`:u=await this.buildUnionNode(e,t,n,r,o,s);break;case`composite`:{let i=await this.buildCompositeNode(e,t,n,r,o,s);return l.push(...i),l}case`geometry`:u=new mn(this.buildNodeID(),t,n,r);break;case`object`:{let r=await this.buildObjectFieldNodes(e,t,n,i,a,o,s,c);return l.push(...r),l}default:return l}return u&&(this.addNode(u),l.push({id:u.getID(),deps:u.getDependencies()})),l}async buildObjectFieldNodes(e,t,n,r,i,a,o,s){let c=[],l=z.fromFieldSchema(e.schema);if(l.isZero())throw Error(`FieldSchemaReference is zero/uninitialized at path '${t}'`);if(!l.isSingle())throw Error(`Object field must use a single schema reference at path '${t}'`);let u=l.asSingle(),d=a.schemas?.[u.id];if(!d)throw Error(`Nested schema '${u.id}' not found for field '${e.name}' at path '${t}'`);if(o.isRecursive(u.id)){let e=B(u.constraints),r=await o.getOrBuildRecursiveGraph(u.id,d,e,a),i=new gn(this.buildNodeID(),t,n,r,u.id);return this.addNode(i),c.push({id:i.getID(),deps:[]}),c}let f=Dn(d,a);o.markBuilding(u.id);try{let e=await this.buildFromSchema(f,t,n,i,d,B(u.constraints),a,o,s,!1),r=new hn(this.buildNodeID(),t,n,e.map(e=>e.id));this.addNode(r),c.push({id:r.getID(),deps:e.map(e=>e.id)})}finally{o.unmarkBuilding(u.id)}return c}async buildContainerNode(t,n,r,i,a,o,s,c){let l=e=>s===`array`?new dn(this.buildNodeID(),n,r,e,i):new fn(this.buildNodeID(),n,r,e,i),u=z.fromFieldSchema(t.schema);if(u.isZero())return l(null);if(u.isSingle()){let e=u.asSingle();if(e.id!==``){let t=a.schemas?.[e.id];if(!t)throw Error(`Nested schema '${e.id}' not found for ${c} at path '${n}'`);let r=X(t),i=r===`composite`||r===`union`,s;s=r===`object`?z.fromSingle({id:e.id}):`schema`in t&&t.schema?z.fromFieldSchema(t.schema):z.zero();let u=s.isZero()?void 0:s.isSingle()?s.asSingle():s.isMultiple()?s.asMultiple():s.asInline(),d={name:`item`,type:r};u!==void 0&&(d.schema=u);let f=`default`in t?t.default:void 0;return f!==void 0&&(d.default=f),l(await this.createSubGraph(`item`,d,n,a,o,i,!1))}}if(u.isInline()){let t=u.asInline();if(!t.type)throw Error(`Inline descriptor missing 'type' field at path '${n}'`);let r=new e,i=new Map,s=t.type;if(s===`object`){let e={name:`__inline_record`,version:`0`,fields:{}};a.schemas!==void 0&&(e.schemas=a.schemas),await r.buildFromSchema(e,``,[],i,null,{},a,o,!0,!1)}else{let e=r.createTypeCheckNode(`item`,[`item`],s);r.addNode(e)}return r.finalize(),l(r)}throw Error(`Invalid schema reference for ${c} at path '${n}'`)}async buildEnumNode(e,t,n,r,i){let a=z.fromFieldSchema(e.schema);if(a.isZero())throw Error(`Enum field must have schema reference at path '${t}'`);let o=a.isSingle()?[a.asSingle()]:a.isMultiple()?a.asMultiple():[];if(o.length===0&&!a.isInline())throw Error(`Enum schema reference must be single, multiple, or inline at path '${t}'`);if(a.isInline()){let e=a.asInline();if(!e.type)throw Error(`Inline enum descriptor missing 'type' at path '${t}'`);if(!e.values||e.values.length===0)throw Error(`Inline enum descriptor missing 'values' at path '${t}'`);let{lookup:i,complex:o}=Mn(e.values),s=e.type===`number`||e.type===`integer`||e.type===`decimal`;return new un(this.buildNodeID(),t,n,i,o,s,r)}let s=new Map,c=[],l=null;for(let e of o){let n,r,a;if(e.id!==``){let o=i.schemas?.[e.id];if(!o)throw Error(`Enum schema '${e.id}' not found at path '${t}'`);if(!(`values`in o)||!o.values?.length)throw Error(`Enum schema '${e.id}' has no values defined at path '${t}'`);a=o.type,{lookup:n,complex:r}=Mn(o.values)}else throw Error(`Inline descriptor in named-reference array is not permitted at path '${t}'`);for(let[e]of n)s.set(e,!0);c=c.concat(r),l||=a}let u=l===`number`||l===`integer`||l===`decimal`;return new un(this.buildNodeID(),t,n,s,c,u,r)}async buildUnionNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Union field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Nested schema '${e.id}' not found for union at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:X(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!1);c.push(o)}return new _n(this.buildNodeID(),t,n,c,r)}async buildCompositeNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Composite field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=Nn(s,i),l=this.createUnexpectedFieldsNode(t,n,c);this.addNode(l);let u=[l.getID()],d=[];for(let e of s){let r=i.schemas?.[e.id];if(!r)throw Error(`Nested schema '${e.id}' not found for composite at path '${t}'`);let o=X(r);if(o===`object`||o===`unknown`){let o=Dn(r,i);a.markBuilding(e.id);let s;try{s=await this.buildFromSchema(o,t,n,new Map,r,B(e.constraints),i,a,!0,!0)}finally{a.unmarkBuilding(e.id)}for(let e of s)e.deps.includes(l.getID())||(e.deps=[l.getID(),...e.deps],this.dependencies.set(e.id,e.deps));d.push(...s)}else if(o===`union`){if(!(`schema`in r)||!r.schema)throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let o=z.fromFieldSchema(r.schema);if(!o.isMultiple())throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Union variant schema '${e.id}' not found at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:X(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!0);c.push(o)}let l=new _n(this.buildNodeID(),t,n,c,u);this.addNode(l),d.push({id:l.getID(),deps:u})}else throw Error(`Composite part '${e.id}' has unsupported effective type '${o}'; composite parts must be object or union schemas at path '${t}'`)}return[{id:l.getID(),deps:[]},...d]}buildFromEffectiveConstraints(e,t,n,r){let i=[];for(let a of e){let e=this.buildFromConstraintRuleWithScope(a.constraint,a.basePath,t,n,r,a.scope);i.push(...e)}return i}buildFromConstraintRuleWithScope(e,t,n,r,i,a){let o=`${t}:${e.name}`;if(i.get(o))return[];if(i.set(o,!0),e.kind===`rule`){let[i,o]=en(t,n,e.fields),s=new vn(this.buildNodeID(),t,n,e,i,o,t,n,a,r);return this.addNode(s),[s.getID()]}if(e.kind===`group`){let i=new yn(this.buildNodeID(),t,n,e,e.name,a,r);return this.addNode(i),[i.getID()]}return[]}async createSubGraph(t,n,r,i,a,o,s){let c={name:`subgraph_${r}`,version:`0`,fields:{[t]:n}};i.schemas!==void 0&&(c.schemas=i.schemas);let l=new e,u=new Map;return await l.buildFromSchema(c,``,[],u,null,{},i,a,o,s),l.finalize(),l}},Fn=class e{fmap;graph;config;constructor(e,t,n){this.graph=e,this.fmap=t,this.config=n}async validate(e){return this.graph.traverse(this.fmap,e,`strict`,this.config.maxDepth,e)}async validatePartial(e){return this.graph.traverse(this.fmap,e,`partialStrict`,this.config.maxDepth,e)}async validateLoose(e){return this.graph.traverse(this.fmap,e,`loose`,this.config.maxDepth,e)}static async create(t,n,r=Yt()){let i=new Pn,a=new En,o=new Map;return await i.buildFromSchema(t,``,[],o,null,{},t,a,!1,!1),i.finalize(),new e(i,n,r)}},In={version:`1.0.0`,name:`Schema`,description:`Meta-schema defining the structure of schema definitions`,fields:{"019d7775-6565-7755-904d-2d0cdf0e1b86":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6565-73d6-a7be-130b3c1e93c8":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6565-722f-9a5a-2810f537a61c":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6565-7fe8-86f6-57a8a305a314":{name:`schemas`,type:`record`,schema:{id:`019d7775-6565-7db3-9bb0-be9fc50550bd`}},"019d7775-6565-7c1f-a6f7-aeb5c4eb35e4":{name:`name`,required:!0,type:`string`},"019d7775-6565-7545-9e25-fc471f57bcfb":{name:`description`,type:`string`},"019d7775-6565-7bdc-a9d4-776b1f163346":{name:`version`,required:!0,type:`string`},"019d7775-6565-74ed-8a52-12027f3c577d":{name:`metadata`,type:`record`}},schemas:{"019d7775-6566-7da1-b24a-67bd81dd2b0e":{name:`Unknown`,type:`unknown`},"019d7775-6565-7db3-9bb0-be9fc50550bd":{name:`NestedSchema`,fields:{"019d7775-6566-77a1-9b36-bbd57f927d11":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6567-7931-b2d6-fb52039c28b5":{name:`type`,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6567-7742-90ae-a96178ae505d":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"019d7775-6567-7709-a7e3-ed75f833da15":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-748e-b099-50c76a5b79c2":{name:`name`,required:!0,type:`string`},"019d7775-6566-74df-a8df-038cc26af034":{name:`description`,type:`string`},"019d7775-6567-748e-bcad-76068cc4739f":{name:`metadata`,type:`record`},"019d7775-6567-7022-b7fb-3975ccae2b72":{name:`concrete`,type:`boolean`},"019d7775-6567-7f25-b7e2-acc72435964f":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6567-750b-a618-5bcdc4e5d04a":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6567-75c4-b399-eb4764617462":{name:`default`,type:`unknown`}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`}}},"019d7775-6565-71ef-a08f-cdcdb116f947":{name:`IndexCondition`,fields:{"019d7775-6567-75ef-8e82-347da30f06fd":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7a70-8b56-e55704397fc2`}},"019d7775-6567-7466-a873-e7866e9d031f":{name:`field`,required:!0,type:`string`},"019d7775-6567-7526-9704-d19fb4eb8d8d":{name:`value`,required:!0,type:`unknown`},"a2f03bc4-e620-4b7f-a2ad-8271471ac1e4":{name:`description`,type:`string`},"8782f46d-5f1f-4102-9c3c-917752d8c9d1":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-713e-8355-df812c6bb8c9":{name:`index_condition_exclusive_type`,predicate:`index_condition_type_exclusive`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`}}},"019d7775-6565-7d6a-a531-47e7ea838bc4":{name:`IndexConditionGroup`,fields:{"019d7775-6567-7b4e-893f-ee7afb0c4be1":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6567-7af2-817f-bc5b8732c198":{name:`conditions`,required:!0,type:`array`,schema:{id:`019d7775-6565-74a3-a6e0-881a0148fc59`}},"670ebd05-3bce-4da7-a39b-20cf9ba67abe":{name:`description`,type:`string`},"8e60fb23-0e3f-4ae5-bfff-fd20da1da756":{name:`metadata`,type:`record`}}},"019d7775-6565-794d-8aba-bdc5e3cbe97e":{name:`ConstraintUnion`,type:`union`,schema:[{id:`019d7775-6565-703f-95cf-e7511d33c91b`},{id:`019d7775-6565-7ea4-a820-e8bcdbc690bc`}]},"019d7775-6566-7786-8769-c13eb5a883d9":{name:`InlineTypeDescriptor`,fields:{"019d7775-6568-713f-867f-93dde123ccfd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e05-ab1b-6f768f459927`}},"019d7775-6568-7ae5-95b7-56c97722c732":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"347a2348-014b-41a7-80ad-a4e8d364ef5d":{name:`description`,type:`string`},"7d0f2347-3dfe-4e37-8235-1f9122e045dc":{name:`metadata`,type:`record`}}},"019d7775-6566-7e3b-860a-c4aa4adf5d6d":{name:`LogicalOperatorEnum`,type:`string`,values:[`and`,`or`,`not`,`nor`,`xor`,`nand`,`xnor`]},"019d7775-6566-7f2f-a009-0af144502aac":{name:`IndexOrderEnum`,type:`string`,values:[`asc`,`desc`]},"019d7775-6565-7f15-b0f4-27a4f40cbf5c":{name:`Field`,fields:{"019d7775-6566-727e-b4ff-4e6e8b800808":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6566-79e9-a60b-1c15e2154580":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-7a4c-877d-262c18adb028":{name:`name`,required:!0,type:`string`},"019d7775-6566-7b93-aa4c-b1e3869512d8":{name:`description`,type:`string`},"019d7775-6566-7ed5-9fb3-33a67b90dbbe":{name:`default`,type:`unknown`},"019d7775-6566-722b-8132-d5d8c4c9b2b6":{name:`required`,type:`boolean`},"a1000000-0000-0000-0000-000000000001":{name:`nullable`,type:`boolean`},"019d7775-6566-7367-9664-adb25994bf41":{name:`deprecated`,type:`boolean`},"019d7775-6566-7fc8-82c6-735838cdfe21":{name:`unique`,type:`boolean`},"95ab0d3b-352c-4aff-a43a-81ab889aaa42":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-7605-8bfb-c27365b73581":{name:`enum_fields_valid`,predicate:`enum_fields_valid`},"019d7775-6563-74fe-a5ae-4eef62a128d9":{name:`arrays_require_schema`,predicate:`collection_requires_schema`},"019d7775-6563-7080-ae67-bad5cd864f07":{name:`objects_require_schema`,predicate:`object_requires_schema`},"019d7775-6563-7e83-92c4-0258a8fc0d3a":{name:`unions_require_multiple_schemas`,predicate:`union_requires_multiple_schemas`},"019d7775-6564-7c60-9f9f-aeeabe05c1b5":{name:`default_value_type_match`,predicate:`default_matches_type`},"019d7775-6564-77ca-94bc-30a07da76c3d":{name:`records_allow_optional_schema`,predicate:`record_schema_cardinality`},"019d7775-6564-7768-8995-72c282cfe69f":{name:`inline_type_descriptor_valid`,predicate:`inline_type_descriptor_valid`},"019d7775-6563-7c55-a6f3-ac8f087d89d1":{name:`primitives_no_schema`,predicate:`primitives_prohibit_schema`},"019d7775-6563-73ce-aef1-86943b54a1ca":{name:`composites_require_multiple_schemas`,predicate:`composite_requires_multiple_schemas`},"019d7775-6564-719d-9948-67dd885e7632":{name:`object_referenced_schema_has_fields`,predicate:`object_referenced_schema_has_fields`},"019d7775-6564-75ce-a807-02396e0b8394":{name:`composite_referenced_schemas_must_be_objects`,predicate:`composite_referenced_schemas_must_be_objects`},"019d7775-6564-75bc-abf5-6ddd81b7d2d4":{name:`schema_reference_form_correct`,predicate:`schema_reference_form_correct`}}},"019d7775-6565-7ab6-9c65-ab60632aeca8":{name:`Constraint`,type:`composite`,schema:[{id:`019d7775-6565-7068-9cb9-28460e0368f0`},{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}]},"019d7775-6566-765a-b142-157d1d6367b7":{name:`FieldTypeEnum`,type:`string`,values:[`unknown`,`string`,`number`,`integer`,`decimal`,`boolean`,`array`,`set`,`enum`,`object`,`record`,`union`,`composite`,`geometry`,`bytes`]},"019d7775-6566-7a70-8b56-e55704397fc2":{name:`ComparisonOperatorEnum`,type:`string`,values:[`eq`,`neq`,`lt`,`lte`,`gt`,`gte`,`in`,`nin`,`contains`,`ncontains`,`exists`,`nexists`]},"019d7775-6565-74a3-a6e0-881a0148fc59":{name:`IndexConditionUnion`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"019d7775-6565-7068-9cb9-28460e0368f0":{name:`ConstraintMetadata`,fields:{"019d7775-6567-7017-9068-5bfcd1f9fef0":{name:`name`,required:!0,type:`string`},"019d7775-6568-7b0d-ad28-391874b5e3de":{name:`description`,type:`string`},"ee8ea6de-95f9-48e5-a047-4744b39e1c8f":{name:`metadata`,type:`record`}}},"019d7775-6565-703f-95cf-e7511d33c91b":{name:`ConstraintRule`,fields:{"019d7775-6568-7ad6-9e45-7fdbdef3fc7e":{name:`predicate`,required:!0,type:`string`},"019d7775-6568-7221-a4de-7e82d6c9f6bc":{name:`parameters`,type:`unknown`},"019d7775-6568-7d8b-a230-328fe5d352e8":{name:`fields`,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`},"019d7775-6563-752b-bf9d-bf8936472250":{name:`constraint_rule_requires_predicate`,predicate:`constraint_rule_requires_predicate`},"019d7775-6564-7fc2-bbdb-0fc59e567f2b":{name:`constraint_fields_exist`,predicate:`constraint_fields_exist`}}},"019d7775-6565-7ea4-a820-e8bcdbc690bc":{name:`ConstraintGroup`,fields:{"019d7775-6568-7ebf-a1c3-829de95d9216":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6568-76d9-a21c-fb289c36cc70":{name:`rules`,required:!0,type:`array`,schema:{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`}}},"019d7775-6565-7f2a-956b-35b40caa8ec7":{name:`SchemaReference`,fields:{"019d7775-6568-762c-932e-89b85f08a951":{name:`id`,required:!0,type:`string`},"019d7775-6568-77eb-bd9d-d874a2354fd5":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6568-7ed9-99b3-90cca439ee2e":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"abb42217-0f28-4501-ab12-c54bfe5d1492":{name:`description`,type:`string`},"ed9a369c-1c98-4bc7-845b-92fa81be31f8":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-79f2-9d64-d6b8c2696deb":{name:`schema_reference_id_required`,predicate:`schema_reference_id_required`}}},"019d7775-6565-71bc-84f3-044dc089d50c":{name:`SchemaReferenceArray`,type:`array`,schema:{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`}},"019d7775-6566-7e05-ab1b-6f768f459927":{name:`InlineTypeEnum`,type:`string`,values:[`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]},"019d7775-6566-7df4-bc09-67956251c4a2":{name:`IndexTypeEnum`,type:`string`,values:[`normal`,`unique`,`primary`,`spatial`,`fulltext`]},"019d7775-6566-7a63-9e10-5d11c8eb607c":{name:`String`,type:`string`},"019d7775-6565-74f6-9c72-9e35d3d16b0a":{name:`Index`,fields:{"019d7775-6567-7390-96c6-8154ccc6d1ad":{name:`name`,required:!0,type:`string`},"019d7775-6567-7568-98c8-76d64b745bc7":{name:`description`,type:`string`},"019d7775-6567-7a3d-b154-1199406b7851":{name:`unique`,type:`boolean`},"019d7775-6567-7e59-a2f4-ffc431e24fdd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7df4-bc09-67956251c4a2`}},"019d7775-6567-787d-921c-fa28f53cedcd":{name:`fields`,required:!0,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}},"019d7775-6567-76c9-869a-c4e25c154583":{name:`order`,type:`enum`,schema:{id:`019d7775-6566-7f2f-a009-0af144502aac`}},"019d7775-6567-7bc2-b870-6e1ae4faa558":{name:`condition`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"75f044b7-253c-4f48-93b7-692b9626c0ea":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-7c2d-b06d-82971c0b1fa9":{name:`index_fields_exist`,predicate:`index_fields_reference_valid`},"019d7775-6564-7660-adc8-3f1ab037d695":{name:`spatial_index_on_geometry_field`,predicate:`spatial_index_on_geometry_field`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`},"019d7775-6564-7285-a734-f7eed15d25e4":{name:`index_fields_not_empty`,predicate:`index_fields_not_empty`}}}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`},"019d7775-6564-7565-b53c-49667d23c237":{name:`global_field_id_uniqueness`,predicate:`global_field_id_uniqueness`},"019d7775-6564-7a5d-bb7e-d2e9e804123f":{name:`schema_reference_integrity`,predicate:`schema_reference_exists`},"019d7775-6563-7f89-8368-395733efec86":{name:`schema_name_required`,predicate:`schema_name_required`}}};const Ln=new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`geometry`,`bytes`,`unknown`]),Rn=new Set([`array`,`set`]),zn=[`fields`],Bn=[`type`,`default`,`values`,`schema`],Vn=new Set([`number`,`integer`,`decimal`,`string`]);function Z(e){let t=e.type;return t==null?[``,!1]:[String(t),!0]}function Hn(e){return Ln.has(e)}function Q(e){return Vn.has(e)}function Un(e){return Rn.has(e)}function $(e,t){let n=e.schemas;return n&&n[t]||null}function Wn(e){if(e.fields){let t=e.fields;if(Object.keys(t).length>0)return!0}let t=e.type;return t===`object`||t===`record`}function Gn(e){let t=e.schema;return t==null?[[],!1]:Array.isArray(t)?[t,!0]:[[t],!1]}function Kn(e,t,n){let r=$(e,t);if(!r)return[U(`ENUM_NAMED_SCHEMA_NOT_FOUND`,`Enum referenced schema '${t}' does not exist`,n)];let i=[],a=r.type;if(a==null)return i.push(U(`ENUM_NAMED_SCHEMA_NO_TYPE`,`Enum referenced schema '${t}' must have a 'type' (string or numeric)`,n)),i;let o=String(a);if(!(o===`string`||Q(o)))return i.push(U(`ENUM_NAMED_SCHEMA_INVALID_TYPE`,`Enum referenced schema '${t}' type must be string or numeric, got '${a}'`,n)),i;let s=r.values;if(s==null)return i.push(U(`ENUM_NAMED_SCHEMA_MISSING_VALUES`,`Enum schema '${t}' must have a 'values' array`,n)),i;let c=s;if(!Array.isArray(c)||c.length===0)return i.push(U(`ENUM_NAMED_SCHEMA_EMPTY_VALUES`,`Enum schema '${t}' 'values' must be a non-empty array`,n)),i;let l=o===`string`;return c.forEach((e,r)=>{e!=null&&(l?typeof e!=`string`&&i.push(U(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be string, got ${typeof e}`,`${n} (values[${r}])`)):Q(o)&&(J(e)||i.push(U(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be numeric (type '${o}'), got ${typeof e}`,`${n} (values[${r}])`))))}),zn.forEach(e=>{let a=r[e];a!=null&&Object.keys(a).length>0&&i.push(U(`ENUM_NAMED_SCHEMA_HAS_FIELDS`,`Enum referenced schema '${t}' must not have '${e}' (must be a simple value schema)`,n))}),i}function qn(e,t){let n=[],r=e.type;if(r==null)return n.push(U(`ENUM_INLINE_NO_TYPE`,`Inline enum descriptor must have a 'type' (string or numeric)`,t)),n;let i=String(r);if(!(i===`string`||Q(i)))return n.push(U(`ENUM_INLINE_INVALID_TYPE`,`Inline enum descriptor type must be string or numeric, got '${r}'`,t)),n;let a=e.values;if(a==null)return n.push(U(`ENUM_INLINE_MISSING_VALUES`,`Inline enum descriptor must have a 'values' array`,t)),n;let o=a;if(!Array.isArray(o)||o.length===0)return n.push(U(`ENUM_INLINE_EMPTY_VALUES`,`Inline enum descriptor 'values' must be a non-empty array`,t)),n;let s=i===`string`;return o.forEach((e,r)=>{e!=null&&(s?typeof e!=`string`&&n.push(U(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be string, got ${typeof e}`,`${t}.values[${r}]`)):Q(i)&&(J(e)||n.push(U(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be numeric (type '${i}'), got ${typeof e}`,`${t}.values[${r}]`))))}),n}function Jn(e,t,n){if(typeof e!=`object`||!e)return[U(`ENUM_REF_INVALID`,`Enum schema reference must be an object`,t)];let r=e,i=`id`in r,a=`type`in r;if(i&&a)return[U(`ENUM_REF_AMBIGUOUS`,`Enum schema reference cannot have both 'id' (named) and 'type' (inline)`,t)];if(i){let e=r.id;return e==null||e===``?[U(`ENUM_NAMED_REF_INVALID_ID`,`Named enum reference 'id' must be a non-empty string`,t)]:Kn(n,String(e),t)}return a?qn(r,t):[U(`ENUM_REF_NO_ID_OR_TYPE`,`Enum schema reference must have either 'id' (named) or 'type' (inline)`,t)]}function Yn(e,t){let n=t.split(`.`),r=e.fields&&typeof e.fields==`object`?e.fields:null,i=null;for(let t=0;t<n.length;t++){if(!r)return null;let a=null;for(let e of Object.values(r))if(e&&typeof e==`object`&&e.name===n[t]){a=e;break}if(!a)return null;if(i=a,t<n.length-1){let t=a.schema;if(t&&typeof t==`object`&&!Array.isArray(t)){if(typeof t.id==`string`){let n=t.id,i=e.schemas?.[n];r=i&&typeof i==`object`?i.fields??null:null}else r=t.fields?t.fields:null}else r=null}}return i?{def:i}:null}const Xn={primitives_prohibit_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;return r&&Hn(n)&&i!=null?[U(`PRIMITIVE_HAS_SCHEMA`,`Primitive type '${n}' cannot have a schema reference`,``)]:[]},enum_fields_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`enum`)return[];if(t.schema==null)return[U(`ENUM_MISSING_SCHEMA`,`Enum field must have a schema reference`,``)];let[i,a]=Gn(t);if(i.length===0)return[U(`ENUM_NO_VALID_REFS`,`Enum field has no valid schema references`,``)];let o=[];return i.forEach((t,n)=>{let r=a?`schema[${n}]`:`schema`;o.push(...Jn(t,r,e.root))}),o},composite_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;if(r&&n===`composite`){if(i==null)return[U(`COMPOSITE_MISSING_SCHEMA`,`Composite type must have schema references`,``)];if(Array.isArray(i)){if(i.length<2)return[U(`COMPOSITE_INSUFFICIENT_SCHEMAS`,`Composite must have at least 2 schema references`,``)]}else return[U(`COMPOSITE_SCHEMA_NOT_ARRAY`,`Composite 'schema' must be an array of references`,``)]}return[]},composite_referenced_schemas_must_be_objects:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`composite`)return[];let i=t.schema;if(!Array.isArray(i)||i.length===0)return[];let a=[],o=e.root,s=(e,t)=>{let n=$(o,e);if(!n)return[];let r=[],i=n.type;if(i!=null){let e=String(i);if(e===`composite`)return[];if(e===`union`){let e=n.schema;return Array.isArray(e)&&e.forEach((e,n)=>{typeof e==`object`&&e&&`id`in e&&r.push(...s(String(e.id),`${t}.schema[${n}]`))}),r}}return Wn(n)||r.push(U(`COMPOSITE_REF_NOT_OBJECT`,`Composite schema '${e}' must effectively represent an object type (has fields, or type 'object'/'record')`,t)),r};return i.forEach((e,t)=>{typeof e==`object`&&e&&`id`in e&&a.push(...s(String(e.id),`schema[${t}]`))}),a},object_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;return r&&n===`object`&&i==null?[U(`OBJECT_MISSING_SCHEMA`,`Object type must have a schema reference`,``)]:[]},object_referenced_schema_has_fields:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let[r,i]=Z(n);if(!i||r!==`object`)return[];let a=n.schema;if(typeof a!=`object`||!a||Array.isArray(a))return[];let o=a.id;if(!o)return[];let s=$(t,String(o));if(!s)return[];let c=s.fields;return c==null?[U(`OBJECT_REF_NO_FIELDS`,`Object field references schema '${o}' which has no 'fields' (must be Schema mode)`,``)]:typeof c!=`object`||Object.keys(c).length===0?[U(`OBJECT_REF_EMPTY_FIELDS`,`Object field references schema '${o}' which has empty fields`,``)]:[]},spatial_index_on_geometry_field:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n||n.type!==`spatial`)return[];let r=n.fields;if(!Array.isArray(r))return[];for(let e of r){let n=String(e),r=Yn(t,n);if(!r)continue;let i=r.def.type;if(i!==`geometry`)return[U(`SPATIAL_INDEX_NON_GEOMETRY`,`Spatial index can only reference geometry fields, but field '${n}' has type '${i}'`,``)]}return[]},index_condition_value_matches_field_type:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let r=n.field;if(r==null)return[];let i=n.value,a=Yn(t,String(r));if(!a)return[];let o=a.def.type,s=!0;switch(o){case`string`:typeof i!=`string`&&i!==null&&(s=!1);break;case`boolean`:typeof i!=`boolean`&&i!==null&&(s=!1);break;case`integer`:!an(i)&&i!==null&&(s=!1);break;case`number`:case`decimal`:!J(i)&&i!==null&&(s=!1)}return s?[]:[U(`INDEX_CONDITION_VALUE_TYPE_MISMATCH`,`Value for field '${r}' must be ${o}`,``)]},schema_reference_exists:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schemas||{},r=[],i=(e,t)=>{if(e&&typeof e==`object`&&`id`in e){let i=e.id;typeof i==`string`&&!n[i]&&r.push(U(`SCHEMA_REFERENCE_NOT_FOUND`,`Referenced schema '${i}' does not exist`,t))}},a=(e,t)=>{if(!(typeof e!=`object`||!e)){if(e.hasOwnProperty(`schema`)){let n=e.schema;Array.isArray(n)?n.forEach((e,n)=>i(e,`${t}.schema[${n}]`)):i(n,`${t}.schema`)}for(let[n,r]of Object.entries(e))typeof r==`object`&&r&&a(r,t?`${t}.${n}`:n)}};return a(t,``),r},default_matches_type:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.type,r=t.default;if(n===void 0||r==null)return[];let i=String(n),a=``;switch(i){case`string`:typeof r!=`string`&&(a=`must be string`);break;case`boolean`:typeof r!=`boolean`&&(a=`must be boolean`);break;case`integer`:an(r)||(a=`must be integer`);break;case`number`:case`decimal`:J(r)||(a=`must be numeric`);break;case`array`:case`set`:Array.isArray(r)||(a=`must be array`);break;case`object`:case`record`:(typeof r!=`object`||Array.isArray(r))&&(a=`must be object`);break;case`geometry`:Array.isArray(r)||(a=`must be array of coordinate arrays`)}return a?[U(`DEFAULT_VALUE_TYPE_MISMATCH`,`Default value ${JSON.stringify(r)} ${a} for type ${i}`,``)]:[]},global_field_id_uniqueness:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=new Map,r=[],i=(e,t)=>{if(!(typeof e!=`object`||!e))for(let i of Object.keys(e)){let e=n.get(i);e?r.push(U(`DUPLICATE_FIELD_ID`,`Field ID '${i}' is not unique; already used at ${e}`,`${t}.${i}`)):n.set(i,`${t}.${i}`)}};t.fields&&i(t.fields,`fields`);let a=t.schemas;if(a&&typeof a==`object`){for(let[e,t]of Object.entries(a))if(typeof t==`object`&&t){let n=t;n.fields&&i(n.fields,`schemas.${e}.fields`)}}return r},constraint_fields_exist:e=>{let t=e.root,n=[],r=(e,n)=>{let r=e.split(`.`),i=n;for(let e=0;e<r.length;e++){let n=r[e],a=i[n];if(a===void 0)return!1;if(e===r.length-1)return!0;let o=a;if(o.type===`object`){let e=o.schema;if(e&&e.id){let n=$(t,String(e.id));if(n&&n.fields){i=n.fields;continue}}}return!1}return!1},i=(e,t,i)=>{for(let[a,o]of Object.entries(e)){if(typeof o!=`object`||!o)continue;let e=o;if(e.predicate&&e.fields){let o=e.fields;Array.isArray(o)&&o.forEach((e,o)=>{typeof e==`string`&&(r(e,t)||n.push(U(`CONSTRAINT_FIELD_NOT_FOUND`,`Constraint '${a}' references non-existent field path '${e}'`,`${i}constraints.${a}.fields[${o}]`)))})}}},a=t.fields,o=t.constraints;o&&a&&i(o,a,``);let s=t.schemas;if(s){for(let[e,t]of Object.entries(s))if(typeof t==`object`&&t){let n=t,r=n.constraints,a=n.fields;r&&a&&i(r,a,`schemas.${e}.`)}}return n},inline_type_descriptor_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schema;if(!n||typeof n!=`object`||Array.isArray(n))return[];let r=t.type,i=n.type;if(r===void 0||i===void 0)return[];let a=new Set([`record`,`array`,`set`,`enum`]),o=String(r);if(!a.has(o))return[U(`INLINE_DESCRIPTOR_NOT_ALLOWED_FOR_FIELD_TYPE`,`Inline type descriptors are only allowed for fields of type record, array, set, or enum, but got '${o}'`,``)];let s=String(i);if(!new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]).has(s))return[U(`INLINE_DESCRIPTOR_INVALID_TYPE`,`Inline descriptor type '${s}' is not allowed (only primitives and 'record')`,``)];let c=n.values;if(c!=null){if(s!==`string`&&!Q(s))return[U(`INLINE_DESCRIPTOR_VALUES_WITHOUT_ENUM`,`'values' can only be used with string or numeric inline types`,``)];if(!Array.isArray(c)||c.length===0)return[U(`INLINE_DESCRIPTOR_VALUES_NOT_ARRAY`,`'values' must be a non‑empty array`,``)]}return[]},schema_reference_form_correct:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r)return[];let i=t.schema;if(i==null)return[];let a=e=>({isNamed:`id`in e,isInline:`type`in e});if(Array.isArray(i)){if(i.length===0)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires at least one reference`,``)];if(n!==`union`&&n!==`composite`&&n!==`enum`)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' cannot use an array of references`,``)];for(let e=0;e<i.length;e++){let t=i[e];if(typeof t!=`object`||!t)return[U(`SCHEMA_REF_FORM_INVALID`,`Element ${e} of schema array is not an object`,``)];let{isNamed:r,isInline:o}=a(t);if(n===`enum`){if(!r&&!o)return[U(`SCHEMA_REF_FORM_INVALID`,`Enum array element ${e} must be a named reference or inline descriptor`,``)]}else if(!r)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' array element ${e} must be a named schema reference (with 'id')`,``)]}}else if(typeof i==`object`){let{isNamed:e,isInline:t}=a(i);switch(n){case`array`:case`set`:case`record`:case`enum`:if(!e&&!t)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires a single named reference or an inline descriptor`,``)];break;case`object`:if(!e)return[U(`SCHEMA_REF_FORM_INVALID`,`Object field requires a single named schema reference`,``)];break;case`union`:case`composite`:return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires an array of references`,``)]}}return[]},collection_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r)return[];let i=t.schema;return Un(n)&&i==null?[U(`COLLECTION_MISSING_SCHEMA`,`Collection type '${n}' must have a schema reference`,``)]:[]},union_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`union`)return[];let i=t.schema;return i==null?[U(`UNION_MISSING_SCHEMA`,`Union type must have schema references`,``)]:Array.isArray(i)?i.length<2?[U(`UNION_INSUFFICIENT_SCHEMAS`,`Union type must have at least 2 schema references, got ${i.length}`,``)]:[]:[U(`UNION_SCHEMA_NOT_ARRAY`,`Union type schema must be an array of schema references`,``)]},record_schema_cardinality:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`record`)return[];let i=t.schema;return Array.isArray(i)?[U(`RECORD_SCHEMA_ARRAY`,`Record type must have zero or one schema reference, not an array`,``)]:[]},nested_schema_exclusive_mode:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!1;for(let e of zn){let r=t[e];if(typeof r==`object`&&r&&Object.keys(r).length>0){n=!0;break}}let r=!1;for(let e of Bn)if(t[e]!=null){r=!0;break}return n&&r?[U(`NESTED_SCHEMA_MIXED_MODE`,`NestedSchema cannot mix BaseSchema fields (fields/indexes/constraints) with FieldProperties (type/values/schema)`,``)]:!n&&!r?[U(`NESTED_SCHEMA_NO_MODE`,`NestedSchema must have either BaseSchema fields or FieldProperties, not neither`,``)]:[]},constraint_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=String(t.name||`UNNAMED`),r=!!(t.predicate&&String(t.predicate)!==``),i=!!(t.operator&&Array.isArray(t.rules)&&t.rules.length>0);return r&&i?[U(`CONSTRAINT_MIXED_TYPE`,`Constraint cannot have both predicate (rule) and operator+rules (group)`,n)]:!r&&!i?[U(`CONSTRAINT_NO_TYPE`,`Constraint must have either predicate (rule) or operator+rules (group)`,n)]:[]},constraint_rule_requires_predicate:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.predicate;return n===void 0?[U(`REQUIRED_FIELD_MISSING`,`Required field 'predicate' is missing`,`predicate`)]:n===null?[U(`REQUIRED_FIELD_NULL`,`Required field 'predicate' cannot be null`,`predicate`)]:typeof n==`string`?n.trim()===``?[U(`REQUIRED_FIELD_EMPTY`,`Required field 'predicate' cannot be empty`,`predicate`)]:[]:[U(`REQUIRED_FIELD_WRONG_TYPE`,`Required field 'predicate' must be a string`,`predicate`)]},index_condition_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!!(t.field&&t.operator&&t.value!==void 0),r=!!(t.operator&&Array.isArray(t.conditions)&&t.conditions.length>0);return n&&r?[U(`INDEX_CONDITION_MIXED_TYPE`,`IndexCondition cannot have both single condition fields and group fields`,``)]:!n&&!r?[U(`INDEX_CONDITION_NO_TYPE`,`IndexCondition must be either a single condition or a group`,``)]:[]},schema_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[U(`SCHEMA_NAME_MISSING`,`Schema name is required`,`name`)]:[]},field_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[U(`FIELD_NAME_MISSING`,`Field name is required`,`name`)]:[]},index_fields_not_empty:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.fields;return!Array.isArray(n)||n.length===0?[U(`INDEX_FIELDS_EMPTY`,`Index must reference at least one field`,`fields`)]:[]},schema_reference_id_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.id;return n==null||String(n).trim()===``?[U(`SCHEMA_REFERENCE_ID_MISSING`,`SchemaReference ID is required`,`id`)]:[]},index_fields_reference_valid:e=>[]};var Zn=class{static validator=null;static async validate(e){return this.validator||=await Fn.create(In,Xn),await this.validator.validate(e)}};export{Nt as AnansiCodec,c as Compiler,u as DataTypes,Fn as DocumentValidator,f as FD_NO_CHILD,Ye as FLAG_COMPRESSED,Xe as FLAG_ENCRYPTED,Ze as FLAG_HASH_PRESENT,n as Literal,p as MAX_SCHEMA_SLOTS,m as MULTI_STEP_BASE,Jt as MigrationEngine,L as MigrationError,Vt as MigrationErrorCode,Zn as SchemaValidator,le as addressForSteps,l as buildEnum,me as buildManifest,Lt as bumpVersion,It as calculateNextBump,Ft as classifyChangeImpact,Mt as decodeAnansiBatch,Ot as decodeAnansiPacket,ft as decodeBatch,ct as decodeDocument,zt as deepMerge,Yt as defaultValidationConfig,jt as encodeAnansiBatchColumnar,At as encodeAnansiBatchRows,Dt as encodeAnansiPacket,ut as encodeBatchColumnar,lt as encodeBatchRows,st as encodeDocument,ee as internalDP,de as link,te as makeDescriptor,Xn as metaSchemaPredicateMap,r as parseSchema,Pt as schemaChangeToPatch,Bt as sha256,h as unpackDescriptor,ue as userDataDP};
|
|
1
|
+
import{blake3 as e}from"hash-wasm";import{decompress as t}from"fzstd";var n=class e{kind;value;constructor(e,t){this.kind=e,this.value=t}isZero(){return this.kind===`zero`}isNull(){return this.kind===`null`}static fromJSON(t){if(t===void 0)return new e(`zero`,void 0);if(t===null)return new e(`null`,void 0);if(typeof t==`string`)return new e(`string`,t);if(typeof t==`boolean`)return new e(`boolean`,t);if(typeof t==`number`)return Number.isInteger(t)?new e(`integer`,t):new e(`float`,t);if(Array.isArray(t))return new e(`array`,t);if(typeof t==`object`)return new e(`object`,t);throw Error(`anansi: unsupported literal ${typeof t}`)}};function r(e){let t=typeof e==`string`?JSON.parse(e):e;if(!t.name)throw Error(`anansi: schema requires a name`);return t}function i(e){return!!e&&!Array.isArray(e)&&typeof e.id==`string`}function a(e){return Array.isArray(e)}function o(e){return!!e&&!Array.isArray(e)&&typeof e.id!=`string`}function s(e,t){return e?`${e}.${t}`:t}var c=class{source;nested=new Map;building=new Set;constructor(e){this.source=e}compile(){for(let e of Object.keys(this.source.schemas??{}))this.compileNested(e);return{root:this.compileFields(this.source.fields??{},[],``),schemas:this.nested}}compileNested(e){let t=this.nested.get(e);if(t)return t;if(this.building.has(e))return null;let n=(this.source.schemas??{})[e];if(!n)throw Error(`anansi: schema '${e}' not found`);this.building.add(e);try{let t=n.values&&n.values.length>0?`enum`:n.type??`unknown`,r={id:e,name:n.name,effectiveType:t,fields:[],isRecursive:!1,values:n.values,enumDef:t===`enum`?l(n.values??[],n.type):void 0};return this.nested.set(e,r),n.fields&&(r.fields=this.compileFields(n.fields,[e],``)),r.isRecursive=r.fields.some(e=>e.kind.tag===`recursive`),r}finally{this.building.delete(e)}}compileFields(e,t,n){let r=[];for(let[i,a]of Object.entries(e))r.push(this.compileField(i,a,t,n));return r}compileField(e,t,r,c){let u=t.type??`unknown`,d={id:e,name:t.name,path:s(c,t.name),type:u,required:!!t.required,deprecated:!!t.deprecated,unique:!!t.unique,nullable:t.nullable!==!1,hasDefault:!n.fromJSON(t.default).isZero(),kind:{tag:`scalar`}},f=t.schema;switch(u){case`geometry`:break;case`record`:d.kind=i(f)?{tag:`container`,c:{itemSchema:this.resolve(f.id)??void 0,record:!0}}:{tag:`container`,c:{record:!0}};break;case`array`:if(a(f))throw Error(`anansi: ${d.path}: array cannot reference multiple schemas`);if(i(f))d.kind={tag:`container`,c:{itemSchema:this.require(f.id,d.path),record:!1}};else if(o(f)){let e=f.type??`unknown`,t={itemType:e,record:!1};(f.values?.length??0)>0&&(t.itemEnum=l(f.values,e)),d.kind={tag:`container`,c:t}}else d.kind={tag:`container`,c:{record:!1}};break;case`object`:d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:o(f)?{tag:`object`,schema:this.synthesizeInline(f,e,r)}:{tag:`scalar`};break;case`union`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`union`,variants:e}}else d.kind=i(f)?{tag:`object`,schema:this.require(f.id,d.path)}:{tag:`scalar`};break;case`composite`:if(a(f)){let e=[];for(let t of f)e.push(this.require(t.id,d.path));d.kind={tag:`composite`,parts:e}}else d.kind={tag:`scalar`};break;case`enum`:if((t.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(t.values,u)};else if(o(f)&&(f.values?.length??0)>0)d.kind={tag:`enum`,enumDef:l(f.values,f.type??`string`)};else if(i(f)){let e=this.resolve(f.id);d.kind=e&&e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}else d.kind={tag:`scalar`};break;default:if(i(f)){let e=this.resolve(f.id);d.kind=e===null?{tag:`recursive`,schemaId:f.id}:e.enumDef?{tag:`enum`,enumDef:e.enumDef}:{tag:`scalar`}}}return d}resolve(e){return this.nested.has(e)?this.nested.get(e):this.building.has(e)?null:this.compileNested(e)}require(e,t){let n=this.resolve(e);if(!n)throw Error(`anansi: ${t}: unresolvable schema reference '${e}'`);return n}synthesizeInline(e,t,n){let r=`inline:${[...n,t].join(`/`)}`,i=this.nested.get(r);if(i)return i;let a={id:r,name:r,effectiveType:e.type??`object`,fields:[],isRecursive:!1};this.nested.set(r,a);let o=e.fields??{};return a.fields=this.compileFields(o,[...n,r],``),a}};function l(e,t){let n=new Map,r=[];for(let t of e)t===null||[`string`,`number`,`boolean`].includes(typeof t)?n.set(JSON.stringify(t),t):r.push(t);return{lookup:n,complex:r,expectNumeric:t===`integer`||t===`number`}}const u={TypeUnknown:0,TypeInt:1,TypeFloat:2,TypeString:3,TypeBool:4,TypeBytes:5,TypeGeometry:6,TypeRecord:7,TypeArrayUnknown:8,TypeArrayInt:9,TypeArrayFloat:10,TypeArrayString:11,TypeArrayBool:12,TypeArrayBytes:13,TypeArrayObject:14,TypeArrayGeometry:15},d={[u.TypeUnknown]:`unknown`,[u.TypeInt]:`int`,[u.TypeFloat]:`float`,[u.TypeString]:`string`,[u.TypeBool]:`bool`,[u.TypeBytes]:`bytes`,[u.TypeGeometry]:`geometry`,[u.TypeRecord]:`record`,[u.TypeArrayUnknown]:`array_unknown`,[u.TypeArrayInt]:`array_int`,[u.TypeArrayFloat]:`array_float`,[u.TypeArrayString]:`array_string`,[u.TypeArrayBool]:`array_bool`,[u.TypeArrayBytes]:`array_bytes`,[u.TypeArrayObject]:`array_object`,[u.TypeArrayGeometry]:`array_geometry`},f=63,p=63,m=16384;function ee(e){return(e.raw&4294967264|(e.raw>>>28&15)<<1)>>>0}function te(e,t,n,r,i){let a=0;return a|=(e&15)<<28,a|=(n&63)<<22,a|=(r&127)<<15,a|=(i.child&63)<<9,a|=(t&3)<<7,i.required&&(a|=64),i.hasDefault&&(a|=32),i.deprecated&&(a|=16),i.unique&&(a|=8),i.terminal&&(a|=4),i.nullable&&(a|=2),i.recursive&&(a|=1),h(a>>>0)}function h(e){return{raw:e,dt:e>>>28&15,schemaIdx:e>>>22&63,fieldIdx:e>>>15&127,childSchemaIdx:e>>>9&63,kind:e>>>7&3,required:!!(e&64),hasDefault:!!(e&32),deprecated:!!(e&16),unique:!!(e&8),terminal:!!(e&4),nullable:!!(e&2),recursive:!!(e&1)}}function ne(e){switch(e){case`string`:return u.TypeString;case`number`:return u.TypeFloat;case`decimal`:return u.TypeString;case`integer`:return u.TypeInt;case`boolean`:return u.TypeBool;case`bytes`:return u.TypeBytes;default:return u.TypeUnknown}}function re(e){return e?.expectNumeric?u.TypeInt:u.TypeString}function ie(e,t){if(e)return u.TypeArrayObject;switch(t){case`string`:return u.TypeArrayString;case`number`:return u.TypeArrayFloat;case`decimal`:return u.TypeArrayString;case`integer`:return u.TypeArrayInt;case`boolean`:return u.TypeArrayBool;case`bytes`:return u.TypeArrayBytes;case`geometry`:return u.TypeArrayGeometry;case`unknown`:return u.TypeArrayUnknown;default:return u.TypeUnknown}}function ae(e){let t=e.kind;if(e.type===`geometry`)return[u.TypeGeometry,0,!0];if(t.tag===`scalar`)return[ne(e.type),0,!0];if(t.tag===`enum`)return[re(t.enumDef),0,!0];if(t.tag===`recursive`)return[u.TypeUnknown,1,!0];if(t.tag===`object`)return[u.TypeUnknown,1,!1];if(t.tag===`container`){let e=!t.c.itemSchema;return t.c.record?[u.TypeRecord,1,e]:[ie(t.c.itemSchema,t.c.itemType),2,e]}return t.tag===`union`?[u.TypeUnknown,3,!1]:[u.TypeUnknown,1,!1]}var oe=class{slots=[];descriptors=[];metas=[];localOffsets=[];fieldTypes=[];linkedSlots=[];assignSlot(e){if(this.slots.length>=63)throw Error(`anansi: exceeds maximum of 63 nested schemas while assigning '${e.name}'`);let t=this.slots.length;return this.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),this.linkedSlots.push({idx:t,slot:this.slots[t],fields:[]}),t}childSlotForField(e,t){let n=e.kind;return n.tag===`recursive`?{idx:t,kindTag:n}:n.tag===`object`?{idx:this.assignSlot(n.schema),kindTag:n}:n.tag===`composite`?{idx:this.assignSlot({id:`composite:${e.path}`,name:`composite:${e.name}`,effectiveType:`object`,fields:se(n.parts),isRecursive:!1}),kindTag:n}:n.tag===`container`&&n.c.itemSchema?{idx:this.assignSlot(n.c.itemSchema),kindTag:n}:null}linkFields(e,t){let n=this.linkedSlots[t],r=[];for(let i=0;i<e.length;i++){if(e.length>128)throw Error(`anansi: slot ${t} exceeds 128 fields`);let a=e[i],[o,s,c]=ae(a),l=this.childSlotForField(a,t),u=l?l.idx:63,d=a.kind.tag===`recursive`,f=te(o,s,t,i,{required:a.required,hasDefault:a.hasDefault,deprecated:a.deprecated,unique:a.unique,terminal:c,nullable:a.nullable,recursive:d,child:u}),p=this.descriptors.length;this.descriptors.push(f),this.fieldTypes.push(a.type);let m={meta:{name:a.name,path:a.path},fd:f,dp:0,abs:p};n.fields.push(m),this.metas.push(m.meta),!c&&u!==63&&r.push({rf:a,fd:f,childIdx:u})}for(let e of r){let t=e.rf.kind;if(t.tag===`object`||t.tag===`composite`){let n=t.tag===`object`?t.schema.fields:se(t.parts),r=this.linkFields(n,e.childIdx);this.setSlot(e.childIdx,r.count)}else if(t.tag===`container`&&t.c.itemSchema){let n=this.linkFields(t.c.itemSchema.fields,e.childIdx);this.setSlot(e.childIdx,n.count)}let r=n.fields.find(t=>t.fd===e.fd);r.child=this.linkedSlots[e.childIdx]}return{count:e.length,slot:n}}setSlot(e,t){let n=this.slots[e];n.fieldStart===-1&&(n.fieldStart=this.descriptors.length-t),n.fieldCount=t}};function se(e){let t=[];for(let n of e)t.push(...n.fields);return t}function ce(e){for(let t=e.slots.length-1;t>=0;t--){let n=e.slots[t];n.fieldCount<0&&(n.fieldStart=0,n.fieldCount=0);let r=0;for(let t=0;t<n.fieldCount;t++){let i=n.fieldStart+t,a=e.descriptors[i];a.terminal?r+=1:a.childSchemaIdx!==63&&(r+=e.slots[a.childSchemaIdx].footprint)}n.footprint=r}for(let t of e.slots){let n=0;for(let r=0;r<t.fieldCount;r++){let i=t.fieldStart+r,a=e.descriptors[i];e.localOffsets[i]=n,a.terminal?n+=1:a.childSchemaIdx!==63&&(n+=e.slots[a.childSchemaIdx].footprint)}}}function le(e,t,n,r){if(r.length===0)return 0;if(r.length===1){let[n,i]=r[0],a=e[n].fieldStart+i;return t[a].terminal?a+1>>>0:0}let i=m;for(let a=0;a<r.length;a++){let[o,s]=r[a],c=e[o].fieldStart+s,l=t[c];if(i+=n[c],a===r.length-1)return l.terminal?i>>>0:0;if(l.terminal)return 0}return i>>>0}function ue(e,t){return(t<<5|e<<1)>>>0}function de(e){let{root:t,schemas:n}=new c(e).compile(),r=new oe;r.slots.push({fieldStart:-1,fieldCount:-1,footprint:-1}),r.linkedSlots.push({idx:0,slot:r.slots[0],fields:[]});let i=r.linkFields(t,0);return r.slots[0].fieldStart=0,r.slots[0].fieldCount=i.count,ce(r),pe(r,r.linkedSlots[0],[]),{slots:r.slots,metas:r.metas,descriptors:r.descriptors,localOffsets:r.localOffsets,fieldTypes:r.fieldTypes,root:r.linkedSlots[0],childrenByPath:fe(r.linkedSlots[0])}}function fe(e){let t=new Map;for(let n of e.fields)n.child&&t.set(n.meta.path,n.child);return t}function pe(e,t,n){for(let r=0;r<t.slot.fieldCount;r++){let i=t.slot.fieldStart+r,a=e.descriptors[i],o=t.fields[r],s=[...n,[t.idx,r]],c=le(e.slots,e.descriptors,e.localOffsets,s);o.dp=c===0?ee(a):ue(a.dt,c),!a.terminal&&o.child&&pe(e,o.child,s)}}function me(e){let t=(e,n,r)=>{let i=[];for(let a=0;a<e.slot.fieldCount;a++){let o=e.fields[a],s=o.fd,c=[...n,[e.idx,a]],l=r?`${r}.${o.meta.name}`:o.meta.name,f={name:o.meta.name,path:l,t:d[s.dt],dp:o.dp};if(!s.terminal&&o.child){s.dt===u.TypeArrayObject?(f.child=t(o.child,c,l),i.push(f)):i.push(...t(o.child,c,l));continue}i.push(f)}return i};return t(e.root,[],``)}function g(e,t){for(t>>>=0;t>=128;)e.push(t&127|128),t>>>=7;e.push(t)}function he(e,t){if(!Number.isSafeInteger(t))throw Error(`anansi: integer ${t} exceeds Number.MAX_SAFE_INTEGER`);_e(e,BigInt(t))}function ge(e,t){for(;t>=128n;)e.push(Number(t&127n)|128),t>>=7n;e.push(Number(t))}function _e(e,t){ge(e,t<<1n^t>>63n)}var _=class{data;pos;constructor(e,t=0){this.data=e,this.pos=t}eof(){return this.pos>=this.data.length}remaining(){return this.data.length-this.pos}byte(){if(this.eof())throw Error(`anansi: unexpected end of buffer`);return this.data[this.pos++]}take(e){if(e<0||this.pos+e>this.data.length)throw Error(`anansi: unexpected end of buffer (need ${e}, have ${this.remaining()})`);let t=this.data.subarray(this.pos,this.pos+e);return this.pos+=e,t}uvarint(){let e=0,t=0;for(let n=0;n<10;n++){let r=this.byte();if(r<128){if(n===9&&r>1)throw Error(`anansi: varint overflow`);return(e|r<<t)>>>0}e|=(r&127)<<t,t+=7}throw Error(`anansi: varint overflow`)}uvarintBig(){let e=0n,t=0n,n=0;for(;n<10;){let r=this.byte();if(e|=BigInt(r&127)<<t,r<128)return e;t+=7n,n++}throw Error(`anansi: varint overflow`)}varint(){let e=this.uvarintBig(),t=e>>1n^-(e&1n),n=Number(t);if(!Number.isSafeInteger(n))throw Error(`anansi: decoded integer ${n} exceeds Number.MAX_SAFE_INTEGER`);return n}};const v={Unknown:0,Int:1,Float:2,String:3,Bool:4,Bytes:5,Geometry:6,Record:7,ArrayUnknown:8,ArrayInt:9,ArrayFloat:10,ArrayString:11,ArrayBool:12,ArrayBytes:13,ArrayObject:14,ArrayGeometry:15},ve=new TextEncoder,ye=new TextDecoder;function be(e){let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.subarray(r,r+n));return btoa(t)}function xe(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function Se(e,t){he(e,t)}function Ce(e){return e.varint()}function y(e,t){let n=new DataView(new ArrayBuffer(8));n.setFloat64(0,t,!0);for(let t=0;t<8;t++)e.push(n.getUint8(t))}function b(e){let t=e.take(8);return new DataView(t.buffer,t.byteOffset,8).getFloat64(0,!0)}function x(e,t){let n=ve.encode(t);g(e,n.length);for(let t of n)e.push(t)}function S(e){let t=e.uvarint();return ye.decode(e.take(t))}function we(e,t){e.push(+!!t)}function Te(e){return e.byte()!==0}function C(e,t){let n=new Uint8Array(t.length+7>>3);t.forEach((e,t)=>{e&&(n[t>>3]|=1<<(t&7))});for(let t of n)e.push(t)}function w(e,t){let n=e.take(t+7>>3),r=[];for(let e=0;e<t;e++)r.push(!!(n[e>>3]&1<<(e&7)));return r}function Ee(e,t){let n=xe(t);g(e,n.length);for(let t of n)e.push(t)}function De(e){let t=e.uvarint();return be(e.take(t))}function Oe(e,t){g(e,t.length);for(let n of t){g(e,n.length/2);for(let t=0;t<n.length;t+=2)y(e,n[t]),y(e,n[t+1])}}function ke(e){let t=e.uvarint(),n=[];for(let r=0;r<t;r++){let t=e.uvarint(),r=[];for(let n=0;n<t;n++)r.push(b(e),b(e));n.push(r)}return n}const T={Null:0,Int:v.Int,Float:v.Float,String:v.String,Bool:v.Bool,Bytes:v.Bytes,Geometry:v.Geometry,Record:v.Record,ArrUnknown:v.ArrayUnknown,ArrInt:v.ArrayInt,ArrFloat:v.ArrayFloat,ArrString:v.ArrayString,ArrBool:v.ArrayBool,ArrBytes:v.ArrayBytes};function Ae(e){return e.length>0&&Array.isArray(e[0])&&Array.isArray(e[0][0])}function je(e,t){if(t==null){e.push(T.Null);return}switch(typeof t){case`boolean`:e.push(T.Bool),we(e,t);return;case`string`:e.push(T.String),x(e,t);return;case`number`:Number.isSafeInteger(t)?(e.push(T.Int),Se(e,t)):(e.push(T.Float),y(e,t));return;case`object`:if(Array.isArray(t)){if(Ae(t)){e.push(T.Geometry),Oe(e,t);return}if(t.every(e=>typeof e==`string`)){e.push(T.ArrString),g(e,t.length);for(let n of t)x(e,n);return}if(t.every(e=>Number.isSafeInteger(e))){e.push(T.ArrInt),g(e,t.length);for(let n of t)Se(e,n);return}if(t.every(e=>typeof e==`number`)){e.push(T.ArrFloat),g(e,t.length);for(let n of t)y(e,n);return}if(t.every(e=>typeof e==`boolean`)){e.push(T.ArrBool);let n=t;g(e,n.length),C(e,n);return}e.push(T.ArrUnknown),g(e,t.length);for(let n of t)je(e,n);return}e.push(T.Record),Ne(e,t);return;default:throw Error(`anansi: unsupported any value ${typeof t}`)}}function Me(e){let t=e.byte();switch(t){case T.Null:return null;case T.Bool:return Te(e);case T.String:return S(e);case T.Int:return Ce(e);case T.Float:return b(e);case T.Bytes:return De(e);case T.Geometry:return ke(e);case T.Record:return Pe(e);case T.ArrInt:{let t=e.uvarint();return Array.from({length:t},()=>Ce(e))}case T.ArrFloat:{let t=e.uvarint();return Array.from({length:t},()=>b(e))}case T.ArrString:{let t=e.uvarint();return Array.from({length:t},()=>S(e))}case T.ArrBool:return w(e,e.uvarint());case T.ArrUnknown:{let t=e.uvarint();return Array.from({length:t},()=>Me(e))}default:throw Error(`anansi: unknown any tag ${t}`)}}function Ne(e,t){let n=Object.keys(t).sort();g(e,n.length);for(let r of n)x(e,r),je(e,t[r])}function Pe(e){let t=e.uvarint(),n={};for(let r=0;r<t;r++){let t=S(e);n[t]=Me(e)}return n}function Fe(e,t){g(e,t.length);for(let n of t)Se(e,n)}function Ie(e){let t=e.uvarint();return Array.from({length:t},()=>Ce(e))}function Le(e,t){g(e,t.length);for(let n of t)y(e,n)}function Re(e){let t=e.uvarint();return Array.from({length:t},()=>b(e))}function ze(e,t){g(e,t.length);for(let n of t)x(e,n)}function Be(e){let t=e.uvarint();return Array.from({length:t},()=>S(e))}function Ve(e,t){g(e,t.length),C(e,t)}function He(e){return w(e,e.uvarint())}function Ue(e,t){g(e,t.length);for(let n of t)Ee(e,n)}function We(e){let t=e.uvarint();return Array.from({length:t},()=>De(e))}function Ge(e,t){g(e,t.length);for(let n of t)Oe(e,n)}function Ke(e){let t=e.uvarint();return Array.from({length:t},()=>ke(e))}function qe(e,t){g(e,t.length);for(let n of t)je(e,n)}function Je(e){let t=e.uvarint();return Array.from({length:t},()=>Me(e))}const E={Dense:0,Sparse:1,Batch:2,Stream:3},Ye=4,Xe=64,Ze=128;function D(e,t){let n=t.split(`.`),r=e;for(let e=0;e<n.length;e++){if(typeof r!=`object`||!r)return[!1,void 0];let t=r;if(!(n[e]in t))return[!1,void 0];r=t[n[e]]}return r===void 0?[!1,void 0]:[!0,r]}function O(e,t){let[n,r]=D(e,t.path);return!n||r===void 0?`notSet`:r===null?`null`:`value`}function Qe(e,t){let n=t.length+1;return{...e,path:e.path.slice(n)}}function $e(e,t){return e.map(e=>Qe(e,t))}function et(e,t,n){switch(t.t){case`int`:Se(e,n);break;case`float`:y(e,n);break;case`string`:x(e,n);break;case`bool`:we(e,n);break;case`bytes`:Ee(e,n);break;case`geometry`:Oe(e,n);break;case`record`:Ne(e,n);break;case`unknown`:je(e,n);break;case`array_int`:Fe(e,n);break;case`array_float`:Le(e,n);break;case`array_string`:ze(e,n);break;case`array_bool`:Ve(e,n);break;case`array_bytes`:Ue(e,n);break;case`array_geometry`:Ge(e,n);break;case`array_unknown`:qe(e,n);break;default:throw Error(`anansi: cannot write value for type ${t.t} field ${t.path}`)}}function tt(e,t){switch(t.t){case`int`:return e.varint();case`float`:return b(e);case`string`:return S(e);case`bool`:return Te(e);case`bytes`:return De(e);case`geometry`:return ke(e);case`record`:return Pe(e);case`unknown`:return Me(e);case`array_int`:return Ie(e);case`array_float`:return Re(e);case`array_string`:return Be(e);case`array_bool`:return He(e);case`array_bytes`:return We(e);case`array_geometry`:return Ke(e);case`array_unknown`:return Je(e);default:throw Error(`anansi: cannot read value for type ${t.t} field ${t.path}`)}}function nt(e,t,n,r,i){let a=$e(t,n);g(e,r.length);for(let t of r){let n=0;for(let e of a)O(t,e)!==`notSet`&&n++;let r=a.length>0?n/a.length:1,o=!(a.length<=64||r>.25),s=o?E.Sparse:E.Dense,c=(i.flags&-200|s)>>>0,l=[],u={flags:i.flags&-4|s,version:i.version};o?j(l,a,t,u):k(l,a,t,u);let d=[c,i.version,...l];g(e,d.length);for(let t of d)e.push(t)}}function rt(e,t,n,r,i){let a=e.uvarint();for(let o=0;o<a;o++){let a=e.uvarint(),o=new _(e.take(a)),s=o.byte();o.byte();let c=i?i():{},l=s&3;if(l===E.Dense)A(o,t,c,n);else if(l===E.Sparse)M(o,t,c,n);else throw Error(`anansi: unsupported nested packet type ${l}`);r.push(c)}}const it={flags:E.Dense,version:0};function k(e,t,n,r=it){let i=2*t.length,a=new Uint8Array(i+7>>3);t.forEach((e,t)=>{let r=O(n,e)===`notSet`?0:O(n,e)===`null`?1:2;a[t>>2]|=r<<((t&3)<<1)});for(let t of a)e.push(t);let o=P(t);for(let t=0;t<=15;t++){let i=o[t];if(!(!i||i.length===0)){if(t===v.Bool){let t=[];for(let e of i)O(n,e)===`value`&&t.push(D(n,e.path)[1]);C(e,t);continue}for(let t of i)if(O(n,t)===`value`){if(t.t===`array_object`){nt(e,t.child,t.name,D(n,t.path)[1]??[],r);continue}et(e,t,D(n,t.path)[1])}}}}function A(e,t,n,r){let i=[],a=2*t.length,o=e.take(a+7>>3);for(let e=0;e<t.length;e++)i.push(o[e>>2]>>((e&3)<<1)&3);let s=P(t);for(let a=0;a<=15;a++){let o=s[a];if(o){if(a===v.Bool){let a=o.filter((e,n)=>i[t.indexOf(o[n])]===2).length,s=w(e,a),c=0;o.forEach(e=>{let a=t.indexOf(e);i[a]===2&&N(n,e.path,r,s[c++])});continue}for(let a of o){let o=i[t.indexOf(a)];if(o!==0){if(o===1){N(n,a.path,r,null);continue}if(a.t===`array_object`){let t=[];rt(e,a.child,a.name,t),N(n,a.path,r,t);continue}N(n,a.path,r,tt(e,a))}}}}}function j(e,t,n,r={flags:E.Sparse,version:0}){let i=t.filter(e=>O(n,e)!==`notSet`);g(e,i.length);for(let t of i){let i=O(n,t),a=t.dp;i===`null`?a|=1:a&=4294967294,g(e,a),i===`value`&&(t.t===`array_object`?nt(e,t.child,t.name,D(n,t.path)[1]??[],r):et(e,t,D(n,t.path)[1]))}}function M(e,t,n,r){let i=new Map;for(let e of t)i.set(e.dp&4294967294,e);let a=e.uvarint();for(let t=0;t<a;t++){let t=e.uvarint(),a=!!(t&1),o=i.get(t&4294967294);if(!o)throw Error(`anansi: sparse packet references unknown data point ${t}`);if(a){N(n,o.path,r,null);continue}if(o.t===`array_object`){let t=[];rt(e,o.child,o.name,t),N(n,o.path,r,t);continue}N(n,o.path,r,tt(e,o))}}function N(e,t,n,r){let i=t;if(n&&i.startsWith(n+`.`)){i=i.slice(n.length+1),at(e,i.split(`.`),r);return}at(e,i.split(`.`),r)}function at(e,t,n){let r=e;for(let e=0;e<t.length-1;e++){let n=t[e];(typeof r[n]!=`object`||r[n]===null)&&(r[n]={}),r=r[n]}r[t[t.length-1]]=n}function P(e){let t=[];for(let n of e){let e=ot[n.t]??v.Unknown;(t[e]??=[]).push(n)}return t}const ot=Object.fromEntries(Object.entries({unknown:v.Unknown,int:v.Int,float:v.Float,string:v.String,bool:v.Bool,bytes:v.Bytes,geometry:v.Geometry,record:v.Record,array_unknown:v.ArrayUnknown,array_int:v.ArrayInt,array_float:v.ArrayFloat,array_string:v.ArrayString,array_bool:v.ArrayBool,array_bytes:v.ArrayBytes,array_object:v.ArrayObject,array_geometry:v.ArrayGeometry}));function F(e){if(e<0||e>1023)throw Error(`anansi: schema version ${e} exceeds maximum of 1023`);return{flags:e>>8<<4,version:e&255}}function I(e,t){return(e>>4&3)<<8|t}function st(e,t,n=0,r=`auto`){let i=F(n),a=0;for(let n of e)O(t,n)!==`notSet`&&a++;let o=e.length?a/e.length:1,s=r===`sparse`||r===`auto`&&!(e.length<=64||o>.25),c=i.flags|(s?E.Sparse:E.Dense),l=[c,i.version],u={flags:c,version:i.version},d=[];return s?j(d,e,t,u):k(d,e,t,u),new Uint8Array([...l,...d])}function ct(e,t){let n=new _(e),r=n.byte(),i=n.byte();if((r>>4&3)===E.Stream)throw Error(`anansi: stream packets unsupported`);if(r&196)throw Error(`anansi: compressed/encrypted/hashed packets require transform support not enabled in this build`);let a=r&3,o={};if(a===E.Dense)A(n,t,o);else if(a===E.Sparse)M(n,t,o);else throw Error(`anansi: use batch API for packet type ${a}`);return{version:I(r,i),doc:o}}function lt(e,t,n=0){let r=F(n),i=0,a=0;for(let n of t){for(let t of e)O(n,t)!==`notSet`&&i++;a+=e.length}let o=a?i/a:1,s=e.length>64&&o<=.25,c=[r.flags|E.Batch,r.version],l=[];g(l,t.length),l.push(s?2:0);let u={flags:r.flags|E.Batch,version:r.version};for(let n of t)s?j(l,e,n,u):k(l,e,n,u);return new Uint8Array([...c,...l])}function ut(e,t,n=0){let r=F(n),i=[r.flags|E.Batch|8,r.version],a=dt(e,t,{flags:i[0],version:r.version});return new Uint8Array([...i,...a])}function dt(e,t,n){let r=[];g(r,t.length),r.push(1);let i=P(e);for(let e=0;e<=15;e++){let a=i[e];if(!a||a.length===0)continue;let o=2*a.length*t.length,s=new Uint8Array(o+7>>3),c=0;for(let e of a)for(let n of t){let t=O(n,e),r=t===`notSet`?0:t===`null`?1:2;s[c>>3]|=r<<(c&7),c+=2}for(let e of s)r.push(e);if(e===v.Bool){for(let e of a){let n=[];for(let r of t)O(r,e)===`value`&&n.push(D(r,e.path)[1]);C(r,n)}continue}for(let e of a)for(let i of t)if(O(i,e)===`value`){if(e.t===`array_object`){nt(r,e.child,e.name,D(i,e.path)[1]??[],n);continue}et(r,e,D(i,e.path)[1])}}return r}function ft(e,t){let n=new _(e),r=n.byte(),i=n.byte();if((r&3)!==E.Batch)throw Error(`anansi: not a batch packet`);if(r&196)throw Error(`anansi: transformed batch packets unsupported in this build`);let a=n.uvarint(),o=n.byte(),s=Array.from({length:a},()=>({}));if(o&1||r&8)pt(n,t,s);else{let e=!!(o&2);for(let r of s)e?M(n,t,r):A(n,t,r)}return{version:I(r,i),docs:s}}function pt(e,t,n){let r=P(t);for(let t=0;t<=15;t++){let i=r[t];if(!i||i.length===0)continue;let a=2*i.length*n.length,o=e.take(a+7>>3),s=[],c=0;for(let e=0;e<i.length;e++){let e=[];for(let t=0;t<n.length;t++)e.push(o[c>>3]>>(c&7)&3),c+=2;s.push(e)}if(t===v.Bool){i.forEach((t,r)=>{let i=s[r].filter(e=>e===2).length,a=w(e,i),o=0;s[r].forEach((e,r)=>{if(e!==0){if(e===1){N(n[r],t.path,void 0,null);return}N(n[r],t.path,void 0,a[o++])}})});continue}i.forEach((t,r)=>{s[r].forEach((r,i)=>{if(r!==0){if(r===1){N(n[i],t.path,void 0,null);return}if(t.t===`array_object`){let r=[];rt(e,t.child,t.name,r),N(n[i],t.path,void 0,r);return}N(n[i],t.path,void 0,tt(e,t))}})})}}const mt=e=>e&3;let ht,gt=!1;function _t(){if(!gt){gt=!0;try{ht=globalThis.process?.getBuiltinModule?.(`node:zlib`)??void 0}catch{ht=void 0}}return ht}function vt(e){let t=_t()?.zstdCompressSync?.(e);if(!t)throw Error(`anansi: zstd compression is unavailable in this runtime (no node:zlib). Send plain packets, or provide a WASM compressor.`);return t}function yt(e,n){return _t()?.zstdDecompressSync?.(e)||t(e)}async function bt(t){let n=await e(t,128),r=new Uint8Array(16);for(let e=0;e<16;e++)r[e]=parseInt(n.slice(e*2,e*2+2),16);return r}async function xt(e){if(e.length!==32)throw Error(`anansi: AES-256-GCM requires a 32-byte key, got ${e.length}`);return crypto.subtle.importKey(`raw`,e,`AES-GCM`,!1,[`encrypt`,`decrypt`])}async function St(e,t,n,r){let i=e;r.integrity&&(i|=128),r.compression&&(i|=4),r.encryptionKey&&(i|=64);let a=[i,t];if(r.integrity)for(let e of await bt(n))a.push(e);let o=[];if(r.compression){g(o,n.length);for(let e of vt(n))o.push(e)}else for(let e of n)o.push(e);if(!r.encryptionKey)return new Uint8Array([...a,...o]);let s=await xt(r.encryptionKey),c=crypto.getRandomValues(new Uint8Array(12)),l=new Uint8Array(await crypto.subtle.encrypt({name:`AES-GCM`,iv:c},s,new Uint8Array(o))),u=new Uint8Array(a.length+12+l.length);return u.set(Uint8Array.from(a),0),u.set(c,a.length),u.set(l,a.length+12),u}async function Ct(e,t,n){let r;t&128&&(r=e.take(16));let i=e.data.subarray(e.pos);if(t&64){if(!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);if(i.length<12)throw Error(`anansi: truncated nonce`);let e=i.subarray(0,12);i=i.subarray(12);let t=await xt(n.decryptionKey),r=await crypto.subtle.decrypt({name:`AES-GCM`,iv:e},t,i);i=new Uint8Array(r)}let a=i.length,o=new _(i);if(t&4){if(a=o.uvarint(),a>536870912)throw Error(`anansi: declared uncompressed size ${a} exceeds limit`);let e=yt(i.subarray(o.pos),a);if(e.length!==a)throw Error(`anansi: decompressed ${e.length} bytes, header declared ${a}`);i=e}if(r){let e=await bt(i);for(let t=0;t<16;t++)if(r[t]!==e[t])throw Error(`anansi: integrity check failed (packet digest mismatch)`)}return new _(i)}function wt(e,t){let n=0;for(let r of e){let[e,i]=Tt(t,r.path);e&&i!=null&&n++}return e.length<=64||e.length===0||n/e.length>.25?`dense`:`sparse`}function Tt(e,t){let n=e;for(let e of t.split(`.`)){if(typeof n!=`object`||!n)return[!1,void 0];let t=n;if(!(e in t))return[!1,void 0];n=t[e]}return[n!==void 0,n]}function Et(e,t,n){let r=n===`sparse`||n===`auto`&&wt(e,t)===`sparse`,i=[];return r?j(i,e,t):k(i,e,t),new Uint8Array(i)}async function Dt(e,t,n=0,r={}){let i=F(n),a=r.kind??`auto`,o=a===`sparse`||a===`auto`&&wt(e,t)===`sparse`,s=i.flags|(o?E.Sparse:E.Dense),c=Et(e,t,o?`sparse`:`dense`);return St(s,i.version,c,r)}async function Ot(e,t,n={}){let r=new _(e),i=r.byte(),a=r.byte();if(i&64&&!n.decryptionKey)throw Error(`anansi: encrypted packet but no decryption key provided`);let o=await Ct(r,i,n),s={},c=mt(i);if(c===E.Dense)A(o,t,s);else if(c===E.Sparse)M(o,t,s);else throw Error(`anansi: use batch API for packet type ${c}`);return{version:I(i,a),doc:s}}function kt(e,t){let n=0,r=0;for(let i of t)for(let t of e){let[e,a]=Tt(i,t.path);e&&a!=null&&n++,r++}let i=r>0?n/r:1,a=e.length>64&&i<=.25,o=[];g(o,t.length),o.push(a?2:0);for(let n of t)a?j(o,e,n):k(o,e,n);return{body:new Uint8Array(o),sparse:a}}async function At(e,t,n=0,r={}){let i=F(n),{body:a,sparse:o}=kt(e,t);return St(i.flags|E.Batch|(o?2:0),i.version,a,r)}async function jt(e,t,n=0,r={}){let i=F(n),a=i.flags|E.Batch|8,o=new Uint8Array(dt(e,t,{flags:a,version:i.version}));return St(a,i.version,o,r)}async function Mt(e,t,n={}){let r=new _(e),i=r.byte(),a=r.byte();if(mt(i)!==E.Batch)throw Error(`anansi: not a batch packet`);let o=await Ct(r,i,n),s=o.uvarint(),c=o.byte(),l=Array.from({length:s},()=>({}));if(c&1||i&8)pt(o,t,l);else for(let e of l)c&2?M(o,t,e):A(o,t,e);return{version:I(i,a),docs:l}}var Nt=class e{linked;fields;fullVersion;kind;enc;dec;constructor(e,t,n,r,i,a){this.linked=e,this.fields=t,this.fullVersion=n,this.kind=r,this.enc=i,this.dec=a}static async create(t,n={}){let i=de(r(t));return new e(i,me(i),n.fullVersion??0,n.kind??`auto`,{compression:n.compression,integrity:n.integrity,encryptionKey:n.encryptionKey},{decryptionKey:n.decryptionKey})}async encode(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?Dt(this.fields,e,this.fullVersion,{...this.enc,kind:this.kind}):st(this.fields,e,this.fullVersion,this.kind)}async decode(e){return Ot(e,this.fields,this.dec)}async encodeBatch(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?At(this.fields,e,this.fullVersion,this.enc):lt(this.fields,e,this.fullVersion)}async decodeBatch(e){return Mt(e,this.fields,this.dec)}async encodeColumnar(e){return this.enc.compression||this.enc.integrity||this.enc.encryptionKey?jt(this.fields,e,this.fullVersion,this.enc):jt(this.fields,e,this.fullVersion,{})}};function Pt(e,t){let n=[];switch(e.type){case`modifyProperty`:n.push({op:`replace`,path:`/${e.id}`,value:e.value});break;case`addField`:n.push({op:`add`,path:`/fields/${e.id}`,value:e.definition});break;case`removeField`:n.push({op:`remove`,path:`/fields/${e.id}`});break;case`modifyField`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/fields/${e.id}/${t}`,value:r});break;case`addIndex`:t.indexes||n.push({op:`add`,path:`/indexes`,value:{}}),n.push({op:`add`,path:`/indexes/${e.id}`,value:e.definition});break;case`removeIndex`:n.push({op:`remove`,path:`/indexes/${e.id}`});break;case`modifyIndex`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/indexes/${e.id}/${t}`,value:r});break;case`addConstraint`:t.constraints||n.push({op:`add`,path:`/constraints`,value:{}}),n.push({op:`add`,path:`/constraints/${e.id}`,value:e.constraint});break;case`removeConstraint`:n.push({op:`remove`,path:`/constraints/${e.id}`});break;case`modifyConstraint`:for(let[t,r]of Object.entries(e.changes))n.push({op:`replace`,path:`/constraints/${e.id}/${t}`,value:r});break;case`addSchema`:t.schemas||n.push({op:`add`,path:`/schemas`,value:{}}),n.push({op:`add`,path:`/schemas/${e.id}`,value:e.definition});break;case`removeSchema`:n.push({op:`remove`,path:`/schemas/${e.id}`});break;case`modifySchema`:for(let r of e.changes)n.push(...Pt(r,t))}return n}function Ft(e,t){switch(e.type){case`removeField`:case`removeIndex`:case`removeConstraint`:return`major`;case`modifyField`:{let t=e.changes;return t.required===!0||t.type!==void 0||t.schema!==void 0||t.unique===!0?`major`:t.deprecated?`minor`:`patch`}case`modifyIndex`:{let t=e.changes;return t.unique!==void 0||t.fields!==void 0?`major`:`minor`}case`modifyConstraint`:{let n=t?.constraints?.[e.id];return n&&`predicate`in e.changes&&e.changes.predicate!==void 0||n&&`parameters`in e.changes&&e.changes.parameters!==void 0?`major`:`minor`}case`addField`:case`addIndex`:case`addConstraint`:case`addSchema`:case`deprecateField`:return`minor`;case`modifySchema`:{let n=`patch`;for(let r of e.changes){let e=Ft(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}default:return`patch`}}function It(e,t){let n=`patch`;for(let r of e){let e=Ft(r,t);if(e===`major`)return`major`;e===`minor`&&(n=`minor`)}return n}function Lt(e,t){let n=e.replace(/^v/,``).match(/^(\d+)\.(\d+)\.(\d+)/);if(!n)throw Error(`anansi: invalid version format "${e}"`);let[,r,i,a]=n.map(Number);switch(t){case`major`:r++,i=0,a=0;break;case`minor`:i++,a=0;break;case`patch`:a++}return`${r}.${i}.${a}`}function Rt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function zt(e,t){let n={...e};for(let e of Object.keys(t)){let r=t[e];Rt(r)&&Rt(n[e])?n[e]=zt(n[e],r):r!==void 0&&(n[e]=r)}return n}async function Bt(e){if(typeof globalThis.crypto?.subtle?.digest==`function`){let t=new TextEncoder().encode(e),n=await globalThis.crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}let{createHash:t}=await import(`node:crypto`);return t(`sha256`).update(e).digest(`hex`)}let Vt=function(e){return e.INVALID_SCHEMA=`INVALID_SCHEMA`,e.INVALID_MIGRATION=`INVALID_MIGRATION`,e.CHECKSUM_MISMATCH=`CHECKSUM_MISMATCH`,e.TIMEOUT=`TIMEOUT`,e.MEMORY_LIMIT=`MEMORY_LIMIT`,e.CONCURRENT_OPERATION=`CONCURRENT_OPERATION`,e.TRANSFORM_ERROR=`TRANSFORM_ERROR`,e.VERSION_NOT_FOUND=`VERSION_NOT_FOUND`,e.CIRCULAR_DEPENDENCY=`CIRCULAR_DEPENDENCY`,e.STREAM_ERROR=`STREAM_ERROR`,e.ROLLBACK_ERROR=`ROLLBACK_ERROR`,e.MISSING_TRANSFORM=`MISSING_TRANSFORM`,e}({});var L=class extends Error{code;migrationId;cause;constructor(e,t,n,r){super(e),this.code=t,this.migrationId=n,this.cause=r,this.name=`MigrationError`}};function Ht(e){return e===``||e===`/`?[]:e.slice(1).split(`/`).map(e=>e.replace(/~1/g,`/`).replace(/~0/g,`~`))}function R(e,t){let n=e;for(let e=0;e<t.length-1;e++){let r=t[e];if(typeof n!=`object`||!n)throw Error(`anansi/engine: path "${r}" not found`);n=Array.isArray(n)?n[parseInt(r,10)]:n[r]}return{parent:n,key:t[t.length-1]}}function Ut(e,t){let n=JSON.parse(JSON.stringify(e));for(let e of t){let t=Ht(e.path);switch(e.op){case`add`:{if(t.length===0){n=e.value;break}let{parent:r,key:i}=R(n,t);if(Array.isArray(r)){let t=i===`-`?r.length:parseInt(i,10);r.splice(t,0,e.value)}else r[i]=e.value;break}case`remove`:{let{parent:e,key:r}=R(n,t);Array.isArray(e)?e.splice(parseInt(r,10),1):delete e[r];break}case`replace`:{let{parent:r,key:i}=R(n,t);r[i]=e.value;break}case`move`:{let t=Ht(e.from),r=R(n,t),i=Array.isArray(r.parent)?r.parent.splice(parseInt(r.key,10),1)[0]:(()=>{let e=r.parent[r.key];return delete r.parent[r.key],e})(),a=Ht(e.path),o=R(n,a);Array.isArray(o.parent)?o.parent.splice(parseInt(o.key,10),0,i):o.parent[o.key]=i;break}case`test`:{let{parent:r,key:i}=R(n,t);if(JSON.stringify(r[i])!==JSON.stringify(e.value))throw Error(`anansi/engine: test failed at ${e.path}`);break}}}return n}async function Wt(e){return Bt(JSON.stringify({id:e.id,schemaVersion:e.schemaVersion,changes:e.changes,description:e.description,rollback:e.rollback,createdAt:e.createdAt}))}async function Gt(e,t){return e.transform?typeof e.transform==`string`?e.transform.startsWith(`http://`)||e.transform.startsWith(`https://`)?Kt(e.transform,t):qt(e.transform,t):e.transform[t]:null}async function Kt(e,t){try{let n=await fetch(e);if(!n.ok)throw new L(`Failed to fetch transform module: ${e}`,`TRANSFORM_ERROR`);let r=await n.text();if(typeof window<`u`){let e=new Blob([r],{type:`application/javascript`});return(await import(URL.createObjectURL(e))).default[t]}{let{runInNewContext:n}=await import(`node:vm`),i={module:{exports:{}},console};return n(r,i,e),i.module.exports[t]}}catch(t){throw t instanceof L?t:new L(`Failed to load remote transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}async function qt(e,t){try{return(await import(e)).default[t]}catch(t){throw t instanceof L?t:new L(`Failed to import local transform module: ${e}`,`TRANSFORM_ERROR`,void 0,t)}}var Jt=class e{currentSchema;history=[];migrations=[];isProcessing=!1;constructor(e,t,n){this.currentSchema=e,t&&(this.migrations=t.sort((e,t)=>e.schemaVersion.localeCompare(t.schemaVersion))),n&&(this.history=n.sort((e,t)=>e.version.localeCompare(t.version)))}data(){return{schema:this.currentSchema,history:[...this.history],migrations:[...this.migrations]}}async add(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);if(!e.changes?.length)throw new L(`Migration must include changes`,`INVALID_MIGRATION`);let t={id:Date.now().toString(),schemaVersion:this.currentSchema.version,changes:e.changes,description:e.description,status:`pending`,rollback:e.rollback,transform:e.transform,createdAt:new Date().toISOString(),checksum:``};t.checksum=await Wt(t),this.migrations.push(t)}async dryRun(t,n,r){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{this.isProcessing=!0;let i={...this.currentSchema},a=this.getRelevantMigrations(n,r),o;if(n===`forward`)o=a.reduce((e,t)=>this.applySchemaChanges(e,t.changes,t.id),i);else{let e=[...this.history];for(let t=0;t<a.length;t++){let t=e.pop();t&&(i={...t})}o=i}let s=await e.processMigrationList(t,n,a);return{newSchema:o,dataPreview:s}}catch(e){throw e instanceof L?e:new L(`Dry run failed`,`INVALID_SCHEMA`,void 0,e)}finally{this.isProcessing=!1}}async prepareMigration(){let e=this.migrations.filter(e=>e.status===`pending`);return await this.validateMigrations(e),e}async migrate(t){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);let n=await this.prepareMigration();try{this.isProcessing=!0,this.transformSchema(`forward`);let r=await e.processMigrationList(t,`forward`,n);return this.markMigrationsApplied(n),r}finally{this.isProcessing=!1}}async rollback(e){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);return this.migrations.filter(e=>e.status===`applied`).slice(-1)[0]?this.rollbackToVersion(this.history[this.history.length-1]?.version||this.currentSchema.version,e):e}async rollbackToVersion(t,n){if(this.isProcessing)throw new L(`Concurrent operation`,`CONCURRENT_OPERATION`);try{let r=this.history.findIndex(e=>e.version===t);if(r===-1)throw new L(`Version ${t} not found in history`,`VERSION_NOT_FOUND`);let i=this.migrations.filter(e=>e.schemaVersion===t&&e.status===`applied`).sort((e,t)=>t.id.localeCompare(e.id)),a=this.history.length-r;if(a<0)return n;for(let e=0;e<a;e++)this.transformSchema(`backward`);let o=await e.processMigrationList(n,`backward`,i);return this.migrations=this.migrations.map(e=>e.schemaVersion>=t&&e.status===`applied`?{...e,status:`rolled_back`}:e),o}finally{this.isProcessing=!1}}static async processMigrationList(e,t,n){return(await Promise.all(n.map(async e=>{try{return{migration:e,transform:await Gt(e,t)}}catch(t){throw new L(`Failed to resolve transform for migration ${e.id}`,`TRANSFORM_ERROR`,e.id,t)}}))).filter(e=>!!e.transform).reduce((e,{migration:t,transform:n})=>e.pipeThrough(new TransformStream({async transform(e,r){try{let t=await n(e);r.enqueue(t)}catch(e){r.error(new L(`Data transformation failed for migration ${t.id}`,`TRANSFORM_ERROR`,t.id,e))}}})),e)}getRelevantMigrations(e,t){return[...this.migrations].filter(n=>{let r=e===`forward`?`pending`:`applied`,i=!t||n.schemaVersion.localeCompare(t)>=0;return n.status===r&&i}).sort((t,n)=>e===`forward`?t.id.localeCompare(n.id):n.id.localeCompare(t.id))}applySchemaChanges(e,t,n){let r=It(t,e),i=Lt(e.version,r);return t.map(t=>{try{return Pt(t,e)}catch(e){throw new L(`Invalid schema change`,`INVALID_SCHEMA`,n,e)}}).reduce((e,t)=>{try{return Ut(e,t)}catch(e){throw new L(`Failed to apply patch`,`INVALID_SCHEMA`,n,e)}},{...e,version:i})}async validateMigrations(e){await Promise.all(e.map(async e=>{let t=await Wt(e);if(e.checksum!==t)throw new L(`Checksum mismatch`,`CHECKSUM_MISMATCH`,e.id)}))}markMigrationsApplied(e){this.migrations=this.migrations.map(t=>e.some(e=>e.id===t.id)?{...t,status:`applied`}:t)}transformSchema(e){try{if(e===`backward`){let e=this.history.pop();if(!e)throw Error(`No previous version`);this.currentSchema=e;return}let t=this.migrations.filter(e=>e.status===`pending`).flatMap(e=>e.changes);if(!t.length)return;this.history.push(structuredClone(this.currentSchema)),this.currentSchema=t.reduce((e,t)=>this.applySchemaChanges(e,[t]),this.currentSchema)}catch(e){throw e instanceof L?e:new L(`Schema transformation failed`,`INVALID_SCHEMA`,void 0,e)}}};function Yt(){return{maxDepth:20,mode:`strict`}}var z=class e{raw;constructor(e){this.raw=e}static zero(){return new e({kind:`zero`})}static fromSingle(t){return new e({kind:`single`,value:t})}static fromMultiple(t){return new e({kind:`multiple`,value:t})}static fromInline(t){return new e({kind:`inline`,value:t})}static fromFieldSchema(t){if(t==null)return e.zero();if(Array.isArray(t))return e.fromMultiple(t);let n=t;return`id`in n&&typeof n.id==`string`?e.fromSingle(t):`type`in n?e.fromInline(t):e.zero()}isZero(){return this.raw.kind===`zero`}isSingle(){return this.raw.kind===`single`}isMultiple(){return this.raw.kind===`multiple`}isInline(){return this.raw.kind===`inline`}asSingle(){if(this.raw.kind!==`single`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'single'`);return this.raw.value}asMultiple(){if(this.raw.kind!==`multiple`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'multiple'`);return this.raw.value}asInline(){if(this.raw.kind!==`inline`)throw Error(`FieldSchemaReference is '${this.raw.kind}', not 'inline'`);return this.raw.value}};function Xt(e){let t=e;if(`predicate`in t&&typeof t.predicate==`string`){let t=e;return{kind:`rule`,name:t.name,description:t.description??void 0,predicate:t.predicate,parameters:t.parameters??void 0,fields:t.fields??[]}}let n=e;return{kind:`group`,name:n.name,description:n.description??void 0,operator:n.operator,rules:(n.rules??[]).map(e=>Xt(e))}}function B(e){if(!e)return{};let t={};for(let[n,r]of Object.entries(e))t[n]=Xt(r);return t}const V={issues:[],success:!0,skipped:!1},Zt={issues:[],success:!0,skipped:!0};function H(e){return{issues:e,success:!1,skipped:!1}}function U(e,t,n){return{code:e,message:t,path:n}}function W(e,t){return e===``?t:`${e}.${t}`}function Qt(e,t,n){return e===``?[n,[n]]:[`${e}.${n}`,[...t,n]]}function $t(e){return e===``?[]:e.split(`.`)}function G(e){let t=e.length;for(let n of e)for(let e of n)e===`[`&&t++;return t}function en(e,t,n){let r=[],i=[];for(let a of n){let n=$t(a);e===``?(r.push(a),i.push(n)):(r.push(W(e,a)),i.push([...t,...n]))}return[r,i]}function tn(e,t){if(t.length===0)return[e,!0];let n=e;for(let e of t){if(n==null)return[void 0,!1];let t=e.indexOf(`[`);if(t===-1){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let t=n;if(!(e in t))return[void 0,!1];n=t[e]}else{let r=e.slice(0,t);if(r!==``){if(typeof n!=`object`||Array.isArray(n))return[void 0,!1];let e=n;if(!(r in e))return[void 0,!1];n=e[r]}let i=e.slice(t).matchAll(/\[(\d+)\]/g);for(let e of i){let t=e[1];if(t===void 0)return[void 0,!1];let r=parseInt(t,10);if(!Array.isArray(n)||r>=n.length)return[void 0,!1];n=n[r]}}}return[n,!0]}function K(e,t){return tn(e.rootData,t)}function q(e){return typeof e==`object`&&e&&!Array.isArray(e)?e:null}function nn(e){if(e==null)return!0;let t=typeof e;return t===`string`||t===`number`||t===`boolean`||t===`bigint`||t===`symbol`}function rn(e,t,n=!1){if(e===t)return!0;if(e===null||t===null||e===void 0||t===void 0)return e===t;let r=typeof e,i=typeof t;if(r===`string`&&i===`string`||r===`boolean`&&i===`boolean`||r===`number`&&i===`number`||n&&r===`number`&&i===`number`)return e===t;if(r!==`object`||i!==`object`)return!1;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!rn(e[r],t[r],n))return!1;return!0}if(Array.isArray(e)!==Array.isArray(t))return!1;let a=e,o=t,s=Object.keys(a),c=Object.keys(o);if(s.length!==c.length)return!1;for(let e of s)if(!(e in o)||!rn(a[e],o[e],n))return!1;return!0}function an(e){return typeof e==`number`&&Number.isInteger(e)}function on(e){return typeof e==`number`&&!Number.isInteger(e)}function J(e){return typeof e==`number`}var Y=class{id;path;pathParts;deps;constructor(e,t,n,r=[]){this.id=e,this.path=t,this.pathParts=n,this.deps=r}getID(){return this.id}getPath(){return this.path}getPathParts(){return this.pathParts}getDependencies(){return this.deps}},sn=class extends Y{expectedFields;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expectedFields=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n)return V;let r=q(t);if(!r)return H([U(`TYPE_MISMATCH`,`Expected object for unexpected field check`,this.path)]);let i=[];for(let e of Object.keys(r))this.expectedFields.has(e)||i.push(U(`UNEXPECTED_FIELD`,`Unexpected field '${e}'`,W(this.path,e)));return i.length>0?H(i):V}},cn=class extends Y{fieldName;parentPath;parentPathParts;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.fieldName=r,this.parentPath=i,this.parentPathParts=a}async execute(e){let[t,n]=K(e,this.parentPathParts);if(!n&&this.parentPath!==``)return V;if(t==null)return Zt;let r=q(t);return r?!(this.fieldName in r)||r[this.fieldName]===void 0?H([U(`REQUIRED_FIELD_MISSING`,`Required field '${this.fieldName}' is missing`,this.path)]):V:H([U(`INVALID_DATA_STRUCTURE`,`Cannot check for required fields on non-object parent`,this.parentPath)])}},ln=class extends Y{expected;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.expected=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;switch(this.expected){case`string`:if(typeof t==`string`)return V;break;case`boolean`:if(typeof t==`boolean`)return V;break;case`number`:if(J(t))return V;break;case`integer`:if(an(t))return V;break;case`decimal`:if(on(t))return V;break;case`bytes`:if(t instanceof Uint8Array||Array.isArray(t)&&t.every(e=>typeof e==`number`))return V;break;case`record`:if(q(t)!==null)return V;break;case`unknown`:return V;default:return V}return H([U(`TYPE_MISMATCH`,`Expected ${this.expected}, got ${typeof t} (${JSON.stringify(t)})`,this.path)])}},un=class extends Y{lookup;complex;expectNumeric;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.lookup=r,this.complex=i,this.expectNumeric=a}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(typeof t!=`string`&&typeof t!=`number`&&typeof t!=`boolean`)return H([U(`TYPE_MISMATCH`,`Expected scalar for enum, got ${typeof t}`,this.path)]);if(this.lookup.has(t))return V;for(let e of this.complex)if(rn(t,e,this.expectNumeric))return V;return H([U(`ENUM_VIOLATION`,`Value ${JSON.stringify(t)} is not in the allowed list`,this.path)])}},dn=class extends Y{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return H([U(`ARRAY_TYPE_MISMATCH`,`Expected array`,this.path)]);let r=G(this.pathParts);if(r>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let i=[],a=e.maxDepth-r;for(let n=0;n<t.length;n++){let r=t[n],o=`${this.path}[${n}]`,s=await this.graph.traverse(e.functionMap,{item:r},e.mode,a,e.originalRoot);for(let e of s)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,o):e.path})}return i.length>0?H(i):V}},fn=class extends Y{graph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;let r=q(t);if(!r)return H([U(`OBJECT_TYPE_MISMATCH`,`Expected object for record, got ${typeof t}`,this.path)]);let i=G(this.pathParts);if(i>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);if(!this.graph)return V;let a=[],o=e.maxDepth-i;for(let[t,n]of Object.entries(r)){let r=W(this.path,t),i=await this.graph.traverse(e.functionMap,{item:n},e.mode,o,e.originalRoot);for(let e of i)a.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,r):e.path})}return a.length>0?H(a):V}},pn=class extends Y{itemGraph;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.itemGraph=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(!Array.isArray(t))return H([U(`SET_TYPE_MISMATCH`,`Expected array for set validation`,this.path)]);if(this.itemGraph){let n=G(this.pathParts);if(n>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=e.maxDepth-n,i=[];for(let n=0;n<t.length;n++){let a=`${this.path}[${n}]`,o=await this.itemGraph.traverse(e.functionMap,{item:t[n]},e.mode,r,e.originalRoot);for(let e of o)i.push({...e,path:e.path.startsWith(`item`)?e.path.replace(`item`,a):e.path})}if(i.length>0)return H(i)}if(t.length<=1)return V;let r=new Map,i=[];for(let e=0;e<t.length;e++){let n=t[e];if(nn(n)){let t=r.get(n);if(t!==void 0)return H([U(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t})`,this.path)]);r.set(n,e)}else{let t=i.find(e=>rn(n,e.val,!1));if(t)return H([U(`SET_DUPLICATE`,`Duplicate value at index ${e} (first seen at index ${t.index})`,this.path)]);i.push({val:n,index:e})}}return V}},mn=class extends Y{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){let[t,n]=K(e,this.pathParts);if(!n)return V;if(!Array.isArray(t))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry must be an array of coordinate arrays`,this.path)]);for(let e=0;e<t.length;e++){let n=t[e];if(!Array.isArray(n))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry inner element at index ${e} must be an array`,`${this.path}[${e}]`)]);for(let t=0;t<n.length;t++)if(!J(n[t]))return H([U(`GEOMETRY_TYPE_MISMATCH`,`Geometry element at [${e}][${t}] is not a valid number`,`${this.path}[${e}][${t}]`)])}return V}},hn=class extends Y{constructor(e,t,n,r=[]){super(e,t,n,r)}async execute(e){return V}},gn=class extends Y{validationGraph;schemaName;constructor(e,t,n,r,i,a=[]){super(e,t,n,a),this.validationGraph=r,this.schemaName=i}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;let r=q(t);if(!r)return H([U(`TYPE_MISMATCH`,`Expected object for recursive schema ${this.schemaName}, got ${typeof t}`,this.path)]);if(G(this.pathParts)>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Recursive schema '${this.schemaName}' exceeds maximum depth of ${e.maxDepth}`,this.path)]);let i=(await this.validationGraph.traverse(e.functionMap,r,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path===``?this.path:W(this.path,e.path)}));return i.length>0?H(i):V}},_n=class extends Y{graphs;constructor(e,t,n,r,i=[]){super(e,t,n,i),this.graphs=r}async execute(e){let[t,n]=K(e,this.pathParts);if(!n||t===void 0)return V;if(G(this.pathParts)>=e.maxDepth)return H([U(`MAX_DEPTH_EXCEEDED`,`Maximum nesting depth of ${e.maxDepth} exceeded`,this.path)]);let r=await Promise.all(this.graphs.map(async(n,r)=>{let i={root:t},a=(await n.traverse(e.functionMap,i,e.mode,e.maxDepth,e.originalRoot)).map(e=>({...e,path:e.path.startsWith(`root`)?e.path.replace(`root`,this.path):e.path,message:`[variant ${r}] ${e.message}`})),o=a.length===0;return!o&&a.length===0&&a.push(U(`INTERNAL_ERROR`,`Variant ${r} failed without reporting any issues`,this.path)),{ok:o,issues:a}}));return r.some(e=>e.ok)?V:H([U(`UNION_MISMATCH`,`Value at '${this.path}' did not match any variant of the union`,this.path),...r.flatMap(e=>e.issues)])}},vn=class extends Y{constraint;fieldPaths;fieldPathParts;constraintPath;constraintPathParts;scope;constructor(e,t,n,r,i,a,o,s,c,l=[]){super(e,t,n,l),this.constraint=r,this.fieldPaths=i,this.fieldPathParts=a,this.constraintPath=o,this.constraintPathParts=s,this.scope=c}async execute(e){if(this.constraint.kind!==`rule`)return H([U(`INTERNAL_ERROR`,`ConstraintNode '${this.constraint.name}': expected a rule, got a group`,this.constraintPath)]);switch(this.scope){case`global`:return this.executeGlobalConstraint(e,this.constraint);case`recursive`:return this.executeRecursiveConstraint(e,this.constraint)}}executeGlobalConstraint(e,t){return Sn(e,t.name,this.constraintPath,this.fieldPaths,this.fieldPathParts,()=>Cn({...e,data:e.rootData},t,this.constraintPath))}executeRecursiveConstraint(e,t){let[n,r]=K(e,this.constraintPathParts);if(!r)return Promise.resolve(Zt);let i=this.fieldPaths.map(e=>this.constraintPath!==``&&e.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPath.length+1):e),a=this.fieldPathParts.map((e,t)=>{let n=this.fieldPaths[t]??``;return this.constraintPath!==``&&n.startsWith(this.constraintPath+`.`)?e.slice(this.constraintPathParts.length):e});return Sn(e,t.name,this.constraintPath,i,a,()=>Cn({...e,data:n},t,this.constraintPath))}},yn=class extends Y{group;name;constructor(e,t,n,r,i,a,o=[]){super(e,t,n,o),this.group=r,this.name=i}async execute(e){let{allPaths:t,allParts:n}=xn(this.group,this.path,this.pathParts);return Sn(e,this.name,this.path,t,n,()=>this.executeGroup(e,this.group,this.path,this.name))}async executeGroup(e,t,n,r){let i=[],a=[];for(let o of t.rules){let t;if(o.kind===`rule`)t=await Cn(e,o,n);else if(o.kind===`group`)t=await this.executeGroup(e,o,n,o.name);else return H([U(`INTERNAL_ERROR`,`Unknown constraint kind in group '${r}'`,n)]);i.push(t.success),t.success||a.push(...t.issues)}return bn(t.operator,i)?V:H([U(`CONSTRAINT_GROUP_VIOLATION`,`Constraint group '${r}' failed`,n),...a])}};function bn(e,t){switch(e){case`and`:return t.every(Boolean);case`or`:return t.some(Boolean);case`not`:return t.length===1&&!t[0];case`nor`:return t.every(e=>!e);case`xor`:return t.filter(Boolean).length===1;case`nand`:return!t.every(Boolean);case`xnor`:return t.filter(Boolean).length!==1;default:return!1}}function xn(e,t,n){let r=new Map;function i(e){for(let a of e.rules)if(a.kind===`rule`){let[e,i]=en(t,n,a.fields);for(let t=0;t<e.length;t++){let n=e[t],a=i[t];r.set(n,a)}}else a.kind===`group`&&i(a)}i(e);let a=Array.from(r.keys());return{allPaths:a,allParts:a.map(e=>r.get(e))}}async function Sn(e,t,n,r,i,a){let o=[],s=[];for(let t=0;t<r.length;t++){let n=i[t]??[],a=r[t]??``,[,c]=K(e,n);c?o.push(a):s.push(a)}if(s.length===0)return a();if(o.length===0)switch(e.mode){case`strict`:return H([U(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:case`loose`:return Zt}switch(e.mode){case`strict`:return H([U(`CONSTRAINT_INCOMPLETE`,`Constraint '${t}' cannot be evaluated: missing required fields ${JSON.stringify(s)}`,n)]);case`partialStrict`:return H([U(`CONSTRAINT_PARTIAL_UPDATE`,`Constraint '${t}' couples fields ${JSON.stringify(r)}. Cannot update only ${JSON.stringify(o)} - all coupled fields must be updated together`,n)]);case`loose`:return Zt}}async function Cn(e,t,n){let r=e.functionMap[t.predicate];if(!r)return H([U(`MISSING_PREDICATE`,`Predicate '${t.predicate}' not found`,n)]);let i=await r({root:e.originalRoot,data:e.data,keys:t.fields,parameters:t.parameters});return i.length>0?H(i.map(e=>({...e,path:e.path===``?n:W(n,e.path)}))):V}var wn=class{constraints=new Map;add(e,t,n,r,i){let a=this.constraints.get(e);(!a||n>=a.specificity)&&this.constraints.set(e,{constraint:t,specificity:n,basePath:r,scope:i})}getEffective(){return Array.from(this.constraints.values())}};function Tn(){return new wn}var En=class{buildingSchemas=new Map;recursiveGraphCache=new Map;isRecursive(e){return(this.buildingSchemas.get(e)??0)>0}markBuilding(e){this.buildingSchemas.set(e,(this.buildingSchemas.get(e)??0)+1)}unmarkBuilding(e){let t=(this.buildingSchemas.get(e)??0)-1;t<=0?this.buildingSchemas.delete(e):this.buildingSchemas.set(e,t)}makeGraphCacheKey(e,t){let n=Object.keys(t).sort();return n.length===0?e:`${e}:${n.join(`,`)}`}async getOrBuildRecursiveGraph(e,t,n,r){let i=this.makeGraphCacheKey(e,n),a=this.recursiveGraphCache.get(i);if(a)return a;let o=new Pn;this.recursiveGraphCache.set(i,o),this.markBuilding(e);try{let e=Dn(t,r);await o.buildFromSchema(e,``,[],new Map,t,n,r,this,!1,!1),o.finalize()}catch(e){throw this.recursiveGraphCache.delete(i),e}finally{this.unmarkBuilding(e)}return o}};function Dn(e,t){let n=`fields`in e?e.fields:{},r=e.constraints??{},i={name:e.name,version:`0`,fields:n,constraints:r};return e.description!==void 0&&(i.description=e.description),t.schemas!==void 0&&(i.schemas=t.schemas),i}function X(e){return`type`in e&&e.type?e.type:`fields`in e&&e.fields&&Object.keys(e.fields).length>0?`object`:`unknown`}function On(e){switch(e){case`object`:case`array`:case`set`:case`record`:case`union`:case`composite`:case`enum`:case`geometry`:return!0;default:return!1}}function kn(e){let t=[];for(let n of e.rules)n.kind===`rule`?t.push(...n.fields):n.kind===`group`&&t.push(...kn(n));return t}function An(e,t){let n={},r=e.constraints??{},i=e=>e===t||e.startsWith(t+`.`);for(let[e,t]of Object.entries(r)){let r=Xt(t);r.kind===`rule`?r.fields.some(i)&&(n[e]=r):r.kind===`group`&&kn(r).some(i)&&(n[e]=r)}return n}function jn(e,t,n,r){let i=Tn(),a=r===``?`global`:`recursive`;for(let[,t]of Object.entries(e))i.add(t.name,t,1,r,a);for(let[,e]of Object.entries(t))i.add(e.name,e,2,r,a);for(let[,e]of Object.entries(n))i.add(e.name,e,3,r,a);return i.getEffective()}function Mn(e){let t=new Map,n=[];for(let r of e)r!=null&&(typeof r==`string`||typeof r==`number`||typeof r==`boolean`?t.set(r,!0):n.push(r));return{lookup:t,complex:n}}function Nn(e,t){let n=new Set;function r(e){let i=X(e);if(i===`object`||i===`unknown`){if(`fields`in e&&e.fields)for(let t of Object.values(e.fields))n.add(t.name)}else if(i===`union`||i===`composite`){if(!(`schema`in e)||!e.schema)return;let n=z.fromFieldSchema(e.schema);if(!n.isMultiple())return;for(let e of n.asMultiple()){let n=t.schemas?.[e.id];n&&r(n)}}}for(let n of e){let e=t.schemas?.[n.id];e&&r(e)}return n}var Pn=class e{nodes=new Map;dependencies=new Map;visitedState=new Map;executionOrder=[];nextNodeID=1;nullablePaths=new Set;buildNodeID(){return this.nextNodeID++}addNode(e){let t=e.getID();this.nodes.has(t)||(this.nodes.set(t,e),this.dependencies.set(t,e.getDependencies()))}createUnexpectedFieldsNode(e,t,n,r=[]){return new sn(this.buildNodeID(),e,t,n,r)}createRequiredFieldNode(e,t,n,r,i,a=[]){return new cn(this.buildNodeID(),e,t,n,r,i,a)}createTypeCheckNode(e,t,n,r=[]){return new ln(this.buildNodeID(),e,t,n,r)}createCompletionNode(e,t,n){return new hn(this.buildNodeID(),e,t,n)}finalize(){this.visitedState=new Map;let e=[],t=!1,n=r=>{if(t)return;let i=this.visitedState.get(r)??0;if(i===1){t=!0;return}if(i!==2){this.visitedState.set(r,1);for(let e of this.dependencies.get(r)??[])n(e);this.visitedState.set(r,2),e.push(r)}};for(let e of this.nodes.keys())if((this.visitedState.get(e)??0)===0&&(n(e),t))throw Error(`Circular dependency detected in validation graph at node ${e}`);this.executionOrder=e}async traverse(e,t,n,r,i){let a={originalRoot:i,rootData:t,data:t,functionMap:e,maxDepth:r,mode:n,visited:new Map,issues:[]};for(let e of this.executionOrder){let t=this.nodes.get(e),r=this.dependencies.get(e)??[],i=!1;for(let t of r){let n=a.visited.get(t);if(n!==void 0&&!n){a.visited.set(e,!0),i=!0;break}}if(i)continue;let o=t.getPathParts(),s=t.getPath(),[c,l]=K(a,o),u=t instanceof cn,d=this.nullablePaths.has(s);if((!l||c===void 0)&&!u){a.visited.set(e,!0);continue}if(c===null){if(u&&d)continue;if(d){a.visited.set(e,!0);continue}}let f=await t.execute(a);if(a.visited.set(e,f.success),f&&!f.skipped&&f.issues.length>0)for(let e of f.issues)switch(n){case`strict`:a.issues.push(e);break;case`partialStrict`:e.code!==`REQUIRED_FIELD_MISSING`&&a.issues.push(e);break;case`loose`:e.code!==`REQUIRED_FIELD_MISSING`&&e.code!==`UNEXPECTED_FIELD`&&a.issues.push(e)}}return a.issues}async buildFromSchema(e,t,n,r,i,a,o,s,c,l){let u=[],d=i&&`fields`in i&&i.fields?i.fields:e.fields,f=null;if(!c){let e=new Set(Object.values(d).map(e=>e.name));f=this.createUnexpectedFieldsNode(t,n,e),this.addNode(f),u.push({id:f.getID(),deps:[]})}let p=[],m=[];for(let i of Object.values(d)){let a=await this.buildFieldNodes(i,t,n,p,e,r,o,s,l);m.push(...a)}let ee=jn(B(i?i.constraints:e.constraints),a,o&&t!==``?An(o,t):{},t),te=m.map(e=>e.id),h=this.buildFromEffectiveConstraints(ee,n,te,r);if(h.length>0){let e=this.createCompletionNode(t,n,h);this.addNode(e),u.push({id:e.getID(),deps:h})}return u}async buildFieldNodes(e,t,n,r,i,a,o,s,c){let[l,u]=Qt(t,n,e.name);e.nullable!==!1&&this.nullablePaths.add(l);let d=r,f=[];if(e.required){let r=this.createRequiredFieldNode(l,u,e.name,t,n,d);this.addNode(r),d=[r.getID()],f.push({id:r.getID(),deps:d})}if(!On(e.type)){let t=this.createTypeCheckNode(l,u,e.type,d);this.addNode(t),d=[t.getID()],f.push({id:t.getID(),deps:d})}let p=await this.buildFieldTypeNodes(e,l,u,d,i,a,o,s,c);return f.push(...p),f}async buildFieldTypeNodes(e,t,n,r,i,a,o,s,c){let l=[],u=null;switch(e.type){case`enum`:u=await this.buildEnumNode(e,t,n,r,o);break;case`array`:u=await this.buildContainerNode(e,t,n,r,o,s,`array`,`array items`);break;case`set`:{let i=await this.buildContainerNode(e,t,n,r,o,s,`array`,`set elements`),a=i instanceof dn?i.graph:null;u=new pn(this.buildNodeID(),t,n,a,r);break}case`record`:u=await this.buildContainerNode(e,t,n,r,o,s,`record`,`record items`);break;case`union`:u=await this.buildUnionNode(e,t,n,r,o,s);break;case`composite`:{let i=await this.buildCompositeNode(e,t,n,r,o,s);return l.push(...i),l}case`geometry`:u=new mn(this.buildNodeID(),t,n,r);break;case`object`:{let r=await this.buildObjectFieldNodes(e,t,n,i,a,o,s,c);return l.push(...r),l}default:return l}return u&&(this.addNode(u),l.push({id:u.getID(),deps:u.getDependencies()})),l}async buildObjectFieldNodes(e,t,n,r,i,a,o,s){let c=[],l=z.fromFieldSchema(e.schema);if(l.isZero())throw Error(`FieldSchemaReference is zero/uninitialized at path '${t}'`);if(!l.isSingle())throw Error(`Object field must use a single schema reference at path '${t}'`);let u=l.asSingle(),d=a.schemas?.[u.id];if(!d)throw Error(`Nested schema '${u.id}' not found for field '${e.name}' at path '${t}'`);if(o.isRecursive(u.id)){let e=B(u.constraints),r=await o.getOrBuildRecursiveGraph(u.id,d,e,a),i=new gn(this.buildNodeID(),t,n,r,u.id);return this.addNode(i),c.push({id:i.getID(),deps:[]}),c}let f=Dn(d,a);o.markBuilding(u.id);try{let e=await this.buildFromSchema(f,t,n,i,d,B(u.constraints),a,o,s,!1),r=new hn(this.buildNodeID(),t,n,e.map(e=>e.id));this.addNode(r),c.push({id:r.getID(),deps:e.map(e=>e.id)})}finally{o.unmarkBuilding(u.id)}return c}async buildContainerNode(t,n,r,i,a,o,s,c){let l=e=>s===`array`?new dn(this.buildNodeID(),n,r,e,i):new fn(this.buildNodeID(),n,r,e,i),u=z.fromFieldSchema(t.schema);if(u.isZero())return l(null);if(u.isSingle()){let e=u.asSingle();if(e.id!==``){let t=a.schemas?.[e.id];if(!t)throw Error(`Nested schema '${e.id}' not found for ${c} at path '${n}'`);let r=X(t),i=r===`composite`||r===`union`,s;s=r===`object`?z.fromSingle({id:e.id}):`schema`in t&&t.schema?z.fromFieldSchema(t.schema):z.zero();let u=s.isZero()?void 0:s.isSingle()?s.asSingle():s.isMultiple()?s.asMultiple():s.asInline(),d={name:`item`,type:r};u!==void 0&&(d.schema=u);let f=`default`in t?t.default:void 0;return f!==void 0&&(d.default=f),l(await this.createSubGraph(`item`,d,n,a,o,i,!1))}}if(u.isInline()){let t=u.asInline();if(!t.type)throw Error(`Inline descriptor missing 'type' field at path '${n}'`);let r=new e,i=new Map,s=t.type;if(s===`object`){let e={name:`__inline_record`,version:`0`,fields:{}};a.schemas!==void 0&&(e.schemas=a.schemas),await r.buildFromSchema(e,``,[],i,null,{},a,o,!0,!1)}else{let e=r.createTypeCheckNode(`item`,[`item`],s);r.addNode(e)}return r.finalize(),l(r)}throw Error(`Invalid schema reference for ${c} at path '${n}'`)}async buildEnumNode(e,t,n,r,i){let a=z.fromFieldSchema(e.schema);if(a.isZero())throw Error(`Enum field must have schema reference at path '${t}'`);let o=a.isSingle()?[a.asSingle()]:a.isMultiple()?a.asMultiple():[];if(o.length===0&&!a.isInline())throw Error(`Enum schema reference must be single, multiple, or inline at path '${t}'`);if(a.isInline()){let e=a.asInline();if(!e.type)throw Error(`Inline enum descriptor missing 'type' at path '${t}'`);if(!e.values||e.values.length===0)throw Error(`Inline enum descriptor missing 'values' at path '${t}'`);let{lookup:i,complex:o}=Mn(e.values),s=e.type===`number`||e.type===`integer`||e.type===`decimal`;return new un(this.buildNodeID(),t,n,i,o,s,r)}let s=new Map,c=[],l=null;for(let e of o){let n,r,a;if(e.id!==``){let o=i.schemas?.[e.id];if(!o)throw Error(`Enum schema '${e.id}' not found at path '${t}'`);if(!(`values`in o)||!o.values?.length)throw Error(`Enum schema '${e.id}' has no values defined at path '${t}'`);a=o.type,{lookup:n,complex:r}=Mn(o.values)}else throw Error(`Inline descriptor in named-reference array is not permitted at path '${t}'`);for(let[e]of n)s.set(e,!0);c=c.concat(r),l||=a}let u=l===`number`||l===`integer`||l===`decimal`;return new un(this.buildNodeID(),t,n,s,c,u,r)}async buildUnionNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Union field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Nested schema '${e.id}' not found for union at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:X(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!1);c.push(o)}return new _n(this.buildNodeID(),t,n,c,r)}async buildCompositeNode(e,t,n,r,i,a){let o=z.fromFieldSchema(e.schema);if(o.isZero()||!o.isMultiple())throw Error(`Composite field must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=Nn(s,i),l=this.createUnexpectedFieldsNode(t,n,c);this.addNode(l);let u=[l.getID()],d=[];for(let e of s){let r=i.schemas?.[e.id];if(!r)throw Error(`Nested schema '${e.id}' not found for composite at path '${t}'`);let o=X(r);if(o===`object`||o===`unknown`){let o=Dn(r,i);a.markBuilding(e.id);let s;try{s=await this.buildFromSchema(o,t,n,new Map,r,B(e.constraints),i,a,!0,!0)}finally{a.unmarkBuilding(e.id)}for(let e of s)e.deps.includes(l.getID())||(e.deps=[l.getID(),...e.deps],this.dependencies.set(e.id,e.deps));d.push(...s)}else if(o===`union`){if(!(`schema`in r)||!r.schema)throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let o=z.fromFieldSchema(r.schema);if(!o.isMultiple())throw Error(`Union part '${e.id}' in composite must reference multiple schemas at path '${t}'`);let s=o.asMultiple(),c=[];for(let e of s){let n=i.schemas?.[e.id];if(!n)throw Error(`Union variant schema '${e.id}' not found at path '${t}'`);let r;r=`fields`in n&&n.fields&&Object.keys(n.fields).length>0?{name:`root`,type:`object`,schema:{id:e.id}}:{name:`root`,type:X(n),schema:`schema`in n?n.schema:void 0};let o=await this.createSubGraph(`root`,r,t,i,a,!0,!0);c.push(o)}let l=new _n(this.buildNodeID(),t,n,c,u);this.addNode(l),d.push({id:l.getID(),deps:u})}else throw Error(`Composite part '${e.id}' has unsupported effective type '${o}'; composite parts must be object or union schemas at path '${t}'`)}return[{id:l.getID(),deps:[]},...d]}buildFromEffectiveConstraints(e,t,n,r){let i=[];for(let a of e){let e=this.buildFromConstraintRuleWithScope(a.constraint,a.basePath,t,n,r,a.scope);i.push(...e)}return i}buildFromConstraintRuleWithScope(e,t,n,r,i,a){let o=`${t}:${e.name}`;if(i.get(o))return[];if(i.set(o,!0),e.kind===`rule`){let[i,o]=en(t,n,e.fields),s=new vn(this.buildNodeID(),t,n,e,i,o,t,n,a,r);return this.addNode(s),[s.getID()]}if(e.kind===`group`){let i=new yn(this.buildNodeID(),t,n,e,e.name,a,r);return this.addNode(i),[i.getID()]}return[]}async createSubGraph(t,n,r,i,a,o,s){let c={name:`subgraph_${r}`,version:`0`,fields:{[t]:n}};i.schemas!==void 0&&(c.schemas=i.schemas);let l=new e,u=new Map;return await l.buildFromSchema(c,``,[],u,null,{},i,a,o,s),l.finalize(),l}},Fn=class e{fmap;graph;config;constructor(e,t,n){this.graph=e,this.fmap=t,this.config=n}async validate(e){return this.graph.traverse(this.fmap,e,`strict`,this.config.maxDepth,e)}async validatePartial(e){return this.graph.traverse(this.fmap,e,`partialStrict`,this.config.maxDepth,e)}async validateLoose(e){return this.graph.traverse(this.fmap,e,`loose`,this.config.maxDepth,e)}static async create(t,n,r=Yt()){let i=new Pn,a=new En,o=new Map;return await i.buildFromSchema(t,``,[],o,null,{},t,a,!1,!1),i.finalize(),new e(i,n,r)}},In={version:`1.0.0`,name:`Schema`,description:`Meta-schema defining the structure of schema definitions`,fields:{"019d7775-6565-7755-904d-2d0cdf0e1b86":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6565-73d6-a7be-130b3c1e93c8":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6565-722f-9a5a-2810f537a61c":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6565-7fe8-86f6-57a8a305a314":{name:`schemas`,type:`record`,schema:{id:`019d7775-6565-7db3-9bb0-be9fc50550bd`}},"019d7775-6565-7c1f-a6f7-aeb5c4eb35e4":{name:`name`,required:!0,type:`string`},"019d7775-6565-7545-9e25-fc471f57bcfb":{name:`description`,type:`string`},"019d7775-6565-7bdc-a9d4-776b1f163346":{name:`version`,required:!0,type:`string`},"019d7775-6565-74ed-8a52-12027f3c577d":{name:`metadata`,type:`record`}},schemas:{"019d7775-6566-7da1-b24a-67bd81dd2b0e":{name:`Unknown`,type:`unknown`},"019d7775-6565-7db3-9bb0-be9fc50550bd":{name:`NestedSchema`,fields:{"019d7775-6566-77a1-9b36-bbd57f927d11":{name:`fields`,type:`record`,schema:{id:`019d7775-6565-7f15-b0f4-27a4f40cbf5c`}},"019d7775-6567-7931-b2d6-fb52039c28b5":{name:`type`,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6567-7742-90ae-a96178ae505d":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"019d7775-6567-7709-a7e3-ed75f833da15":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-748e-b099-50c76a5b79c2":{name:`name`,required:!0,type:`string`},"019d7775-6566-74df-a8df-038cc26af034":{name:`description`,type:`string`},"019d7775-6567-748e-bcad-76068cc4739f":{name:`metadata`,type:`record`},"019d7775-6567-7022-b7fb-3975ccae2b72":{name:`concrete`,type:`boolean`},"019d7775-6567-7f25-b7e2-acc72435964f":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6567-750b-a618-5bcdc4e5d04a":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"019d7775-6567-75c4-b399-eb4764617462":{name:`default`,type:`unknown`}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`}}},"019d7775-6565-71ef-a08f-cdcdb116f947":{name:`IndexCondition`,fields:{"019d7775-6567-75ef-8e82-347da30f06fd":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7a70-8b56-e55704397fc2`}},"019d7775-6567-7466-a873-e7866e9d031f":{name:`field`,required:!0,type:`string`},"019d7775-6567-7526-9704-d19fb4eb8d8d":{name:`value`,required:!0,type:`unknown`},"a2f03bc4-e620-4b7f-a2ad-8271471ac1e4":{name:`description`,type:`string`},"8782f46d-5f1f-4102-9c3c-917752d8c9d1":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-713e-8355-df812c6bb8c9":{name:`index_condition_exclusive_type`,predicate:`index_condition_type_exclusive`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`}}},"019d7775-6565-7d6a-a531-47e7ea838bc4":{name:`IndexConditionGroup`,fields:{"019d7775-6567-7b4e-893f-ee7afb0c4be1":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6567-7af2-817f-bc5b8732c198":{name:`conditions`,required:!0,type:`array`,schema:{id:`019d7775-6565-74a3-a6e0-881a0148fc59`}},"670ebd05-3bce-4da7-a39b-20cf9ba67abe":{name:`description`,type:`string`},"8e60fb23-0e3f-4ae5-bfff-fd20da1da756":{name:`metadata`,type:`record`}}},"019d7775-6565-794d-8aba-bdc5e3cbe97e":{name:`ConstraintUnion`,type:`union`,schema:[{id:`019d7775-6565-703f-95cf-e7511d33c91b`},{id:`019d7775-6565-7ea4-a820-e8bcdbc690bc`}]},"019d7775-6566-7786-8769-c13eb5a883d9":{name:`InlineTypeDescriptor`,fields:{"019d7775-6568-713f-867f-93dde123ccfd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e05-ab1b-6f768f459927`}},"019d7775-6568-7ae5-95b7-56c97722c732":{name:`values`,type:`array`,schema:{id:`019d7775-6566-7da1-b24a-67bd81dd2b0e`}},"347a2348-014b-41a7-80ad-a4e8d364ef5d":{name:`description`,type:`string`},"7d0f2347-3dfe-4e37-8235-1f9122e045dc":{name:`metadata`,type:`record`}}},"019d7775-6566-7e3b-860a-c4aa4adf5d6d":{name:`LogicalOperatorEnum`,type:`string`,values:[`and`,`or`,`not`,`nor`,`xor`,`nand`,`xnor`]},"019d7775-6566-7f2f-a009-0af144502aac":{name:`IndexOrderEnum`,type:`string`,values:[`asc`,`desc`]},"019d7775-6565-7f15-b0f4-27a4f40cbf5c":{name:`Field`,fields:{"019d7775-6566-727e-b4ff-4e6e8b800808":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-765a-b142-157d1d6367b7`}},"019d7775-6566-79e9-a60b-1c15e2154580":{name:`schema`,type:`union`,schema:[{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`},{id:`019d7775-6565-71bc-84f3-044dc089d50c`},{id:`019d7775-6566-7786-8769-c13eb5a883d9`}]},"019d7775-6566-7a4c-877d-262c18adb028":{name:`name`,required:!0,type:`string`},"019d7775-6566-7b93-aa4c-b1e3869512d8":{name:`description`,type:`string`},"019d7775-6566-7ed5-9fb3-33a67b90dbbe":{name:`default`,type:`unknown`},"019d7775-6566-722b-8132-d5d8c4c9b2b6":{name:`required`,type:`boolean`},"a1000000-0000-0000-0000-000000000001":{name:`nullable`,type:`boolean`},"019d7775-6566-7367-9664-adb25994bf41":{name:`deprecated`,type:`boolean`},"019d7775-6566-7fc8-82c6-735838cdfe21":{name:`unique`,type:`boolean`},"95ab0d3b-352c-4aff-a43a-81ab889aaa42":{name:`metadata`,type:`record`}},constraints:{"019d7775-6563-7605-8bfb-c27365b73581":{name:`enum_fields_valid`,predicate:`enum_fields_valid`},"019d7775-6563-74fe-a5ae-4eef62a128d9":{name:`arrays_require_schema`,predicate:`collection_requires_schema`},"019d7775-6563-7080-ae67-bad5cd864f07":{name:`objects_require_schema`,predicate:`object_requires_schema`},"019d7775-6563-7e83-92c4-0258a8fc0d3a":{name:`unions_require_multiple_schemas`,predicate:`union_requires_multiple_schemas`},"019d7775-6564-7c60-9f9f-aeeabe05c1b5":{name:`default_value_type_match`,predicate:`default_matches_type`},"019d7775-6564-77ca-94bc-30a07da76c3d":{name:`records_allow_optional_schema`,predicate:`record_schema_cardinality`},"019d7775-6564-7768-8995-72c282cfe69f":{name:`inline_type_descriptor_valid`,predicate:`inline_type_descriptor_valid`},"019d7775-6563-7c55-a6f3-ac8f087d89d1":{name:`primitives_no_schema`,predicate:`primitives_prohibit_schema`},"019d7775-6563-73ce-aef1-86943b54a1ca":{name:`composites_require_multiple_schemas`,predicate:`composite_requires_multiple_schemas`},"019d7775-6564-719d-9948-67dd885e7632":{name:`object_referenced_schema_has_fields`,predicate:`object_referenced_schema_has_fields`},"019d7775-6564-75ce-a807-02396e0b8394":{name:`composite_referenced_schemas_must_be_objects`,predicate:`composite_referenced_schemas_must_be_objects`},"019d7775-6564-75bc-abf5-6ddd81b7d2d4":{name:`schema_reference_form_correct`,predicate:`schema_reference_form_correct`}}},"019d7775-6565-7ab6-9c65-ab60632aeca8":{name:`Constraint`,type:`composite`,schema:[{id:`019d7775-6565-7068-9cb9-28460e0368f0`},{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}]},"019d7775-6566-765a-b142-157d1d6367b7":{name:`FieldTypeEnum`,type:`string`,values:[`unknown`,`string`,`number`,`integer`,`decimal`,`boolean`,`array`,`set`,`enum`,`object`,`record`,`union`,`composite`,`geometry`,`bytes`]},"019d7775-6566-7a70-8b56-e55704397fc2":{name:`ComparisonOperatorEnum`,type:`string`,values:[`eq`,`neq`,`lt`,`lte`,`gt`,`gte`,`in`,`nin`,`contains`,`ncontains`,`exists`,`nexists`]},"019d7775-6565-74a3-a6e0-881a0148fc59":{name:`IndexConditionUnion`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"019d7775-6565-7068-9cb9-28460e0368f0":{name:`ConstraintMetadata`,fields:{"019d7775-6567-7017-9068-5bfcd1f9fef0":{name:`name`,required:!0,type:`string`},"019d7775-6568-7b0d-ad28-391874b5e3de":{name:`description`,type:`string`},"ee8ea6de-95f9-48e5-a047-4744b39e1c8f":{name:`metadata`,type:`record`}}},"019d7775-6565-703f-95cf-e7511d33c91b":{name:`ConstraintRule`,fields:{"019d7775-6568-7ad6-9e45-7fdbdef3fc7e":{name:`predicate`,required:!0,type:`string`},"019d7775-6568-7221-a4de-7e82d6c9f6bc":{name:`parameters`,type:`unknown`},"019d7775-6568-7d8b-a230-328fe5d352e8":{name:`fields`,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`},"019d7775-6563-752b-bf9d-bf8936472250":{name:`constraint_rule_requires_predicate`,predicate:`constraint_rule_requires_predicate`},"019d7775-6564-7fc2-bbdb-0fc59e567f2b":{name:`constraint_fields_exist`,predicate:`constraint_fields_exist`}}},"019d7775-6565-7ea4-a820-e8bcdbc690bc":{name:`ConstraintGroup`,fields:{"019d7775-6568-7ebf-a1c3-829de95d9216":{name:`operator`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7e3b-860a-c4aa4adf5d6d`}},"019d7775-6568-76d9-a21c-fb289c36cc70":{name:`rules`,required:!0,type:`array`,schema:{id:`019d7775-6565-794d-8aba-bdc5e3cbe97e`}}},constraints:{"019d7775-6563-7028-b96f-bc6e0abefc15":{name:`constraint_type_exclusive`,predicate:`constraint_type_exclusive`}}},"019d7775-6565-7f2a-956b-35b40caa8ec7":{name:`SchemaReference`,fields:{"019d7775-6568-762c-932e-89b85f08a951":{name:`id`,required:!0,type:`string`},"019d7775-6568-77eb-bd9d-d874a2354fd5":{name:`indexes`,type:`record`,schema:{id:`019d7775-6565-74f6-9c72-9e35d3d16b0a`}},"019d7775-6568-7ed9-99b3-90cca439ee2e":{name:`constraints`,type:`record`,schema:{id:`019d7775-6565-7ab6-9c65-ab60632aeca8`}},"abb42217-0f28-4501-ab12-c54bfe5d1492":{name:`description`,type:`string`},"ed9a369c-1c98-4bc7-845b-92fa81be31f8":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-79f2-9d64-d6b8c2696deb":{name:`schema_reference_id_required`,predicate:`schema_reference_id_required`}}},"019d7775-6565-71bc-84f3-044dc089d50c":{name:`SchemaReferenceArray`,type:`array`,schema:{id:`019d7775-6565-7f2a-956b-35b40caa8ec7`}},"019d7775-6566-7e05-ab1b-6f768f459927":{name:`InlineTypeEnum`,type:`string`,values:[`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]},"019d7775-6566-7df4-bc09-67956251c4a2":{name:`IndexTypeEnum`,type:`string`,values:[`normal`,`unique`,`primary`,`spatial`,`fulltext`]},"019d7775-6566-7a63-9e10-5d11c8eb607c":{name:`String`,type:`string`},"019d7775-6565-74f6-9c72-9e35d3d16b0a":{name:`Index`,fields:{"019d7775-6567-7390-96c6-8154ccc6d1ad":{name:`name`,required:!0,type:`string`},"019d7775-6567-7568-98c8-76d64b745bc7":{name:`description`,type:`string`},"019d7775-6567-7a3d-b154-1199406b7851":{name:`unique`,type:`boolean`},"019d7775-6567-7e59-a2f4-ffc431e24fdd":{name:`type`,required:!0,type:`enum`,schema:{id:`019d7775-6566-7df4-bc09-67956251c4a2`}},"019d7775-6567-787d-921c-fa28f53cedcd":{name:`fields`,required:!0,type:`array`,schema:{id:`019d7775-6566-7a63-9e10-5d11c8eb607c`}},"019d7775-6567-76c9-869a-c4e25c154583":{name:`order`,type:`enum`,schema:{id:`019d7775-6566-7f2f-a009-0af144502aac`}},"019d7775-6567-7bc2-b870-6e1ae4faa558":{name:`condition`,type:`union`,schema:[{id:`019d7775-6565-71ef-a08f-cdcdb116f947`},{id:`019d7775-6565-7d6a-a531-47e7ea838bc4`}]},"75f044b7-253c-4f48-93b7-692b9626c0ea":{name:`metadata`,type:`record`}},constraints:{"019d7775-6564-7c2d-b06d-82971c0b1fa9":{name:`index_fields_exist`,predicate:`index_fields_reference_valid`},"019d7775-6564-7660-adc8-3f1ab037d695":{name:`spatial_index_on_geometry_field`,predicate:`spatial_index_on_geometry_field`},"019d7775-6564-712b-8c53-9fa93809f219":{name:`index_condition_value_type_match`,predicate:`index_condition_value_matches_field_type`},"019d7775-6564-7285-a734-f7eed15d25e4":{name:`index_fields_not_empty`,predicate:`index_fields_not_empty`}}}},constraints:{"019d7775-6563-7554-bfae-d6ad7774d5ac":{name:`nested_schema_mode_exclusive`,predicate:`nested_schema_exclusive_mode`},"019d7775-6564-7565-b53c-49667d23c237":{name:`global_field_id_uniqueness`,predicate:`global_field_id_uniqueness`},"019d7775-6564-7a5d-bb7e-d2e9e804123f":{name:`schema_reference_integrity`,predicate:`schema_reference_exists`},"019d7775-6563-7f89-8368-395733efec86":{name:`schema_name_required`,predicate:`schema_name_required`}}};const Ln=new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`geometry`,`bytes`,`unknown`]),Rn=new Set([`array`,`set`]),zn=[`fields`],Bn=[`type`,`default`,`values`,`schema`],Vn=new Set([`number`,`integer`,`decimal`,`string`]);function Z(e){let t=e.type;return t==null?[``,!1]:[String(t),!0]}function Hn(e){return Ln.has(e)}function Q(e){return Vn.has(e)}function Un(e){return Rn.has(e)}function $(e,t){let n=e.schemas;return n&&n[t]||null}function Wn(e){if(e.fields){let t=e.fields;if(Object.keys(t).length>0)return!0}let t=e.type;return t===`object`||t===`record`}function Gn(e){let t=e.schema;return t==null?[[],!1]:Array.isArray(t)?[t,!0]:[[t],!1]}function Kn(e,t,n){let r=$(e,t);if(!r)return[U(`ENUM_NAMED_SCHEMA_NOT_FOUND`,`Enum referenced schema '${t}' does not exist`,n)];let i=[],a=r.type;if(a==null)return i.push(U(`ENUM_NAMED_SCHEMA_NO_TYPE`,`Enum referenced schema '${t}' must have a 'type' (string or numeric)`,n)),i;let o=String(a);if(!(o===`string`||Q(o)))return i.push(U(`ENUM_NAMED_SCHEMA_INVALID_TYPE`,`Enum referenced schema '${t}' type must be string or numeric, got '${a}'`,n)),i;let s=r.values;if(s==null)return i.push(U(`ENUM_NAMED_SCHEMA_MISSING_VALUES`,`Enum schema '${t}' must have a 'values' array`,n)),i;let c=s;if(!Array.isArray(c)||c.length===0)return i.push(U(`ENUM_NAMED_SCHEMA_EMPTY_VALUES`,`Enum schema '${t}' 'values' must be a non-empty array`,n)),i;let l=o===`string`;return c.forEach((e,r)=>{e!=null&&(l?typeof e!=`string`&&i.push(U(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be string, got ${typeof e}`,`${n} (values[${r}])`)):Q(o)&&(J(e)||i.push(U(`ENUM_NAMED_VALUE_TYPE_MISMATCH`,`Enum schema '${t}': value at index ${r} must be numeric (type '${o}'), got ${typeof e}`,`${n} (values[${r}])`))))}),zn.forEach(e=>{let a=r[e];a!=null&&Object.keys(a).length>0&&i.push(U(`ENUM_NAMED_SCHEMA_HAS_FIELDS`,`Enum referenced schema '${t}' must not have '${e}' (must be a simple value schema)`,n))}),i}function qn(e,t){let n=[],r=e.type;if(r==null)return n.push(U(`ENUM_INLINE_NO_TYPE`,`Inline enum descriptor must have a 'type' (string or numeric)`,t)),n;let i=String(r);if(!(i===`string`||Q(i)))return n.push(U(`ENUM_INLINE_INVALID_TYPE`,`Inline enum descriptor type must be string or numeric, got '${r}'`,t)),n;let a=e.values;if(a==null)return n.push(U(`ENUM_INLINE_MISSING_VALUES`,`Inline enum descriptor must have a 'values' array`,t)),n;let o=a;if(!Array.isArray(o)||o.length===0)return n.push(U(`ENUM_INLINE_EMPTY_VALUES`,`Inline enum descriptor 'values' must be a non-empty array`,t)),n;let s=i===`string`;return o.forEach((e,r)=>{e!=null&&(s?typeof e!=`string`&&n.push(U(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be string, got ${typeof e}`,`${t}.values[${r}]`)):Q(i)&&(J(e)||n.push(U(`ENUM_INLINE_VALUE_TYPE_MISMATCH`,`Inline enum value at index ${r} must be numeric (type '${i}'), got ${typeof e}`,`${t}.values[${r}]`))))}),n}function Jn(e,t,n){if(typeof e!=`object`||!e)return[U(`ENUM_REF_INVALID`,`Enum schema reference must be an object`,t)];let r=e,i=`id`in r,a=`type`in r;if(i&&a)return[U(`ENUM_REF_AMBIGUOUS`,`Enum schema reference cannot have both 'id' (named) and 'type' (inline)`,t)];if(i){let e=r.id;return e==null||e===``?[U(`ENUM_NAMED_REF_INVALID_ID`,`Named enum reference 'id' must be a non-empty string`,t)]:Kn(n,String(e),t)}return a?qn(r,t):[U(`ENUM_REF_NO_ID_OR_TYPE`,`Enum schema reference must have either 'id' (named) or 'type' (inline)`,t)]}function Yn(e,t){let n=t.split(`.`),r=e.fields&&typeof e.fields==`object`?e.fields:null,i=null;for(let t=0;t<n.length;t++){if(!r)return null;let a=null;for(let e of Object.values(r))if(e&&typeof e==`object`&&e.name===n[t]){a=e;break}if(!a)return null;if(i=a,t<n.length-1){let t=a.schema;if(t&&typeof t==`object`&&!Array.isArray(t)){if(typeof t.id==`string`){let n=t.id,i=e.schemas?.[n];r=i&&typeof i==`object`?i.fields??null:null}else r=t.fields?t.fields:null}else r=null}}return i?{def:i}:null}const Xn={primitives_prohibit_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;return r&&Hn(n)&&i!=null?[U(`PRIMITIVE_HAS_SCHEMA`,`Primitive type '${n}' cannot have a schema reference`,``)]:[]},enum_fields_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`enum`)return[];if(t.schema==null)return[U(`ENUM_MISSING_SCHEMA`,`Enum field must have a schema reference`,``)];let[i,a]=Gn(t);if(i.length===0)return[U(`ENUM_NO_VALID_REFS`,`Enum field has no valid schema references`,``)];let o=[];return i.forEach((t,n)=>{let r=a?`schema[${n}]`:`schema`;o.push(...Jn(t,r,e.root))}),o},composite_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;if(r&&n===`composite`){if(i==null)return[U(`COMPOSITE_MISSING_SCHEMA`,`Composite type must have schema references`,``)];if(Array.isArray(i)){if(i.length<2)return[U(`COMPOSITE_INSUFFICIENT_SCHEMAS`,`Composite must have at least 2 schema references`,``)]}else return[U(`COMPOSITE_SCHEMA_NOT_ARRAY`,`Composite 'schema' must be an array of references`,``)]}return[]},composite_referenced_schemas_must_be_objects:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`composite`)return[];let i=t.schema;if(!Array.isArray(i)||i.length===0)return[];let a=[],o=e.root,s=(e,t)=>{let n=$(o,e);if(!n)return[];let r=[],i=n.type;if(i!=null){let e=String(i);if(e===`composite`)return[];if(e===`union`){let e=n.schema;return Array.isArray(e)&&e.forEach((e,n)=>{typeof e==`object`&&e&&`id`in e&&r.push(...s(String(e.id),`${t}.schema[${n}]`))}),r}}return Wn(n)||r.push(U(`COMPOSITE_REF_NOT_OBJECT`,`Composite schema '${e}' must effectively represent an object type (has fields, or type 'object'/'record')`,t)),r};return i.forEach((e,t)=>{typeof e==`object`&&e&&`id`in e&&a.push(...s(String(e.id),`schema[${t}]`))}),a},object_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t),i=t.schema;return r&&n===`object`&&i==null?[U(`OBJECT_MISSING_SCHEMA`,`Object type must have a schema reference`,``)]:[]},object_referenced_schema_has_fields:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let[r,i]=Z(n);if(!i||r!==`object`)return[];let a=n.schema;if(typeof a!=`object`||!a||Array.isArray(a))return[];let o=a.id;if(!o)return[];let s=$(t,String(o));if(!s)return[];let c=s.fields;return c==null?[U(`OBJECT_REF_NO_FIELDS`,`Object field references schema '${o}' which has no 'fields' (must be Schema mode)`,``)]:typeof c!=`object`||Object.keys(c).length===0?[U(`OBJECT_REF_EMPTY_FIELDS`,`Object field references schema '${o}' which has empty fields`,``)]:[]},spatial_index_on_geometry_field:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n||n.type!==`spatial`)return[];let r=n.fields;if(!Array.isArray(r))return[];for(let e of r){let n=String(e),r=Yn(t,n);if(!r)continue;let i=r.def.type;if(i!==`geometry`)return[U(`SPATIAL_INDEX_NON_GEOMETRY`,`Spatial index can only reference geometry fields, but field '${n}' has type '${i}'`,``)]}return[]},index_condition_value_matches_field_type:e=>{let t=e.root,n=e.data;if(typeof n!=`object`||!n)return[];let r=n.field;if(r==null)return[];let i=n.value,a=Yn(t,String(r));if(!a)return[];let o=a.def.type,s=!0;switch(o){case`string`:typeof i!=`string`&&i!==null&&(s=!1);break;case`boolean`:typeof i!=`boolean`&&i!==null&&(s=!1);break;case`integer`:!an(i)&&i!==null&&(s=!1);break;case`number`:case`decimal`:!J(i)&&i!==null&&(s=!1)}return s?[]:[U(`INDEX_CONDITION_VALUE_TYPE_MISMATCH`,`Value for field '${r}' must be ${o}`,``)]},schema_reference_exists:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schemas||{},r=[],i=(e,t)=>{if(e&&typeof e==`object`&&`id`in e){let i=e.id;typeof i==`string`&&!n[i]&&r.push(U(`SCHEMA_REFERENCE_NOT_FOUND`,`Referenced schema '${i}' does not exist`,t))}},a=(e,t)=>{if(!(typeof e!=`object`||!e)){if(e.hasOwnProperty(`schema`)){let n=e.schema;Array.isArray(n)?n.forEach((e,n)=>i(e,`${t}.schema[${n}]`)):i(n,`${t}.schema`)}for(let[n,r]of Object.entries(e))typeof r==`object`&&r&&a(r,t?`${t}.${n}`:n)}};return a(t,``),r},default_matches_type:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.type,r=t.default;if(n===void 0||r==null)return[];let i=String(n),a=``;switch(i){case`string`:typeof r!=`string`&&(a=`must be string`);break;case`boolean`:typeof r!=`boolean`&&(a=`must be boolean`);break;case`integer`:an(r)||(a=`must be integer`);break;case`number`:case`decimal`:J(r)||(a=`must be numeric`);break;case`array`:case`set`:Array.isArray(r)||(a=`must be array`);break;case`object`:case`record`:(typeof r!=`object`||Array.isArray(r))&&(a=`must be object`);break;case`geometry`:Array.isArray(r)||(a=`must be array of coordinate arrays`)}return a?[U(`DEFAULT_VALUE_TYPE_MISMATCH`,`Default value ${JSON.stringify(r)} ${a} for type ${i}`,``)]:[]},global_field_id_uniqueness:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=new Map,r=[],i=(e,t)=>{if(!(typeof e!=`object`||!e))for(let i of Object.keys(e)){let e=n.get(i);e?r.push(U(`DUPLICATE_FIELD_ID`,`Field ID '${i}' is not unique; already used at ${e}`,`${t}.${i}`)):n.set(i,`${t}.${i}`)}};t.fields&&i(t.fields,`fields`);let a=t.schemas;if(a&&typeof a==`object`){for(let[e,t]of Object.entries(a))if(typeof t==`object`&&t){let n=t;n.fields&&i(n.fields,`schemas.${e}.fields`)}}return r},constraint_fields_exist:e=>{let t=e.root,n=[],r=(e,n)=>{let r=e.split(`.`),i=n;for(let e=0;e<r.length;e++){let n=r[e],a=i[n];if(a===void 0)return!1;if(e===r.length-1)return!0;let o=a;if(o.type===`object`){let e=o.schema;if(e&&e.id){let n=$(t,String(e.id));if(n&&n.fields){i=n.fields;continue}}}return!1}return!1},i=(e,t,i)=>{for(let[a,o]of Object.entries(e)){if(typeof o!=`object`||!o)continue;let e=o;if(e.predicate&&e.fields){let o=e.fields;Array.isArray(o)&&o.forEach((e,o)=>{typeof e==`string`&&(r(e,t)||n.push(U(`CONSTRAINT_FIELD_NOT_FOUND`,`Constraint '${a}' references non-existent field path '${e}'`,`${i}constraints.${a}.fields[${o}]`)))})}}},a=t.fields,o=t.constraints;o&&a&&i(o,a,``);let s=t.schemas;if(s){for(let[e,t]of Object.entries(s))if(typeof t==`object`&&t){let n=t,r=n.constraints,a=n.fields;r&&a&&i(r,a,`schemas.${e}.`)}}return n},inline_type_descriptor_valid:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.schema;if(!n||typeof n!=`object`||Array.isArray(n))return[];let r=t.type,i=n.type;if(r===void 0||i===void 0)return[];let a=new Set([`record`,`array`,`set`,`enum`]),o=String(r);if(!a.has(o))return[U(`INLINE_DESCRIPTOR_NOT_ALLOWED_FOR_FIELD_TYPE`,`Inline type descriptors are only allowed for fields of type record, array, set, or enum, but got '${o}'`,``)];let s=String(i);if(!new Set([`string`,`number`,`integer`,`decimal`,`boolean`,`bytes`,`unknown`,`record`]).has(s))return[U(`INLINE_DESCRIPTOR_INVALID_TYPE`,`Inline descriptor type '${s}' is not allowed (only primitives and 'record')`,``)];let c=n.values;if(c!=null){if(s!==`string`&&!Q(s))return[U(`INLINE_DESCRIPTOR_VALUES_WITHOUT_ENUM`,`'values' can only be used with string or numeric inline types`,``)];if(!Array.isArray(c)||c.length===0)return[U(`INLINE_DESCRIPTOR_VALUES_NOT_ARRAY`,`'values' must be a non‑empty array`,``)]}return[]},schema_reference_form_correct:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r)return[];let i=t.schema;if(i==null)return[];let a=e=>({isNamed:`id`in e,isInline:`type`in e});if(Array.isArray(i)){if(i.length===0)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires at least one reference`,``)];if(n!==`union`&&n!==`composite`&&n!==`enum`)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' cannot use an array of references`,``)];for(let e=0;e<i.length;e++){let t=i[e];if(typeof t!=`object`||!t)return[U(`SCHEMA_REF_FORM_INVALID`,`Element ${e} of schema array is not an object`,``)];let{isNamed:r,isInline:o}=a(t);if(n===`enum`){if(!r&&!o)return[U(`SCHEMA_REF_FORM_INVALID`,`Enum array element ${e} must be a named reference or inline descriptor`,``)]}else if(!r)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' array element ${e} must be a named schema reference (with 'id')`,``)]}}else if(typeof i==`object`){let{isNamed:e,isInline:t}=a(i);switch(n){case`array`:case`set`:case`record`:case`enum`:if(!e&&!t)return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires a single named reference or an inline descriptor`,``)];break;case`object`:if(!e)return[U(`SCHEMA_REF_FORM_INVALID`,`Object field requires a single named schema reference`,``)];break;case`union`:case`composite`:return[U(`SCHEMA_REF_FORM_INVALID`,`Field type '${n}' requires an array of references`,``)]}}return[]},collection_requires_schema:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r)return[];let i=t.schema;return Un(n)&&i==null?[U(`COLLECTION_MISSING_SCHEMA`,`Collection type '${n}' must have a schema reference`,``)]:[]},union_requires_multiple_schemas:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`union`)return[];let i=t.schema;return i==null?[U(`UNION_MISSING_SCHEMA`,`Union type must have schema references`,``)]:Array.isArray(i)?i.length<2?[U(`UNION_INSUFFICIENT_SCHEMAS`,`Union type must have at least 2 schema references, got ${i.length}`,``)]:[]:[U(`UNION_SCHEMA_NOT_ARRAY`,`Union type schema must be an array of schema references`,``)]},record_schema_cardinality:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let[n,r]=Z(t);if(!r||n!==`record`)return[];let i=t.schema;return Array.isArray(i)?[U(`RECORD_SCHEMA_ARRAY`,`Record type must have zero or one schema reference, not an array`,``)]:[]},nested_schema_exclusive_mode:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!1;for(let e of zn){let r=t[e];if(typeof r==`object`&&r&&Object.keys(r).length>0){n=!0;break}}let r=!1;for(let e of Bn)if(t[e]!=null){r=!0;break}return n&&r?[U(`NESTED_SCHEMA_MIXED_MODE`,`NestedSchema cannot mix BaseSchema fields (fields/indexes/constraints) with FieldProperties (type/values/schema)`,``)]:!n&&!r?[U(`NESTED_SCHEMA_NO_MODE`,`NestedSchema must have either BaseSchema fields or FieldProperties, not neither`,``)]:[]},constraint_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=String(t.name||`UNNAMED`),r=!!(t.predicate&&String(t.predicate)!==``),i=!!(t.operator&&Array.isArray(t.rules)&&t.rules.length>0);return r&&i?[U(`CONSTRAINT_MIXED_TYPE`,`Constraint cannot have both predicate (rule) and operator+rules (group)`,n)]:!r&&!i?[U(`CONSTRAINT_NO_TYPE`,`Constraint must have either predicate (rule) or operator+rules (group)`,n)]:[]},constraint_rule_requires_predicate:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.predicate;return n===void 0?[U(`REQUIRED_FIELD_MISSING`,`Required field 'predicate' is missing`,`predicate`)]:n===null?[U(`REQUIRED_FIELD_NULL`,`Required field 'predicate' cannot be null`,`predicate`)]:typeof n==`string`?n.trim()===``?[U(`REQUIRED_FIELD_EMPTY`,`Required field 'predicate' cannot be empty`,`predicate`)]:[]:[U(`REQUIRED_FIELD_WRONG_TYPE`,`Required field 'predicate' must be a string`,`predicate`)]},index_condition_type_exclusive:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=!!(t.field&&t.operator&&t.value!==void 0),r=!!(t.operator&&Array.isArray(t.conditions)&&t.conditions.length>0);return n&&r?[U(`INDEX_CONDITION_MIXED_TYPE`,`IndexCondition cannot have both single condition fields and group fields`,``)]:!n&&!r?[U(`INDEX_CONDITION_NO_TYPE`,`IndexCondition must be either a single condition or a group`,``)]:[]},schema_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[U(`SCHEMA_NAME_MISSING`,`Schema name is required`,`name`)]:[]},field_name_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.name;return n==null||String(n).trim()===``?[U(`FIELD_NAME_MISSING`,`Field name is required`,`name`)]:[]},index_fields_not_empty:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.fields;return!Array.isArray(n)||n.length===0?[U(`INDEX_FIELDS_EMPTY`,`Index must reference at least one field`,`fields`)]:[]},schema_reference_id_required:e=>{let t=e.data;if(typeof t!=`object`||!t)return[];let n=t.id;return n==null||String(n).trim()===``?[U(`SCHEMA_REFERENCE_ID_MISSING`,`SchemaReference ID is required`,`id`)]:[]},index_fields_reference_valid:e=>[]};var Zn=class{static validator=null;static async validate(e){return this.validator||=await Fn.create(In,Xn),await this.validator.validate(e)}};export{Nt as AnansiCodec,c as Compiler,u as DataTypes,Fn as DocumentValidator,f as FD_NO_CHILD,Ye as FLAG_COMPRESSED,Xe as FLAG_ENCRYPTED,Ze as FLAG_HASH_PRESENT,n as Literal,p as MAX_SCHEMA_SLOTS,m as MULTI_STEP_BASE,Jt as MigrationEngine,L as MigrationError,Vt as MigrationErrorCode,Zn as SchemaValidator,le as addressForSteps,l as buildEnum,me as buildManifest,Lt as bumpVersion,It as calculateNextBump,Ft as classifyChangeImpact,Mt as decodeAnansiBatch,Ot as decodeAnansiPacket,ft as decodeBatch,ct as decodeDocument,zt as deepMerge,Yt as defaultValidationConfig,jt as encodeAnansiBatchColumnar,At as encodeAnansiBatchRows,Dt as encodeAnansiPacket,ut as encodeBatchColumnar,lt as encodeBatchRows,st as encodeDocument,ee as internalDP,de as link,te as makeDescriptor,Xn as metaSchemaPredicateMap,r as parseSchema,Pt as schemaChangeToPatch,Bt as sha256,h as unpackDescriptor,ue as userDataDP};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asaidimu/anansi",
|
|
3
|
-
"version": "8.6.
|
|
3
|
+
"version": "8.6.8",
|
|
4
4
|
"description": "Self-contained TypeScript implementation of the Anansi binary wire format — schema compile/link/addressing, dense/sparse/batch packet codecs, transforms (zstd/BLAKE3/AES-GCM), and document validation. Byte-compatible with the Go implementation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|