Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@preserves/schema

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@preserves/schema - npm Package Compare versions

Comparing version 0.21.9 to 0.21.10

.yarnrc

2

dist/preserves-schema.es6.min.js

@@ -1,1 +0,1 @@

import*as e from"@preserves/core";import{is as t,stringify as r,KeyedDictionary as n,Reader as i,genericEmbeddedTypeDecode as o,peel as a,position as s,preserves as c,Record as d,Dictionary as u,isCompound as l,isEmbedded as f,strip as b,Annotated as m,annotate as v,annotations as _,KeyedSet as p,FlexSet as h,fold as y,encode as S}from"@preserves/core";const g=1,w=e.Symbol.for("Boolean"),$=e.Symbol.for("ByteString"),P=e.Symbol.for("Double"),N=e.Symbol.for("Float"),k=e.Symbol.for("SignedInteger"),A=e.Symbol.for("String"),E=e.Symbol.for("Symbol"),R=e.Symbol.for("and"),D=e.Symbol.for("any"),T=e.Symbol.for("atom"),I=e.Symbol.for("bundle"),x=e.Symbol.for("definitions"),B=e.Symbol.for("dict"),C=e.Symbol.for("dictof"),M=e.Symbol.for("embedded"),L=e.Symbol.for("embeddedType"),V=e.Symbol.for("lit"),F=e.Symbol.for("named"),O=e.Symbol.for("or"),K=e.Symbol.for("rec"),q=e.Symbol.for("ref"),z=e.Symbol.for("schema"),J=e.Symbol.for("seqof"),j=e.Symbol.for("setof"),U=e.Symbol.for("tuple"),H=e.Symbol.for("tuplePrefix"),Y=e.Symbol.for("version"),Z=!1;let G=null;function Q(){return null===G&&(G=e.decode(e.Bytes.fromHex("b4b306736368656d61b7b30776657273696f6e91b30b646566696e6974696f6e73b7b303526566b4b303726563b4b3036c6974b30372656684b4b3057475706c65b5b4b3056e616d6564b3066d6f64756c65b4b303726566b584b30a4d6f64756c65506174688484b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484848484b30642756e646c65b4b303726563b4b3036c6974b30662756e646c6584b4b3057475706c65b5b4b3056e616d6564b3076d6f64756c6573b4b303726566b584b3074d6f64756c65738484848484b306536368656d61b4b303726563b4b3036c6974b306736368656d6184b4b3057475706c65b5b4b30464696374b7b30776657273696f6eb4b3056e616d6564b30776657273696f6eb4b303726566b584b30756657273696f6e8484b30b646566696e6974696f6e73b4b3056e616d6564b30b646566696e6974696f6e73b4b303726566b584b30b446566696e6974696f6e738484b30c656d62656464656454797065b4b3056e616d6564b30c656d62656464656454797065b4b303726566b584b310456d626564646564547970654e616d6584848484848484b30742696e64696e67b4b303726563b4b3036c6974b3056e616d656484b4b3057475706c65b5b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e8484848484b3074d6f64756c6573b4b306646963746f66b4b303726566b584b30a4d6f64756c655061746884b4b303726566b584b306536368656d618484b3075061747465726eb4b3026f72b5b5b10d53696d706c655061747465726eb4b303726566b584b30d53696d706c655061747465726e8484b5b10f436f6d706f756e645061747465726eb4b303726566b584b30f436f6d706f756e645061747465726e84848484b30756657273696f6eb4b3036c69749184b30841746f6d4b696e64b4b3026f72b5b5b107426f6f6c65616eb4b3036c6974b307426f6f6c65616e8484b5b105466c6f6174b4b3036c6974b305466c6f61748484b5b106446f75626c65b4b3036c6974b306446f75626c658484b5b10d5369676e6564496e7465676572b4b3036c6974b30d5369676e6564496e74656765728484b5b106537472696e67b4b3036c6974b306537472696e678484b5b10a42797465537472696e67b4b3036c6974b30a42797465537472696e678484b5b10653796d626f6cb4b3036c6974b30653796d626f6c84848484b30a446566696e6974696f6eb4b3026f72b5b5b1026f72b4b303726563b4b3036c6974b3026f7284b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b3104e616d6564416c7465726e61746976658484b4b3056e616d6564b3087061747465726e31b4b303726566b584b3104e616d6564416c7465726e6174697665848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b3104e616d6564416c7465726e61746976658484848484848484b5b103616e64b4b303726563b4b3036c6974b303616e6484b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3087061747465726e31b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e8484848484848484b5b1075061747465726eb4b303726566b584b3075061747465726e84848484b30a4d6f64756c6550617468b4b3057365716f66b4b30461746f6db30653796d626f6c8484b30b446566696e6974696f6e73b4b306646963746f66b4b30461746f6db30653796d626f6c84b4b303726566b584b30a446566696e6974696f6e8484b30c4e616d65645061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b3075061747465726e84848484b30d53696d706c655061747465726eb4b3026f72b5b5b103616e79b4b3036c6974b303616e798484b5b10461746f6db4b303726563b4b3036c6974b30461746f6d84b4b3057475706c65b5b4b3056e616d6564b30861746f6d4b696e64b4b303726566b584b30841746f6d4b696e64848484848484b5b108656d626564646564b4b303726563b4b3036c6974b308656d62656464656484b4b3057475706c65b5b4b3056e616d6564b309696e74657266616365b4b303726566b584b30d53696d706c655061747465726e848484848484b5b1036c6974b4b303726563b4b3036c6974b3036c697484b4b3057475706c65b5b4b3056e616d6564b30576616c7565b303616e798484848484b5b1057365716f66b4b303726563b4b3036c6974b3057365716f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b1057365746f66b4b303726563b4b3036c6974b3057365746f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b106646963746f66b4b303726563b4b3036c6974b306646963746f6684b4b3057475706c65b5b4b3056e616d6564b3036b6579b4b303726566b584b30d53696d706c655061747465726e8484b4b3056e616d6564b30576616c7565b4b303726566b584b30d53696d706c655061747465726e848484848484b5b103526566b4b303726566b584b30352656684848484b30f436f6d706f756e645061747465726eb4b3026f72b5b5b103726563b4b303726563b4b3036c6974b30372656384b4b3057475706c65b5b4b3056e616d6564b3056c6162656cb4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3066669656c6473b4b303726566b584b30c4e616d65645061747465726e848484848484b5b1057475706c65b4b303726563b4b3036c6974b3057475706c6584b4b3057475706c65b5b4b3056e616d6564b3087061747465726e73b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e84848484848484b5b10b7475706c65507265666978b4b303726563b4b3036c6974b30b7475706c6550726566697884b4b3057475706c65b5b4b3056e616d6564b3056669786564b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087661726961626c65b4b303726566b584b3124e616d656453696d706c655061747465726e848484848484b5b10464696374b4b303726563b4b3036c6974b3046469637484b4b3057475706c65b5b4b3056e616d6564b307656e7472696573b4b303726566b584b31144696374696f6e617279456e74726965738484848484848484b310456d626564646564547970654e616d65b4b3026f72b5b5b10566616c7365b4b3036c6974808484b5b103526566b4b303726566b584b30352656684848484b3104e616d6564416c7465726e6174697665b4b3057475706c65b5b4b3056e616d6564b30c76617269616e744c6162656cb4b30461746f6db306537472696e678484b4b3056e616d6564b3077061747465726eb4b303726566b584b3075061747465726e84848484b31144696374696f6e617279456e7472696573b4b306646963746f66b303616e79b4b303726566b584b3124e616d656453696d706c655061747465726e8484b3124e616d656453696d706c655061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b30d53696d706c655061747465726e8484848484b30c656d62656464656454797065808484"))),G}const W={};function X(e){return{modules:e,__as_preserve__(){return he(this)}}}function ee(e){return e}function te({version:e,embeddedType:t,definitions:r}){return{version:e,embeddedType:t,definitions:r,__as_preserve__(){return we(this)}}}function re(){return{__as_preserve__:()=>Ne()}}var ne,ie,oe,ae,se,ce,de,ue;function le(e){return e}function fe(e){return e}function be({variantLabel:e,pattern:t}){return{variantLabel:e,pattern:t,__as_preserve__(){return ze(this)}}}function me({name:e,pattern:t}){return{name:e,pattern:t,__as_preserve__(){return Ze(this)}}}function ve({module:e,name:t}){return{module:e,name:t,__as_preserve__(){return Qe(this)}}}function _e(e){return e}function pe(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,I)?{}:void 0,void 0!==n){let e;e=ye(t[0]),void 0!==e&&(r={modules:e,__as_preserve__(){return he(this)}})}}return r}function he(t){return e.Record(I,[Se(t.modules)])}function ye(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t=We(e),void 0!==t){let e;if(e=ge(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Se(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[Xe(e),we(t)])))}function ge(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,z)?{}:void 0,void 0!==n&&e.Dictionary.isDictionary(t[0])){let e;if(void 0!==(e=t[0].get(Y))){let n;if(n=Pe(e),void 0!==n){let e;if(void 0!==(e=t[0].get(L))){let i;if(i=ke(e),void 0!==i){let e;if(void 0!==(e=t[0].get(x))){let t;t=Ee(e),void 0!==t&&(r={version:n,embeddedType:i,definitions:t,__as_preserve__(){return we(this)}})}}}}}}}return r}function we(t){return e.Record(z,[new e.Dictionary([[Y,Ne(t.version)],[L,Ae(t.embeddedType)],[x,Re(t.definitions)]])])}function $e(t){let r=Pe(t);if(void 0===r)throw new TypeError(`Invalid Version: ${e.stringify(t)}`);return r}function Pe(t){let r,n;return r=e.is(t,g)?{}:void 0,void 0!==r&&(n={__as_preserve__:()=>Ne()}),n}function Ne(e){return g}function ke(t){let r,n;if(r=e.is(t,Z)?{}:void 0,void 0!==r&&(n={_variant:"false",__as_preserve__(){return Ae(this)}}),void 0===n){let e;e=Ge(t),void 0!==e&&(n={_variant:"Ref",value:e,__as_preserve__(){return Ae(this)}})}return n}function Ae(e){switch(e._variant){case"false":return Z;case"Ref":return Qe(e.value)}}function Ee(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t="symbol"==typeof e?e:void 0,void 0!==t){let e;if(e=De(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Re(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[e,Te(t)])))}function De(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,O)?{}:void 0,void 0!==n&&e.isSequence(t[0])&&t[0].length>=2){let e;if(e=qe(t[0][0]),void 0!==e){let n;if(n=qe(t[0][1]),void 0!==n){let i,o;i=t[0].slice(2),o=[];for(const e of i){let t;if(t=qe(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(r={_variant:"or",pattern0:e,pattern1:n,patternN:o,__as_preserve__(){return Te(this)}})}}}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,R)?{}:void 0,void 0!==n&&e.isSequence(t[0])&&t[0].length>=2){let e;if(e=Ue(t[0][0]),void 0!==e){let n;if(n=Ue(t[0][1]),void 0!==n){let i,o;i=t[0].slice(2),o=[];for(const e of i){let t;if(t=Ue(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(r={_variant:"and",pattern0:e,pattern1:n,patternN:o,__as_preserve__(){return Te(this)}})}}}}if(void 0===r){let e;e=Ie(t),void 0!==e&&(r={_variant:"Pattern",value:e,__as_preserve__(){return Te(this)}})}}return r}function Te(t){switch(t._variant){case"or":return e.Record(O,[[ze(t.pattern0),ze(t.pattern1),...t.patternN.map((e=>ze(e)))]]);case"and":return e.Record(R,[[He(t.pattern0),He(t.pattern1),...t.patternN.map((e=>He(e)))]]);case"Pattern":return xe(t.value)}}function Ie(e){let t,r;if(t=Be(e),void 0!==t&&(r={_variant:"SimplePattern",value:t,__as_preserve__(){return xe(this)}}),void 0===r){let t;t=Me(e),void 0!==t&&(r={_variant:"CompoundPattern",value:t,__as_preserve__(){return xe(this)}})}return r}function xe(e){switch(e._variant){case"SimplePattern":return Ce(e.value);case"CompoundPattern":return Le(e.value)}}function Be(t){let r,n;if(r=e.is(t,D)?{}:void 0,void 0!==r&&(n={_variant:"any",__as_preserve__(){return Ce(this)}}),void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,T)?{}:void 0,void 0!==r){let e;e=Oe(t[0]),void 0!==e&&(n={_variant:"atom",atomKind:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,M)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"embedded",interface:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,V)?{}:void 0,void 0!==r){let e;e=t[0],void 0!==e&&(n={_variant:"lit",value:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,J)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"seqof",pattern:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,j)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"setof",pattern:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,C)?{}:void 0,void 0!==r){let e;if(e=Be(t[0]),void 0!==e){let r;r=Be(t[1]),void 0!==r&&(n={_variant:"dictof",key:e,value:r,__as_preserve__(){return Ce(this)}})}}}if(void 0===n){let e;e=Ge(t),void 0!==e&&(n={_variant:"Ref",value:e,__as_preserve__(){return Ce(this)}})}}}}}}}return n}function Ce(t){switch(t._variant){case"any":return D;case"atom":return e.Record(T,[Ke(t.atomKind)]);case"embedded":return e.Record(M,[Ce(t.interface)]);case"lit":return e.Record(V,[t.value]);case"seqof":return e.Record(J,[Ce(t.pattern)]);case"setof":return e.Record(j,[Ce(t.pattern)]);case"dictof":return e.Record(C,[Ce(t.key),Ce(t.value)]);case"Ref":return Qe(t.value)}}function Me(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,K)?{}:void 0,void 0!==n){let e;if(e=Ue(t[0]),void 0!==e){let n;n=Ue(t[1]),void 0!==n&&(r={_variant:"rec",label:e,fields:n,__as_preserve__(){return Le(this)}})}}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,U)?{}:void 0,void 0!==n){let n;if(n=void 0,e.isSequence(t[0])){n=[];for(const e of t[0]){let t;if(t=Ue(e),void 0===t){n=void 0;break}n.push(t)}}void 0!==n&&(r={_variant:"tuple",patterns:n,__as_preserve__(){return Le(this)}})}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,H)?{}:void 0,void 0!==n){let n;if(n=void 0,e.isSequence(t[0])){n=[];for(const e of t[0]){let t;if(t=Ue(e),void 0===t){n=void 0;break}n.push(t)}}if(void 0!==n){let e;e=Je(t[1]),void 0!==e&&(r={_variant:"tuplePrefix",fixed:n,variable:e,__as_preserve__(){return Le(this)}})}}}if(void 0===r&&e.Record.isRecord(t)){let n;if(n=e.is(t.label,B)?{}:void 0,void 0!==n){let e;e=Ve(t[0]),void 0!==e&&(r={_variant:"dict",entries:e,__as_preserve__(){return Le(this)}})}}}}return r}function Le(t){switch(t._variant){case"rec":return e.Record(K,[He(t.label),He(t.fields)]);case"tuple":return e.Record(U,[t.patterns.map((e=>He(e)))]);case"tuplePrefix":return e.Record(H,[t.fixed.map((e=>He(e))),je(t.variable)]);case"dict":return e.Record(B,[Fe(t.entries)])}}function Ve(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t=e,void 0!==t){let e;if(e=Je(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Fe(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[e,je(t)])))}function Oe(t){let r,n;if(r=e.is(t,w)?{}:void 0,void 0!==r&&(n={_variant:"Boolean",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,N)?{}:void 0,void 0!==r&&(n={_variant:"Float",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,P)?{}:void 0,void 0!==r&&(n={_variant:"Double",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,k)?{}:void 0,void 0!==r&&(n={_variant:"SignedInteger",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,A)?{}:void 0,void 0!==r&&(n={_variant:"String",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,$)?{}:void 0,void 0!==r&&(n={_variant:"ByteString",__as_preserve__(){return Ke(this)}}),void 0===n){let r;r=e.is(t,E)?{}:void 0,void 0!==r&&(n={_variant:"Symbol",__as_preserve__(){return Ke(this)}})}}}}}}return n}function Ke(e){switch(e._variant){case"Boolean":return w;case"Float":return N;case"Double":return P;case"SignedInteger":return k;case"String":return A;case"ByteString":return $;case"Symbol":return E}}function qe(t){let r;if(e.isSequence(t)&&2===t.length){let e;if(e="string"==typeof t[0]?t[0]:void 0,void 0!==e){let n;n=Ie(t[1]),void 0!==n&&(r={variantLabel:e,pattern:n,__as_preserve__(){return ze(this)}})}}return r}function ze(e){return[e.variantLabel,xe(e.pattern)]}function Je(e){let t,r;if(t=Ye(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return je(this)}}),void 0===r){let t;t=Be(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return je(this)}})}return r}function je(e){switch(e._variant){case"named":return Ze(e.value);case"anonymous":return Ce(e.value)}}function Ue(e){let t,r;if(t=Ye(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return He(this)}}),void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return He(this)}})}return r}function He(e){switch(e._variant){case"named":return Ze(e.value);case"anonymous":return xe(e.value)}}function Ye(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,F)?{}:void 0,void 0!==n){let e;if(e="symbol"==typeof t[0]?t[0]:void 0,void 0!==e){let n;n=Be(t[1]),void 0!==n&&(r={name:e,pattern:n,__as_preserve__(){return Ze(this)}})}}}return r}function Ze(t){return e.Record(F,[t.name,Ce(t.pattern)])}function Ge(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,q)?{}:void 0,void 0!==n){let e;if(e=We(t[0]),void 0!==e){let n;n="symbol"==typeof t[1]?t[1]:void 0,void 0!==n&&(r={module:e,name:n,__as_preserve__(){return Qe(this)}})}}}return r}function Qe(t){return e.Record(q,[Xe(t.module),t.name])}function We(t){let r,n;if(r=void 0,e.isSequence(t)){r=[];for(const e of t){let t;if(t="symbol"==typeof e?e:void 0,void 0===t){r=void 0;break}r.push(t)}}return void 0!==r&&(n=r),n}function Xe(e){return e.map((e=>e))}X.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Bundle")}},ee.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Modules")}},te.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Schema")}},re.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Version")}},function(t){function r(){return{_variant:"false",__as_preserve__(){return Ae(this)}}}function n(e){return{_variant:"Ref",value:e,__as_preserve__(){return Ae(this)}}}t.$false=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("EmbeddedTypeName"),variant:e.Symbol.for("false")}},t.Ref=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("EmbeddedTypeName"),variant:e.Symbol.for("Ref")}}}(ne||(ne={})),le.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definitions")}},function(t){function r({pattern0:e,pattern1:t,patternN:r}){return{_variant:"or",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return Te(this)}}}function n({pattern0:e,pattern1:t,patternN:r}){return{_variant:"and",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return Te(this)}}}function i(e){return{_variant:"Pattern",value:e,__as_preserve__(){return Te(this)}}}t.or=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("or")}},t.and=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("and")}},t.Pattern=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("Pattern")}}}(ie||(ie={})),function(t){function r(e){return{_variant:"SimplePattern",value:e,__as_preserve__(){return xe(this)}}}function n(e){return{_variant:"CompoundPattern",value:e,__as_preserve__(){return xe(this)}}}t.SimplePattern=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Pattern"),variant:e.Symbol.for("SimplePattern")}},t.CompoundPattern=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Pattern"),variant:e.Symbol.for("CompoundPattern")}}}(oe||(oe={})),function(t){function r(){return{_variant:"any",__as_preserve__(){return Ce(this)}}}function n(e){return{_variant:"atom",atomKind:e,__as_preserve__(){return Ce(this)}}}function i(e){return{_variant:"embedded",interface:e,__as_preserve__(){return Ce(this)}}}function o(e){return{_variant:"lit",value:e,__as_preserve__(){return Ce(this)}}}function a(e){return{_variant:"seqof",pattern:e,__as_preserve__(){return Ce(this)}}}function s(e){return{_variant:"setof",pattern:e,__as_preserve__(){return Ce(this)}}}function c({key:e,value:t}){return{_variant:"dictof",key:e,value:t,__as_preserve__(){return Ce(this)}}}function d(e){return{_variant:"Ref",value:e,__as_preserve__(){return Ce(this)}}}t.any=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("any")}},t.atom=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("atom")}},t.embedded=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("embedded")}},t.lit=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("lit")}},t.seqof=a,a.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("seqof")}},t.setof=s,s.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("setof")}},t.dictof=c,c.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("dictof")}},t.Ref=d,d.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("Ref")}}}(ae||(ae={})),function(t){function r({label:e,fields:t}){return{_variant:"rec",label:e,fields:t,__as_preserve__(){return Le(this)}}}function n(e){return{_variant:"tuple",patterns:e,__as_preserve__(){return Le(this)}}}function i({fixed:e,variable:t}){return{_variant:"tuplePrefix",fixed:e,variable:t,__as_preserve__(){return Le(this)}}}function o(e){return{_variant:"dict",entries:e,__as_preserve__(){return Le(this)}}}t.rec=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("rec")}},t.tuple=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("tuple")}},t.tuplePrefix=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("tuplePrefix")}},t.dict=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("dict")}}}(se||(se={})),fe.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("DictionaryEntries")}},function(t){function r(){return{_variant:"Boolean",__as_preserve__(){return Ke(this)}}}function n(){return{_variant:"Float",__as_preserve__(){return Ke(this)}}}function i(){return{_variant:"Double",__as_preserve__(){return Ke(this)}}}function o(){return{_variant:"SignedInteger",__as_preserve__(){return Ke(this)}}}function a(){return{_variant:"String",__as_preserve__(){return Ke(this)}}}function s(){return{_variant:"ByteString",__as_preserve__(){return Ke(this)}}}function c(){return{_variant:"Symbol",__as_preserve__(){return Ke(this)}}}t.Boolean=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Boolean")}},t.Float=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Float")}},t.Double=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Double")}},t.SignedInteger=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("SignedInteger")}},t.String=a,a.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("String")}},t.ByteString=s,s.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("ByteString")}},t.Symbol=c,c.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Symbol")}}}(ce||(ce={})),be.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedAlternative")}},function(t){function r(e){return{_variant:"named",value:e,__as_preserve__(){return je(this)}}}function n(e){return{_variant:"anonymous",value:e,__as_preserve__(){return je(this)}}}t.named=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedSimplePattern"),variant:e.Symbol.for("named")}},t.anonymous=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedSimplePattern"),variant:e.Symbol.for("anonymous")}}}(de||(de={})),function(t){function r(e){return{_variant:"named",value:e,__as_preserve__(){return He(this)}}}function n(e){return{_variant:"anonymous",value:e,__as_preserve__(){return He(this)}}}t.named=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedPattern"),variant:e.Symbol.for("named")}},t.anonymous=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedPattern"),variant:e.Symbol.for("anonymous")}}}(ue||(ue={})),me.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Binding")}},ve.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Ref")}},_e.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("ModulePath")}},X.__from_preserve__=pe,ee.__from_preserve__=ye,te.__from_preserve__=ge,re.__from_preserve__=Pe,function(e){e.__from_preserve__=ke}(ne||(ne={})),le.__from_preserve__=Ee,function(e){e.__from_preserve__=De}(ie||(ie={})),function(e){e.__from_preserve__=Ie}(oe||(oe={})),function(e){e.__from_preserve__=Be}(ae||(ae={})),function(e){e.__from_preserve__=Me}(se||(se={})),fe.__from_preserve__=Ve,function(e){e.__from_preserve__=Oe}(ce||(ce={})),be.__from_preserve__=qe,function(e){e.__from_preserve__=Je}(de||(de={})),function(e){e.__from_preserve__=Ue}(ue||(ue={})),me.__from_preserve__=Ye,ve.__from_preserve__=Ge,_e.__from_preserve__=We;const et=new Set(["abstract","await","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete","do","double","else","enum","export","extends","false","final","finally","float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield"]);function tt(e){return e.match(/^(.*\.)?([^.]+)$/)[2]}function rt(e){return/^[a-zA-Z][a-zA-Z_0-9]*$/.test(e)}function nt(e){return e.split(".").every(rt)}function it(e){return/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(e)&&!function(e){return et.has(e)}(e)}function ot(e,t){return at(e.replace("$","$$"),t)}function at(e,t){if(it(e))return e;if(it("$"+e))return"$"+e;if(void 0!==t)return t();throw new Error(`Internal error: jsToken needs to be completed (${e})`)}const st=Symbol.for("&"),ct=Symbol.for("."),dt=Symbol.for("..."),ut=Symbol.for("="),lt=Symbol.for("include"),ft=Symbol.for("/");function bt(e,r){for(const n of e)if(t(n.schemaModulePath,r))return n;return null}function mt(e){return"_i_"+e.schemaModulePath.map((e=>e.description)).join("$")}function vt(e){return e.map((e=>e.description)).join(".")}function _t(e){return[...e.module,e.name].map((e=>e.description)).join(".")}function pt(e){return"named"===e._variant?oe.SimplePattern(e.value.pattern):e.value}function ht(e){return"named"===e._variant?e:ue.anonymous(oe.SimplePattern(e.value))}function yt(e){return"named"===e._variant?e.value.name.description:void 0}function St(e){return ue.anonymous(oe.SimplePattern(e))}function gt(e){return"string"==typeof e?e:"symbol"==typeof e?e.description:"number"==typeof e||"boolean"==typeof e?""+e:void 0}var wt,$t=Object.freeze({__proto__:null,$1:g,$Boolean:w,$ByteString:$,$Double:P,$Float:N,$SignedInteger:k,$String:A,$Symbol:E,$and:R,$any:D,$atom:T,$bundle:I,$definitions:x,$dict:B,$dictof:C,$embedded:M,$embeddedType:L,$lit:V,$named:F,$or:O,$rec:K,$ref:q,$schema:z,$seqof:J,$setof:j,$tuple:U,$tuplePrefix:H,$version:Y,ANDSYM:st,get AtomKind(){return ce},Binding:me,Bundle:X,get CompoundPattern(){return se},DOT:ct,DOTDOTDOT:dt,get Definition(){return ie},Definitions:le,DictionaryEntries:fe,EQUALS:ut,get EmbeddedTypeName(){return ne},INCLUDE:lt,ModulePath:_e,Modules:ee,NamedAlternative:be,get NamedPattern(){return ue},get NamedSimplePattern(){return de},ORSYM:ft,get Pattern(){return oe},Ref:ve,Schema:te,get SimplePattern(){return ae},Version:re,__lit6:Z,_imports:W,_schema:Q,anonymousSimplePattern:St,asAtomKind:function(t){let r=Oe(t);if(void 0===r)throw new TypeError(`Invalid AtomKind: ${e.stringify(t)}`);return r},asBinding:function(t){let r=Ye(t);if(void 0===r)throw new TypeError(`Invalid Binding: ${e.stringify(t)}`);return r},asBundle:function(t){let r=pe(t);if(void 0===r)throw new TypeError(`Invalid Bundle: ${e.stringify(t)}`);return r},asCompoundPattern:function(t){let r=Me(t);if(void 0===r)throw new TypeError(`Invalid CompoundPattern: ${e.stringify(t)}`);return r},asDefinition:function(t){let r=De(t);if(void 0===r)throw new TypeError(`Invalid Definition: ${e.stringify(t)}`);return r},asDefinitions:function(t){let r=Ee(t);if(void 0===r)throw new TypeError(`Invalid Definitions: ${e.stringify(t)}`);return r},asDictionaryEntries:function(t){let r=Ve(t);if(void 0===r)throw new TypeError(`Invalid DictionaryEntries: ${e.stringify(t)}`);return r},asEmbeddedTypeName:function(t){let r=ke(t);if(void 0===r)throw new TypeError(`Invalid EmbeddedTypeName: ${e.stringify(t)}`);return r},asModulePath:function(t){let r=We(t);if(void 0===r)throw new TypeError(`Invalid ModulePath: ${e.stringify(t)}`);return r},asModules:function(t){let r=ye(t);if(void 0===r)throw new TypeError(`Invalid Modules: ${e.stringify(t)}`);return r},asNamedAlternative:function(t){let r=qe(t);if(void 0===r)throw new TypeError(`Invalid NamedAlternative: ${e.stringify(t)}`);return r},asNamedPattern:function(t){let r=Ue(t);if(void 0===r)throw new TypeError(`Invalid NamedPattern: ${e.stringify(t)}`);return r},asNamedSimplePattern:function(t){let r=Je(t);if(void 0===r)throw new TypeError(`Invalid NamedSimplePattern: ${e.stringify(t)}`);return r},asPattern:function(t){let r=Ie(t);if(void 0===r)throw new TypeError(`Invalid Pattern: ${e.stringify(t)}`);return r},asRef:function(t){let r=Ge(t);if(void 0===r)throw new TypeError(`Invalid Ref: ${e.stringify(t)}`);return r},asSchema:function(t){let r=ge(t);if(void 0===r)throw new TypeError(`Invalid Schema: ${e.stringify(t)}`);return r},asSimplePattern:function(t){let r=Be(t);if(void 0===r)throw new TypeError(`Invalid SimplePattern: ${e.stringify(t)}`);return r},asVersion:$e,envLookup:bt,formatModulePath:vt,formatRef:_t,fromAtomKind:Ke,fromBinding:Ze,fromBundle:he,fromCompoundPattern:Le,fromDefinition:Te,fromDefinitions:Re,fromDictionaryEntries:Fe,fromEmbeddedTypeName:Ae,fromModulePath:Xe,fromModules:Se,fromNamedAlternative:ze,fromNamedPattern:He,fromNamedSimplePattern:je,fromPattern:xe,fromRef:Qe,fromSchema:we,fromSimplePattern:Ce,fromVersion:Ne,isValidJsId:it,isValidQid:nt,isValidToken:rt,jsId:ot,jsToken:at,modsymFor:mt,nameFor:yt,namelike:gt,promoteNamedSimplePattern:ht,qidLast:tt,toAtomKind:Oe,toBinding:Ye,toBundle:pe,toCompoundPattern:Me,toDefinition:De,toDefinitions:Ee,toDictionaryEntries:Ve,toEmbeddedTypeName:ke,toModulePath:We,toModules:ye,toNamedAlternative:qe,toNamedPattern:Ue,toNamedSimplePattern:Je,toPattern:Ie,toRef:Ge,toSchema:ge,toSimplePattern:Be,toVersion:Pe,unnamePattern:pt,unnameSimplePattern:function(e){return"named"===e._variant?e.value.pattern:e.value}});function Pt(e){const t=new Nt;return e.definitions.forEach(t.checkDefinition.bind(t)),t.problems.length>0?{ok:!1,problems:t.problems}:{ok:!0,schema:e}}!function(e){e[e.AVAILABLE=0]="AVAILABLE",e[e.NOT_AVAILABLE=1]="NOT_AVAILABLE"}(wt||(wt={}));class Nt{constructor(){this.problems=[]}recordProblem(e,t){this.problems.push(`${t} in ${e}`)}checkBinding(e,t,r){const n=t.description;e.has(n)&&this.recordProblem(r,`duplicate binding named ${JSON.stringify(n)}`),rt(n)||this.recordProblem(r,`invalid binding name ${JSON.stringify(n)}`),e.add(n)}checkDefinition(e,t){switch(e._variant){case"or":{const r=new Set;[e.pattern0,e.pattern1,...e.patternN].forEach((({variantLabel:e,pattern:n})=>{const i=`variant ${e} of ${t.description}`;r.has(e)&&this.recordProblem(i,"duplicate variant label"),rt(e)||this.recordProblem(i,"invalid variant label"),r.add(e),this.checkPattern(new Set,n,i,wt.AVAILABLE)}));break}case"and":{const r=[e.pattern0,e.pattern1,...e.patternN],n=new Set;r.forEach((e=>this.checkNamedPattern(n,e,t.description)));break}case"Pattern":this.checkPattern(new Set,e.value,t.description,wt.AVAILABLE)}}checkNamedPattern(e,t,r){switch(t._variant){case"named":this.checkBinding(e,t.value.name,r),this.checkPattern(e,oe.SimplePattern(t.value.pattern),`${JSON.stringify(t.value.name.description)} of ${r}`,wt.AVAILABLE);break;case"anonymous":this.checkPattern(e,t.value,r,wt.NOT_AVAILABLE)}}checkPattern(e,t,n,i){switch(t._variant){case"SimplePattern":"lit"!==t.value._variant&&i===wt.NOT_AVAILABLE&&this.recordProblem(n,"cannot recover serialization of non-literal pattern"),"Ref"!==t.value._variant||rt(t.value.value.name.description)&&t.value.value.module.every((e=>rt(e.description)))||this.recordProblem(n,"invalid reference name");break;case"CompoundPattern":(t=>{switch(t._variant){case"rec":this.checkNamedPattern(e,t.label,`label of ${n}`),this.checkNamedPattern(e,t.fields,`fields of ${n}`);break;case"tuple":t.patterns.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`)));break;case"tuplePrefix":t.fixed.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`))),this.checkNamedPattern(e,ht(t.variable),`tail of ${n}`);break;case"dict":t.entries.forEach(((t,i)=>this.checkNamedPattern(e,ht(t),`entry ${r(i)} in dictionary in ${n}`)))}})(t.value)}}}class kt extends Error{constructor(e,t){super(e),this.pos=t}}const At=new WeakMap;function Et(e,t){return null===t&&console.error("Internal error in Schema reader: null source position for",e),null!==t&&At.set(e,t),e}function Rt(e){var t;return null!==(t=At.get(e))&&void 0!==t?t:null}function Dt(e,r){const n=[];let i=[];function o(){i.length>0&&(n.push(i),i=[])}for(const n of e)t(n,r)?o():i.push(n);return o(),n}function Tt(e){var t;throw new kt(c`Invalid Schema clause: ${e}`,s(null!==(t=e[0])&&void 0!==t&&t))}function It(e,t,n){throw new kt(`Invalid pattern in ${e}: ${r(t)}`,n)}function xt(e,t){return new i(e,{name:null==t?void 0:t.name,includeAnnotations:!0,embeddedDecode:o}).readToEnd()}function Bt(e,t){const r=Pt(Ct(xt(e,t),null!=t?t:{}));if(r.ok)return r.schema;throw new Error("Schema is not invertible:\n"+r.problems.map((e=>" - "+e)).join("\n"))}function Ct(e,r){let i,o=ne.$false(),d=new n;if(function e(n){const u=Dt(a(n),ct);for(const n of u)if(n.length>=2&&t(n[1],ut)){const e=s(n[0]),t=a(n[0]);if("symbol"!=typeof t&&Tt(n),!rt(t.description))throw new kt(c`Invalid definition name: ${t}`,e);if(d.has(t))throw new kt(c`Duplicate definition: ${n}`,e);d.set(t,Lt(t,e,n.slice(2)))}else if(2===n.length&&t(n[0],Y))i=$e(a(n[1]));else if(2===n.length&&t(n[0],L)){const e=s(n[1]),t=a(n[1]);!1===t?o=ne.$false():"symbol"==typeof t&&nt(t.description)?o=ne.Ref(Kt(t.description,e)):It("embedded type name specification",t,e)}else if(2===n.length&&t(n[0],lt)){const t=s(n[1]),i=a(n[1]);if("string"!=typeof i)throw new kt(c`Invalid include: ${n}`,t);if(void 0===r.readInclude)throw new kt(c`Cannot include files in schema`,t);e(xt(r.readInclude(i),r))}else Tt(n)}(e),void 0===i)throw new kt("Schema: missing version declaration.",null);return te({version:re(),embeddedType:o,definitions:d})}function Mt(e){throw new kt("Named patterns must be Simple patterns",e)}function Lt(e,t,r){function n(r){const n=Ot(r)||Ot(r[0]),i=Ft(e,r);if(!1!==n)return be({variantLabel:n.description,pattern:i});if("CompoundPattern"===i._variant&&"rec"===i.value._variant&&"anonymous"===i.value.label._variant&&"SimplePattern"===i.value.label.value._variant&&"lit"===i.value.label.value.value._variant&&"symbol"==typeof i.value.label.value.value.value&&rt(i.value.label.value.value.value.description))return be({variantLabel:i.value.label.value.value.value.description,pattern:i});if("SimplePattern"===i._variant&&"Ref"===i.value._variant&&nt(i.value.value.name.description))return be({variantLabel:tt(i.value.value.name.description),pattern:i});if("SimplePattern"===i._variant&&"lit"===i.value._variant){const e=gt(i.value.value);if(void 0!==e)return be({variantLabel:e,pattern:i})}throw new kt(c`Name missing for alternative: ${r}`,t)}function i(t){const r=Ot(t)||Ot(t[0]),n=Ft(e,t);return!1!==r?("SimplePattern"!==n._variant&&Mt(s(t[0])),ue.named(me({name:r,pattern:n.value}))):ue.anonymous(n)}const o=Dt(r,st),a=Dt(r,ft);if(0===o.length||0===a.length)throw new kt(c`Invalid Schema clause: ${r}`,t);if(o.length>1&&a.length>1)throw new kt(c`Mixed "or" and "and" clause: ${r}`,t);return o.length>1?ie.and({pattern0:i(o[0]),pattern1:i(o[1]),patternN:o.slice(2).map(i)}):a.length>1?ie.or({pattern0:n(a[0]),pattern1:n(a[1]),patternN:a.slice(2).map(n)}):ie.Pattern(Ft(e,a[0]))}function Vt(e,t){return v(e,..._(t))}function Ft(e,n){function i(n,i,c){const m=s(n),v=a(n);function _(){It(r(e),v,m)}if("symbol"!=typeof v){if(d.isRecord(v)){const e=v.label;return d.isRecord(e)?(0!==e.length&&_(),e.label===V?(1!==v.length&&_(),i(ae.lit(v[0]))):c()):c()}if(Array.isArray(v)&&2===v.length&&t(v[1],dt))return i(ae.seqof(o(v[0])));if(Set.isSet(v)){1!==v.size&&_();const[e]=v.values();return i(ae.setof(o(e)))}if(u.isDictionary(v)&&2===v.size&&v.has(dt)){const e=v.clone();e.delete(dt);const[[t,r]]=e.entries();return i(ae.dictof({key:o(t),value:o(r)}))}return l(v)?c():f(v)?i(ae.embedded(o(v.embeddedValue.generic))):i(ae.lit(b(v)))}{const e=v.description;switch(e){case"any":return i(ae.any());case"bool":return i(ae.atom(ce.Boolean()));case"float":return i(ae.atom(ce.Float()));case"double":return i(ae.atom(ce.Double()));case"int":return i(ae.atom(ce.SignedInteger()));case"string":return i(ae.atom(ce.String()));case"bytes":return i(ae.atom(ce.ByteString()));case"symbol":return i(ae.atom(ce.Symbol()));default:if("="===e[0])return i(ae.lit(Symbol.for(e.slice(1))));if(nt(e))return i(ae.Ref(Kt(e,m)));_()}}}const o=e=>i(e,(e=>e),(()=>{throw new kt("Compound patterns not accepted here",s(e))}));function c(e,t,r,n){return o=>{let a=Ot(o);return!1===a&&void 0!==n&&"symbol"==typeof n&&rt(n.description)&&(a=n),!1===a?t(r(o)):e(me({name:a,pattern:i(o,(e=>e),(()=>Mt(s(o))))}))}}const m=c(ue.named,ue.anonymous,(t=>Ft(e,[t]))),v=c(de.named,de.anonymous,o),_=a(n);return 1!==_.length&&It(r(e),_,_.length>0?s(_[0]):s(_)),i(_[0],oe.SimplePattern,(()=>oe.CompoundPattern(function(n){const i=s(n),l=a(n);function f(){It(r(e),l,i)}if(d.isRecord(l)){const t=l.label;if(!d.isRecord(t))return se.rec({label:ue.anonymous(oe.SimplePattern(ae.lit(t))),fields:ue.anonymous(Ft(e,[Vt([...l],n)]))});if(0!==t.length&&f(),t.label===K)return 2!==l.length&&f(),se.rec({label:m(l[0]),fields:m(l[1])});f()}else{if(Array.isArray(l)&&l.length>2&&t(l[l.length-1],dt)){const e=l[l.length-2],t=Vt([e,dt],e);return se.tuplePrefix({fixed:l.slice(0,l.length-2).map(m),variable:v(t)})}if(Array.isArray(l))return se.tuple(l.map(m));if(u.isDictionary(l)&&!l.has(dt))return se.dict(l.mapEntries((([e,t])=>[b(e),c(de.named,de.anonymous,o,b(e))(t)])));f()}}(_[0]))))}function Ot(e){if(!m.isAnnotated(e))return!1;for(const t of e.annotations){const e=a(t);if("symbol"==typeof e)return rt(e.description)&&e}return!1}function Kt(e,t){const r=e.split(".");return Et(ve({module:r.slice(0,r.length-1).map(Symbol.for),name:Symbol.for(r[r.length-1])}),t)}const qt=80;class zt{constructor(){this.width=qt,this.indentDelta=" ",this.currentIndent="\n",this.buffer=[]}get indentSize(){return this.indentDelta.length}set indentSize(e){this.indentDelta=new Array(e+1).join(" ")}write(e){"string"==typeof e?this.buffer.push(e):e.writeOn(this)}newline(){this.write(this.currentIndent)}toString(){return this.buffer.join("")}withIndent(e){const t=this.currentIndent;try{this.currentIndent=this.currentIndent+this.indentDelta,e()}finally{this.currentIndent=t}}clone(){const e=Object.assign(new zt,this);return e.buffer=[],e}}class Jt{constructor(e){if(e.some((e=>void 0===e)))throw new Error("aiee");this.items=e}get separator(){return""}get terminator(){return""}writeOn(e){let t=!1;this.items.forEach((r=>{t?e.write(this.separator):t=!0,e.write(r)})),e.write(this.terminator)}}class jt extends Jt{get separator(){return", "}}class Ut extends jt{writeHorizontally(e){e.write(this.open),super.writeOn(e),e.write(this.close)}writeVertically(e){e.write(this.open),this.items.length>0&&(e.withIndent((()=>{this.items.forEach(((t,r)=>{e.newline(),e.write(t);const n=r===this.items.length-1?this.terminator:this.separator;e.write(n.trimRight())}))})),e.newline()),e.write(this.close)}writeOn(e){const t=e.clone();this.writeHorizontally(t);const r=t.toString();r.length<=e.width?e.write(r):this.writeVertically(e)}}class Ht extends Ut{get open(){return"("}get close(){return")"}}class Yt extends Ht{constructor(e,t){super(t),this.operator=e}get separator(){return this.operator}}class Zt extends Ut{get open(){return"["}get close(){return"]"}}class Gt extends Ut{get open(){return"<"}get close(){return">"}}class Qt extends Ut{get open(){return"{"}get close(){return"}"}}class Wt extends Qt{get separator(){return"; "}get terminator(){return";"}}const Xt=(...e)=>new Jt(e),er=(...e)=>new Ht(e),tr=(e,t,...r)=>0===r.length?e:new Yt(t,r),rr=(...e)=>new Zt(e),nr=(...e)=>new Gt(e),ir=(...e)=>new Qt(e),or=(...e)=>1===e.length&&e[0]instanceof Wt?e[0]:new Wt(e),ar=(e,t)=>Xt(JSON.stringify(e),": ",t);var sr;!function(e){e.union=e=>({kind:"union",variants:e}),e.unit=()=>({kind:"unit"}),e.ref=(e,t)=>({kind:"ref",typeName:e,ref:t}),e.array=e=>({kind:"array",type:e}),e.set=e=>({kind:"set",type:e}),e.dictionary=(e,t)=>({kind:"dictionary",key:e,value:t}),e.record=e=>({kind:"record",fields:e})}(sr||(sr={}));const cr=sr.ref("_.Value",null);var dr=Object.freeze({__proto__:null,ANY_TYPE:cr,get Type(){return sr}});function ur(e){return ar("_variant",JSON.stringify(e))}function lr(e,t,r){switch(r.kind){case"unit":return[];case"ref":case"set":case"dictionary":case"array":return[ar("value",fr(e,r))];case"record":return Array.from(r.fields).map((([t,r])=>ar(t,fr(e,r))));default:throw new Error("Unreachable")}}function fr(e,t){switch(t.kind){case"union":return tr("never"," | ",...Array.from(t.variants).flatMap((t=>function(e,t,[r,n]){let i=lr(e,0,n);return ir(ur(r),...i)}(e,0,t))));case"unit":case"record":return ir(...lr(e,0,t));case"ref":return null===t.ref&&"_embedded"===t.typeName?t.typeName:Xt(t.typeName,e.genericArgsFor(t));case"set":return Xt("_.KeyedSet",nr(fr(e,t.type),"_embedded"));case"dictionary":return Xt("_.KeyedDictionary",nr(fr(e,t.key),fr(e,t.value),"_embedded"));case"array":return Xt("Array",nr(fr(e,t.type)));default:throw new Error("Unreachable")}}function br(e,t){switch(t._variant){case"or":return sr.union(new Map([t.pattern0,t.pattern1,...t.patternN].map((t=>[t.variantLabel,vr(e,t.pattern)]))));case"and":return mr(e,[t.pattern0,t.pattern1,...t.patternN]);case"Pattern":return vr(e,t.value)}}function mr(e,t){const r=new Map;return t.forEach((t=>pr(r,e,t))),r.size>0?sr.record(r):sr.unit()}function vr(e,t){return"SimplePattern"===t._variant?_r(e,t.value):mr(e,[ue.anonymous(t)])}function _r(e,t){switch(t._variant){case"any":return cr;case"atom":switch(t.atomKind._variant){case"Boolean":return sr.ref("boolean",null);case"Float":case"Double":case"SignedInteger":return sr.ref("number",null);case"String":return sr.ref("string",null);case"ByteString":return sr.ref("_.Bytes",null);case"Symbol":return sr.ref("symbol",null)}case"embedded":return sr.ref("_embedded",null);case"lit":return sr.unit();case"seqof":return sr.array(_r(e,t.pattern));case"setof":return sr.set(_r(e,t.pattern));case"dictof":return sr.dictionary(_r(e,t.key),_r(e,t.value));case"Ref":return e(t.value);default:throw new Error("Unreachable")}}function pr(e,t,r){if("named"===r._variant){const n=_r(t,r.value.pattern);"unit"!==n.kind&&e.set(r.value.name.description,n)}else"CompoundPattern"===r.value._variant&&function(e,t,r){switch(r._variant){case"rec":pr(e,t,r.label),pr(e,t,r.fields);break;case"tuple":r.patterns.forEach((r=>pr(e,t,r)));break;case"tuplePrefix":r.fixed.forEach((r=>pr(e,t,r))),pr(e,t,ht(r.variable));break;case"dict":r.entries.forEach(((r,n)=>pr(e,t,ht(r))));break;default:throw new Error("Unreachable")}}(e,t,r.value.value)}var hr=Object.freeze({__proto__:null,simpleType:_r,typeFor:vr,typeForDefinition:br,typeForIntersection:mr});class yr{constructor(e,t,r,n){switch(this.literals=new u,this.preamble=[],this.typedefs=[],this.functiondefs=[],this.imports=new p,this.env=e,this.modulePath=t,this.schema=r,this.options=n,r.embeddedType._variant){case"false":this.embeddedType="_.GenericEmbedded";break;case"Ref":{const e=this.resolver()(r.embeddedType.value);this.embeddedType=e.typeName;break}}}literal(e){let t=this.literals.get(e);return void 0===t&&(t=ot("$"+r(e),(()=>"__lit"+this.literals.size)),this.literals.set(e,t)),t}derefPattern(e,t=0){if(t>128)throw new Error("Recursion limit exceeded");return"Pattern"===e._variant&&"SimplePattern"===e.value._variant&&"Ref"===e.value.value._variant?this.lookup(e.value.value.value,((e,r)=>this.derefPattern(e,t+1)),((r,n,i,o,a,s)=>this.derefPattern(null!=a?a:e,t+1))):e}definePreamble(e){this.preamble.push(e)}defineType(e){this.typedefs.push(e)}defineFunctions(e,t){this.functiondefs.push(...t(new Sr(this,e)))}resolver(e){return t=>this.lookup(t,((e,r)=>sr.ref(t.name.description,t)),((e,r,n,i,o,a)=>(this.imports.add([e,r,n,i]),sr.ref(`${r}${i}.${t.name.description}`,t))),e)}lookupType(e,t){const r=this.lookup(e,((e,t)=>t),((e,t,r,n,i,o)=>o),t);return r?r():null}lookup(e,t,r,n){const i=e.module.length?e.module:null!=n?n:this.modulePath,o=bt(this.env,i);if(null!==o){const n=null===o.typescriptModuleExpr?"":"."+o.typescriptModuleExpr;if(null===o.schema)return r(i,mt(o),o.typescriptModulePath,n,null,null);{const a=o.schema.definitions.get(e.name);if(void 0!==a){let s=()=>br(this.resolver(i),a);return e.module.length?r(i,mt(o),o.typescriptModulePath,n,a,s):t(a,s)}}}throw new kt(`Undefined reference: ${_t(e)}`,Rt(e))}genericParameters(){return nr(Xt("_embedded = ",this.embeddedType))}genericParametersFor(e){return this.hasEmbedded(e)?this.genericParameters():""}genericArgs(){return"<_embedded>"}genericArgsFor(e){return this.hasEmbedded(e)?this.genericArgs():""}hasEmbedded(e){const t=this,r=new gr(this.modulePath);return function e(n){switch(n.kind){case"union":for(const t of n.variants.values())if(e(t))return!0;return!1;case"unit":return!1;case"array":return e(n.type);case"set":case"dictionary":return!0;case"ref":if(null!==n.ref)return r.cycleCheck(n.ref,(e=>t.lookupType(e,r.modulePath)),(t=>!!t&&e(t)),(()=>!1));switch(n.typeName){case"_embedded":case"_.Value":return!0;default:return!1}case"record":for(const t of n.fields.values())if(e(t))return!0;return!1}}(e)}withAsPreserveMixinType(e,t){return"unit"===t.kind||"record"===t.kind||"union"===t.kind?tr("any"," & ",Xt(e,this.genericArgsFor(t)),ir(Xt("__as_preserve__",this.hasEmbedded(t)?"":this.genericParameters(),"()",": _.Value",this.genericArgs()))):Xt(e,this.genericArgsFor(t))}}class Sr{constructor(e,t){this.tempCounter=0,this.temps=new Map,this.captures=[],this.variantName=void 0,this.mod=e,this.definitionName=t}gentempname(){return"_tmp"+this.tempCounter++}gentemp(e=cr){const t=fr(this.mod,e),r=function(e,t=qt){const r=new zt;return r.width=t,e.forEach((e=>r.write(e))),r.toString()}([t],1/0),n=this.gentempname();let i=this.temps.get(r);return void 0===i&&(i={type:t,names:[]},this.temps.set(r,i)),i.names.push(n),n}block(e){const t=this.temps;this.temps=new Map;const r=e(),n=this.temps;return this.temps=t,or(...Array.from(n).map((([e,{type:t,names:r}])=>Xt("let ",((...e)=>new jt(e))(...r),": (",t,") | undefined"))),...r)}withCapture(e,t,r){void 0!==e&&this.captures.push({fieldName:e,sourceExpr:t});const n=r(t);return void 0!==e&&this.captures.pop(),n}convertCapture(e,t,r){return this.withCapture(e,t,(e=>Xt(`if (${e} !== void 0) `,this.block((()=>r())))))}buildCapturedCompound(e){const t=[...(r=this.variantName,void 0===r?[]:[ur(r)]),...this.captures.map((({fieldName:e,sourceExpr:t})=>ar(e,t))),Xt("__as_preserve__() ",or(`return from${this.definitionName}(this)`))];var r;return Xt(`${e} = `,ir(...t))}}class gr{constructor(e){this.modulePath=e,this.seen=new h(wr)}cycleCheck(e,t,r,n){const i=ve({module:e.module.length?e.module:this.modulePath,name:e.name});if(this.seen.has(i))return n();{this.seen.add(i);const e=t(i),n=this.modulePath;this.modulePath=i.module;const o=r(e);return this.modulePath=n,o}}}function wr(e){return r([...e.module,e.name])}function $r(e,t,r,n){switch(t._variant){case"or":{const o=[t.pattern0,t.pattern1,...t.patternN];function i(t){return e.variantName=o[t].variantLabel,[...Pr(e,o[t].pattern,r,n),...t<o.length-1?[Xt(`if (${n} === void 0) `,e.block((()=>i(t+1))))]:[]]}return i(0)}case"and":{const a=[t.pattern0,t.pattern1,...t.patternN];function i(t){return t<a.length?kr(e,a[t],r,(()=>i(t+1))):[e.buildCapturedCompound(n)]}return i(0)}case"Pattern":return e.variantName=void 0,Pr(e,t.value,r,n)}}function Pr(e,t,r,n){return kr(e,ue.anonymous(t),r,(r=>void 0===r?[e.buildCapturedCompound(n)]:void 0!==e.variantName?"unit"===vr(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[e.withCapture("value",r,(()=>e.buildCapturedCompound(n)))]:"unit"===vr(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[`${n} = ${r}`]))}function Nr(e,t,r,n,i,o){function a(n){if(n<t.length)return kr(e,t[n],`${r}[${n}]`,(()=>a(n+1)));if(void 0===i)return o();{const n=e.gentemp(sr.array(cr));return[t.length>0?`${n} = ${r}.slice(${t.length})`:`${n} = ${r}`,...kr(e,ht(i),n,o,!0)]}}const s=void 0===i?Xt(` && ${r}.length === ${t.length}`):0===t.length?"":Xt(` && ${r}.length >= ${t.length}`);return n?a(0):[Xt(`if (_.isSequence(${r})`,s,") ",e.block((()=>a(0))))]}function kr(e,t,r,n,i=!1){let o=pt(t),a=yt(t);if("SimplePattern"===o._variant){const t=e.gentemp(_r(e.mod.resolver(),o.value));return[...Ar(e,o.value,r,t,i),e.convertCapture(a,t,(()=>n(t)))]}return function(e,t,r,n,i){switch(t._variant){case"rec":return[Xt(`if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(${r})) `,e.block((()=>kr(e,t.label,`${r}.label`,(()=>kr(e,t.fields,r,i,!0))))))];case"tuple":return Nr(e,t.patterns,r,n,void 0,i);case"tuplePrefix":return Nr(e,t.fixed,r,n,t.variable,i);case"dict":{const a=Array.from(t.entries);function o(t){if(t<a.length){const[n,i]=a[t],s=e.gentemp();return[Xt(`if ((${s} = ${r}.get(${e.mod.literal(n)})) !== void 0) `,e.block((()=>kr(e,ht(i),s,(()=>o(t+1))))))]}return i()}return[Xt(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>o(0))))]}default:throw new Error("Unreachable")}}(e,o.value,r,i,(()=>n(void 0)))}function Ar(e,t,r,n,i){switch(t._variant){case"any":return[`${n} = ${r}`];case"atom":{let e,i=`${r}`;switch(t.atomKind._variant){case"Boolean":e=`typeof ${r} === 'boolean'`;break;case"Float":e=`_.Float.isSingle(${r})`,i=`${r}.value`;break;case"Double":e=`_.Float.isDouble(${r})`,i=`${r}.value`;break;case"SignedInteger":e=`typeof ${r} === 'number'`;break;case"String":e=`typeof ${r} === 'string'`;break;case"ByteString":e=`_.Bytes.isBytes(${r})`;break;case"Symbol":e=`typeof ${r} === 'symbol'`}return[Xt(`${n} = `,e," ? ",i," : void 0")]}case"embedded":return[`${n} = _.isEmbedded<_embedded>(${r}) ? ${r}.embeddedValue : void 0`];case"lit":return[`${n} = _.is(${r}, ${e.mod.literal(t.value)}) ? {} : void 0`];case"seqof":{const o=()=>{const i=e.gentempname();return[Xt(`${n} = []`),Xt(`for (const ${i} of ${r}) `,e.block((()=>[...kr(e,St(t.pattern),i,(e=>[`${n}.push(${e})`,"continue"])),Xt(`${n} = void 0`),Xt("break")])))]};return i?o():[`${n} = void 0`,Xt(`if (_.isSequence(${r})) `,e.block(o))]}case"setof":return[`${n} = void 0`,Xt(`if (_.Set.isSet<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname();return[Xt(`${n} = new _.KeyedSet()`),Xt(`for (const ${i} of ${r}) `,e.block((()=>[...kr(e,St(t.pattern),i,(e=>[`${n}.add(${e})`,"continue"])),Xt(`${n} = void 0`),Xt("break")])))]})))];case"dictof":return[`${n} = void 0`,Xt(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname(),o=e.gentempname();return[Xt(`${n} = new _.KeyedDictionary()`),Xt(`for (const [${o}, ${i}] of ${r}) `,e.block((()=>[...kr(e,St(t.key),o,(r=>kr(e,St(t.value),i,(e=>[`${n}.set(${r}, ${e})`,"continue"])))),Xt(`${n} = void 0`),Xt("break")])))]})))];case"Ref":return e.mod.lookup(t.value,((e,i)=>[`${n} = to${t.value.name.description}(${r})`]),((i,o,a,s,c,d)=>(e.mod.imports.add([i,o,a,s]),[`${n} = ${o}${s}.to${t.value.name.description}${e.mod.genericArgs()}(${r})`])));default:throw new Error("Unreachable")}}function Er(e,t,r,n,i,o,a){const s=[];let c=!1;function d(e,t){"unit"!==e.kind&&s.push([t,e])}"record"===i.kind?i.fields.forEach(d):(d(i,"value"),c=void 0===n&&"unit"!==i.kind);const u=void 0!==n?[ar("_variant",JSON.stringify(n))]:[];s.forEach((([e,t])=>u.push(Xt(JSON.stringify(e),": ",ot(e))))),u.push(Xt("__as_preserve__() ",or(`return from${ot(t)}(this)`)));const l=s.length>1?[Xt(ir(...s.map((e=>ot(e[0])))),": ",ir(...s.map((t=>Xt(ot(t[0]),": ",fr(e,t[1]))))))]:s.map((t=>Xt(ot(t[0]),": ",fr(e,t[1]))));return[Xt(`export function ${ot(r)}`,e.genericParametersFor(o),er(...l),": ",a," ",or(Xt("return ",c?"value":ir(...u)))),Xt(`${ot(r)}.schema = function () `,or(Xt("return ",ir("schema: _schema()","imports: _imports",`definitionName: _.Symbol.for(${JSON.stringify(t)})`,...void 0===n?[]:[`variant: _.Symbol.for(${JSON.stringify(n)})`]))))]}function Rr(e,t,r){switch(t._variant){case"or":return[Xt(`switch (${r}._variant) `,or(...[t.pattern0,t.pattern1,...t.patternN].map((t=>Xt("case ",JSON.stringify(t.variantLabel),": ",e.block((()=>{const n="SimplePattern"===t.pattern._variant;return[Xt("return ",Dr(e,t.pattern,n?`${r}.value`:r))]})))))))];case"and":return[Xt("return _.merge",er("(a, b) => (a === b) ? a : void 0",...[t.pattern0,t.pattern1,...t.patternN].flatMap((t=>"anonymous"===t._variant&&"SimplePattern"===t.value._variant?[]:[Ir(e,t,r)]))))];case"Pattern":return[Xt("return ",Dr(e,t.value,`${r}`))]}}function Dr(e,t,r){switch(t._variant){case"SimplePattern":return(t=>{switch(t._variant){case"any":return`${r}`;case"atom":switch(t.atomKind._variant){case"Float":return`_.Single(${r})`;case"Double":return`_.Double(${r})`;default:return`${r}`}case"lit":return e.mod.literal(t.value);case"embedded":return`_.embed(${r})`;case"seqof":return Xt(`${r}.map(v => `,Dr(e,oe.SimplePattern(t.pattern),"v"),")");case"setof":return Xt("new _.Set<_embedded>",er(`_.Array.from(${r}.values()).map(v => `,Dr(e,oe.SimplePattern(t.pattern),"v"),")"));case"dictof":return Xt("new _.Dictionary<_embedded>",er(Xt(`_.Array.from(${r}.entries()).map(([k, v]) => `,rr(Dr(e,oe.SimplePattern(t.key),"k"),Dr(e,oe.SimplePattern(t.value),"v")),")")));case"Ref":return e.mod.lookup(t.value,((n,i)=>`from${t.value.name.description}${e.mod.genericArgs()}(${r})`),((n,i,o,a,s,c)=>(e.mod.imports.add([n,i,o,a]),`${i}${a}.from${t.value.name.description}${e.mod.genericArgs()}(${r})`)))}})(t.value);case"CompoundPattern":return(t=>{switch(t._variant){case"rec":return Xt("_.Record",er(Ir(e,t.label,r),Ir(e,t.fields,r)));case"tuple":return rr(...t.patterns.map((t=>Ir(e,t,r))));case"tuplePrefix":{const n=Ir(e,ht(t.variable),r);return 0===t.fixed.length?n:rr(...t.fixed.map((t=>Ir(e,t,r))),Xt("... ",n))}case"dict":return Xt("new _.Dictionary<_embedded>",er(rr(...Array.from(t.entries.entries()).map((([t,n])=>rr(e.mod.literal(t),function(e,t,r){if("named"===t._variant){const n=Tr(r,t.value.name.description);return Dr(e,oe.SimplePattern(t.value.pattern),n)}return Dr(e,oe.SimplePattern(t.value),r)}(e,n,r)))))))}})(t.value)}}function Tr(e,t){return`${e}[${JSON.stringify(t)}]`}function Ir(e,t,r){if("named"===t._variant){const n=Tr(r,t.value.name.description);return Dr(e,oe.SimplePattern(t.value.pattern),n)}return Dr(e,t.value,r)}function xr(e,t,n,i={}){var o;const a=new yr(e,t,n,i);a.definePreamble("let __schema: _.Value | null = null;"),a.definePreamble(Xt("export function _schema() ",or(Xt("if (__schema === null) ",or(`__schema = _.decode<_.GenericEmbedded>(_.Bytes.fromHex("${S(we(n)).toHex()}"))`)),"return __schema")));"false"!==n.embeddedType._variant&&a.defineType(Xt("export type _embedded = ",a.embeddedType,";"));for(const[e,t]of n.definitions){const n=br(a.resolver(),t),i=r(e),o=a.withAsPreserveMixinType(i,n);a.defineType(Xt(`export type ${i}`,a.genericParametersFor(n)," = ",fr(a,n),";")),"union"===n.kind?a.defineFunctions(i,(e=>[Xt(`export namespace ${i} `,or(...Array.from(n.variants).flatMap((([e,t])=>Er(a,i,e,e,t,n,o)))))])):a.defineFunctions(i,(e=>Er(a,i,i,void 0,n,n,o)))}for(const[e,t]of n.definitions){const r=br(a.resolver(),t),n=e,i=e.description,o=a.withAsPreserveMixinType(i,r);a.defineFunctions(i,(e=>[Xt(`export function as${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): ",o," ",e.block((()=>[Xt(`let result = to${n.description}(v)`),Xt("if (result === void 0) ",`throw new TypeError(\`Invalid ${n.description}: \${_.stringify(v)}\`)`),Xt("return result")])))])),a.defineFunctions(i,(e=>[Xt(`export function to${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): undefined | ",o," ",e.block((()=>[Xt("let result: undefined | ",o),...$r(e,t,"v","result"),Xt("return result")]))),"union"===r.kind?Xt(`export namespace ${ot(n.description)} `,or(Xt(`export const __from_preserve__ = to${n.description}`))):Xt(`${ot(n.description)}.__from_preserve__ = to${n.description};`)])),a.defineFunctions(i,(e=>[Xt(`export function from${n.description}`,a.genericParameters(),"(_v: ",n.description,a.genericArgsFor(r),"): _.Value<_embedded> ",e.block((()=>Rr(e,t,"_v"))))]))}a.definePreamble(Xt("export const _imports = ",ir(...Array.from(a.imports.values()).map((([e,t,n,i])=>Xt(r(vt(e)),": ",t,i))))));const s=new zt;s.write(`import * as _ from ${JSON.stringify(null!==(o=i.preservesModule)&&void 0!==o?o:"@preserves/core")};\n`),a.imports.forEach((([e,t,r,n])=>{s.write(`import * as ${t} from ${JSON.stringify(r)};\n`)})),s.newline();const c=Array.from(a.literals);c.sort(((e,t)=>e[1]<t[1]?-1:e[1]===t[1]?0:1));for(const[e,t]of c)s.write(Xt(`export const ${t} = `,y(e,{boolean:e=>e.toString(),single:e=>e.toString(),double:e=>e.toString(),integer:e=>e.toString(),string:e=>JSON.stringify(e),bytes:e=>Xt("Uint8Array.from(",rr(...Array.from(e).map((e=>e.toString()))),")"),symbol:e=>`_.Symbol.for(${JSON.stringify(e.description)})`,record:(e,t)=>Xt("_.Record<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>",er(t(e.label),rr(...e.map(t)))),array:(e,t)=>rr(...e.map(t)),set:(e,t)=>Xt("new _.Set<_.Value<_embedded>>",er(rr(...Array.from(e).map(t)))),dictionary:(e,t)=>Xt("new _.Dictionary<_embedded>",er(rr(...Array.from(e).map((([e,r])=>rr(t(e),t(r))))))),annotated:(e,t)=>Xt("_.annotate<_embedded>",er(t(e.item),...e.annotations.map(t))),embedded(e,t){throw new Error(`Cannot emit source code for construction of embedded ${r(e)}`)}}),";\n"));return s.newline(),a.preamble.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.typedefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.functiondefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.toString()}export{hr as GenType,$t as Meta,kt as SchemaSyntaxError,dr as Type,Pt as checkSchema,xr as compile,Ct as parseSchema,Bt as readSchema,Et as recordPosition,Rt as refPosition};
import*as e from"@preserves/core";import{is as t,stringify as r,KeyedDictionary as n,Reader as i,genericEmbeddedTypeDecode as o,peel as a,position as s,preserves as c,Record as d,Dictionary as u,isCompound as l,isEmbedded as f,strip as b,Annotated as m,annotate as v,annotations as _,KeyedSet as p,FlexSet as h,fold as y,encode as S}from"@preserves/core";const g=1,w=e.Symbol.for("Boolean"),$=e.Symbol.for("ByteString"),P=e.Symbol.for("Double"),N=e.Symbol.for("Float"),k=e.Symbol.for("SignedInteger"),A=e.Symbol.for("String"),E=e.Symbol.for("Symbol"),R=e.Symbol.for("and"),D=e.Symbol.for("any"),T=e.Symbol.for("atom"),I=e.Symbol.for("bundle"),x=e.Symbol.for("definitions"),B=e.Symbol.for("dict"),C=e.Symbol.for("dictof"),M=e.Symbol.for("embedded"),L=e.Symbol.for("embeddedType"),V=e.Symbol.for("lit"),F=e.Symbol.for("named"),O=e.Symbol.for("or"),K=e.Symbol.for("rec"),q=e.Symbol.for("ref"),z=e.Symbol.for("schema"),J=e.Symbol.for("seqof"),j=e.Symbol.for("setof"),U=e.Symbol.for("tuple"),H=e.Symbol.for("tuplePrefix"),Y=e.Symbol.for("version"),Z=!1;let G=null;function Q(){return null===G&&(G=e.decode(e.Bytes.fromHex("b4b306736368656d61b7b30776657273696f6e91b30b646566696e6974696f6e73b7b303526566b4b303726563b4b3036c6974b30372656684b4b3057475706c65b5b4b3056e616d6564b3066d6f64756c65b4b303726566b584b30a4d6f64756c65506174688484b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484848484b30642756e646c65b4b303726563b4b3036c6974b30662756e646c6584b4b3057475706c65b5b4b3056e616d6564b3076d6f64756c6573b4b303726566b584b3074d6f64756c65738484848484b306536368656d61b4b303726563b4b3036c6974b306736368656d6184b4b3057475706c65b5b4b30464696374b7b30776657273696f6eb4b3056e616d6564b30776657273696f6eb4b303726566b584b30756657273696f6e8484b30b646566696e6974696f6e73b4b3056e616d6564b30b646566696e6974696f6e73b4b303726566b584b30b446566696e6974696f6e738484b30c656d62656464656454797065b4b3056e616d6564b30c656d62656464656454797065b4b303726566b584b310456d626564646564547970654e616d6584848484848484b30742696e64696e67b4b303726563b4b3036c6974b3056e616d656484b4b3057475706c65b5b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e8484848484b3074d6f64756c6573b4b306646963746f66b4b303726566b584b30a4d6f64756c655061746884b4b303726566b584b306536368656d618484b3075061747465726eb4b3026f72b5b5b10d53696d706c655061747465726eb4b303726566b584b30d53696d706c655061747465726e8484b5b10f436f6d706f756e645061747465726eb4b303726566b584b30f436f6d706f756e645061747465726e84848484b30756657273696f6eb4b3036c69749184b30841746f6d4b696e64b4b3026f72b5b5b107426f6f6c65616eb4b3036c6974b307426f6f6c65616e8484b5b105466c6f6174b4b3036c6974b305466c6f61748484b5b106446f75626c65b4b3036c6974b306446f75626c658484b5b10d5369676e6564496e7465676572b4b3036c6974b30d5369676e6564496e74656765728484b5b106537472696e67b4b3036c6974b306537472696e678484b5b10a42797465537472696e67b4b3036c6974b30a42797465537472696e678484b5b10653796d626f6cb4b3036c6974b30653796d626f6c84848484b30a446566696e6974696f6eb4b3026f72b5b5b1026f72b4b303726563b4b3036c6974b3026f7284b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b3104e616d6564416c7465726e61746976658484b4b3056e616d6564b3087061747465726e31b4b303726566b584b3104e616d6564416c7465726e6174697665848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b3104e616d6564416c7465726e61746976658484848484848484b5b103616e64b4b303726563b4b3036c6974b303616e6484b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3087061747465726e31b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e8484848484848484b5b1075061747465726eb4b303726566b584b3075061747465726e84848484b30a4d6f64756c6550617468b4b3057365716f66b4b30461746f6db30653796d626f6c8484b30b446566696e6974696f6e73b4b306646963746f66b4b30461746f6db30653796d626f6c84b4b303726566b584b30a446566696e6974696f6e8484b30c4e616d65645061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b3075061747465726e84848484b30d53696d706c655061747465726eb4b3026f72b5b5b103616e79b4b3036c6974b303616e798484b5b10461746f6db4b303726563b4b3036c6974b30461746f6d84b4b3057475706c65b5b4b3056e616d6564b30861746f6d4b696e64b4b303726566b584b30841746f6d4b696e64848484848484b5b108656d626564646564b4b303726563b4b3036c6974b308656d62656464656484b4b3057475706c65b5b4b3056e616d6564b309696e74657266616365b4b303726566b584b30d53696d706c655061747465726e848484848484b5b1036c6974b4b303726563b4b3036c6974b3036c697484b4b3057475706c65b5b4b3056e616d6564b30576616c7565b303616e798484848484b5b1057365716f66b4b303726563b4b3036c6974b3057365716f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b1057365746f66b4b303726563b4b3036c6974b3057365746f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b106646963746f66b4b303726563b4b3036c6974b306646963746f6684b4b3057475706c65b5b4b3056e616d6564b3036b6579b4b303726566b584b30d53696d706c655061747465726e8484b4b3056e616d6564b30576616c7565b4b303726566b584b30d53696d706c655061747465726e848484848484b5b103526566b4b303726566b584b30352656684848484b30f436f6d706f756e645061747465726eb4b3026f72b5b5b103726563b4b303726563b4b3036c6974b30372656384b4b3057475706c65b5b4b3056e616d6564b3056c6162656cb4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3066669656c6473b4b303726566b584b30c4e616d65645061747465726e848484848484b5b1057475706c65b4b303726563b4b3036c6974b3057475706c6584b4b3057475706c65b5b4b3056e616d6564b3087061747465726e73b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e84848484848484b5b10b7475706c65507265666978b4b303726563b4b3036c6974b30b7475706c6550726566697884b4b3057475706c65b5b4b3056e616d6564b3056669786564b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087661726961626c65b4b303726566b584b3124e616d656453696d706c655061747465726e848484848484b5b10464696374b4b303726563b4b3036c6974b3046469637484b4b3057475706c65b5b4b3056e616d6564b307656e7472696573b4b303726566b584b31144696374696f6e617279456e74726965738484848484848484b310456d626564646564547970654e616d65b4b3026f72b5b5b10566616c7365b4b3036c6974808484b5b103526566b4b303726566b584b30352656684848484b3104e616d6564416c7465726e6174697665b4b3057475706c65b5b4b3056e616d6564b30c76617269616e744c6162656cb4b30461746f6db306537472696e678484b4b3056e616d6564b3077061747465726eb4b303726566b584b3075061747465726e84848484b31144696374696f6e617279456e7472696573b4b306646963746f66b303616e79b4b303726566b584b3124e616d656453696d706c655061747465726e8484b3124e616d656453696d706c655061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b30d53696d706c655061747465726e8484848484b30c656d62656464656454797065808484"))),G}const W={};function X(e){return{modules:e,__as_preserve__(){return he(this)}}}function ee(e){return e}function te({version:e,embeddedType:t,definitions:r}){return{version:e,embeddedType:t,definitions:r,__as_preserve__(){return we(this)}}}function re(){return{__as_preserve__:()=>Ne()}}var ne,ie,oe,ae,se,ce,de,ue;function le(e){return e}function fe(e){return e}function be({variantLabel:e,pattern:t}){return{variantLabel:e,pattern:t,__as_preserve__(){return ze(this)}}}function me({name:e,pattern:t}){return{name:e,pattern:t,__as_preserve__(){return Ze(this)}}}function ve({module:e,name:t}){return{module:e,name:t,__as_preserve__(){return Qe(this)}}}function _e(e){return e}function pe(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,I)?{}:void 0,void 0!==n){let e;e=ye(t[0]),void 0!==e&&(r={modules:e,__as_preserve__(){return he(this)}})}}return r}function he(t){return e.Record(I,[Se(t.modules)])}function ye(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t=We(e),void 0!==t){let e;if(e=ge(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Se(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[Xe(e),we(t)])))}function ge(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,z)?{}:void 0,void 0!==n&&e.Dictionary.isDictionary(t[0])){let e;if(void 0!==(e=t[0].get(Y))){let n;if(n=Pe(e),void 0!==n){let e;if(void 0!==(e=t[0].get(L))){let i;if(i=ke(e),void 0!==i){let e;if(void 0!==(e=t[0].get(x))){let t;t=Ee(e),void 0!==t&&(r={version:n,embeddedType:i,definitions:t,__as_preserve__(){return we(this)}})}}}}}}}return r}function we(t){return e.Record(z,[new e.Dictionary([[Y,Ne(t.version)],[L,Ae(t.embeddedType)],[x,Re(t.definitions)]])])}function $e(t){let r=Pe(t);if(void 0===r)throw new TypeError(`Invalid Version: ${e.stringify(t)}`);return r}function Pe(t){let r,n;return r=e.is(t,g)?{}:void 0,void 0!==r&&(n={__as_preserve__:()=>Ne()}),n}function Ne(e){return g}function ke(t){let r,n;if(r=e.is(t,Z)?{}:void 0,void 0!==r&&(n={_variant:"false",__as_preserve__(){return Ae(this)}}),void 0===n){let e;e=Ge(t),void 0!==e&&(n={_variant:"Ref",value:e,__as_preserve__(){return Ae(this)}})}return n}function Ae(e){switch(e._variant){case"false":return Z;case"Ref":return Qe(e.value)}}function Ee(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t="symbol"==typeof e?e:void 0,void 0!==t){let e;if(e=De(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Re(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[e,Te(t)])))}function De(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,O)?{}:void 0,void 0!==n&&e.isSequence(t[0])&&t[0].length>=2){let e;if(e=qe(t[0][0]),void 0!==e){let n;if(n=qe(t[0][1]),void 0!==n){let i,o;i=t[0].slice(2),o=[];for(const e of i){let t;if(t=qe(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(r={_variant:"or",pattern0:e,pattern1:n,patternN:o,__as_preserve__(){return Te(this)}})}}}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,R)?{}:void 0,void 0!==n&&e.isSequence(t[0])&&t[0].length>=2){let e;if(e=Ue(t[0][0]),void 0!==e){let n;if(n=Ue(t[0][1]),void 0!==n){let i,o;i=t[0].slice(2),o=[];for(const e of i){let t;if(t=Ue(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(r={_variant:"and",pattern0:e,pattern1:n,patternN:o,__as_preserve__(){return Te(this)}})}}}}if(void 0===r){let e;e=Ie(t),void 0!==e&&(r={_variant:"Pattern",value:e,__as_preserve__(){return Te(this)}})}}return r}function Te(t){switch(t._variant){case"or":return e.Record(O,[[ze(t.pattern0),ze(t.pattern1),...t.patternN.map((e=>ze(e)))]]);case"and":return e.Record(R,[[He(t.pattern0),He(t.pattern1),...t.patternN.map((e=>He(e)))]]);case"Pattern":return xe(t.value)}}function Ie(e){let t,r;if(t=Be(e),void 0!==t&&(r={_variant:"SimplePattern",value:t,__as_preserve__(){return xe(this)}}),void 0===r){let t;t=Me(e),void 0!==t&&(r={_variant:"CompoundPattern",value:t,__as_preserve__(){return xe(this)}})}return r}function xe(e){switch(e._variant){case"SimplePattern":return Ce(e.value);case"CompoundPattern":return Le(e.value)}}function Be(t){let r,n;if(r=e.is(t,D)?{}:void 0,void 0!==r&&(n={_variant:"any",__as_preserve__(){return Ce(this)}}),void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,T)?{}:void 0,void 0!==r){let e;e=Oe(t[0]),void 0!==e&&(n={_variant:"atom",atomKind:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,M)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"embedded",interface:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,V)?{}:void 0,void 0!==r){let e;e=t[0],void 0!==e&&(n={_variant:"lit",value:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,J)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"seqof",pattern:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,j)?{}:void 0,void 0!==r){let e;e=Be(t[0]),void 0!==e&&(n={_variant:"setof",pattern:e,__as_preserve__(){return Ce(this)}})}}if(void 0===n){if(e.Record.isRecord(t)){let r;if(r=e.is(t.label,C)?{}:void 0,void 0!==r){let e;if(e=Be(t[0]),void 0!==e){let r;r=Be(t[1]),void 0!==r&&(n={_variant:"dictof",key:e,value:r,__as_preserve__(){return Ce(this)}})}}}if(void 0===n){let e;e=Ge(t),void 0!==e&&(n={_variant:"Ref",value:e,__as_preserve__(){return Ce(this)}})}}}}}}}return n}function Ce(t){switch(t._variant){case"any":return D;case"atom":return e.Record(T,[Ke(t.atomKind)]);case"embedded":return e.Record(M,[Ce(t.interface)]);case"lit":return e.Record(V,[t.value]);case"seqof":return e.Record(J,[Ce(t.pattern)]);case"setof":return e.Record(j,[Ce(t.pattern)]);case"dictof":return e.Record(C,[Ce(t.key),Ce(t.value)]);case"Ref":return Qe(t.value)}}function Me(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,K)?{}:void 0,void 0!==n){let e;if(e=Ue(t[0]),void 0!==e){let n;n=Ue(t[1]),void 0!==n&&(r={_variant:"rec",label:e,fields:n,__as_preserve__(){return Le(this)}})}}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,U)?{}:void 0,void 0!==n){let n;if(n=void 0,e.isSequence(t[0])){n=[];for(const e of t[0]){let t;if(t=Ue(e),void 0===t){n=void 0;break}n.push(t)}}void 0!==n&&(r={_variant:"tuple",patterns:n,__as_preserve__(){return Le(this)}})}}if(void 0===r){if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,H)?{}:void 0,void 0!==n){let n;if(n=void 0,e.isSequence(t[0])){n=[];for(const e of t[0]){let t;if(t=Ue(e),void 0===t){n=void 0;break}n.push(t)}}if(void 0!==n){let e;e=Je(t[1]),void 0!==e&&(r={_variant:"tuplePrefix",fixed:n,variable:e,__as_preserve__(){return Le(this)}})}}}if(void 0===r&&e.Record.isRecord(t)){let n;if(n=e.is(t.label,B)?{}:void 0,void 0!==n){let e;e=Ve(t[0]),void 0!==e&&(r={_variant:"dict",entries:e,__as_preserve__(){return Le(this)}})}}}}return r}function Le(t){switch(t._variant){case"rec":return e.Record(K,[He(t.label),He(t.fields)]);case"tuple":return e.Record(U,[t.patterns.map((e=>He(e)))]);case"tuplePrefix":return e.Record(H,[t.fixed.map((e=>He(e))),je(t.variable)]);case"dict":return e.Record(B,[Fe(t.entries)])}}function Ve(t){let r,n;if(r=void 0,e.Dictionary.isDictionary(t)){r=new e.KeyedDictionary;for(const[e,n]of t){let t;if(t=e,void 0!==t){let e;if(e=Je(n),void 0!==e){r.set(t,e);continue}}r=void 0;break}}return void 0!==r&&(n=r),n}function Fe(t){return new e.Dictionary(e.Array.from(t.entries()).map((([e,t])=>[e,je(t)])))}function Oe(t){let r,n;if(r=e.is(t,w)?{}:void 0,void 0!==r&&(n={_variant:"Boolean",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,N)?{}:void 0,void 0!==r&&(n={_variant:"Float",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,P)?{}:void 0,void 0!==r&&(n={_variant:"Double",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,k)?{}:void 0,void 0!==r&&(n={_variant:"SignedInteger",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,A)?{}:void 0,void 0!==r&&(n={_variant:"String",__as_preserve__(){return Ke(this)}}),void 0===n){let r;if(r=e.is(t,$)?{}:void 0,void 0!==r&&(n={_variant:"ByteString",__as_preserve__(){return Ke(this)}}),void 0===n){let r;r=e.is(t,E)?{}:void 0,void 0!==r&&(n={_variant:"Symbol",__as_preserve__(){return Ke(this)}})}}}}}}return n}function Ke(e){switch(e._variant){case"Boolean":return w;case"Float":return N;case"Double":return P;case"SignedInteger":return k;case"String":return A;case"ByteString":return $;case"Symbol":return E}}function qe(t){let r;if(e.isSequence(t)&&2===t.length){let e;if(e="string"==typeof t[0]?t[0]:void 0,void 0!==e){let n;n=Ie(t[1]),void 0!==n&&(r={variantLabel:e,pattern:n,__as_preserve__(){return ze(this)}})}}return r}function ze(e){return[e.variantLabel,xe(e.pattern)]}function Je(e){let t,r;if(t=Ye(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return je(this)}}),void 0===r){let t;t=Be(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return je(this)}})}return r}function je(e){switch(e._variant){case"named":return Ze(e.value);case"anonymous":return Ce(e.value)}}function Ue(e){let t,r;if(t=Ye(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return He(this)}}),void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return He(this)}})}return r}function He(e){switch(e._variant){case"named":return Ze(e.value);case"anonymous":return xe(e.value)}}function Ye(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,F)?{}:void 0,void 0!==n){let e;if(e="symbol"==typeof t[0]?t[0]:void 0,void 0!==e){let n;n=Be(t[1]),void 0!==n&&(r={name:e,pattern:n,__as_preserve__(){return Ze(this)}})}}}return r}function Ze(t){return e.Record(F,[t.name,Ce(t.pattern)])}function Ge(t){let r;if(e.Record.isRecord(t)){let n;if(n=e.is(t.label,q)?{}:void 0,void 0!==n){let e;if(e=We(t[0]),void 0!==e){let n;n="symbol"==typeof t[1]?t[1]:void 0,void 0!==n&&(r={module:e,name:n,__as_preserve__(){return Qe(this)}})}}}return r}function Qe(t){return e.Record(q,[Xe(t.module),t.name])}function We(t){let r,n;if(r=void 0,e.isSequence(t)){r=[];for(const e of t){let t;if(t="symbol"==typeof e?e:void 0,void 0===t){r=void 0;break}r.push(t)}}return void 0!==r&&(n=r),n}function Xe(e){return e.map((e=>e))}X.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Bundle")}},ee.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Modules")}},te.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Schema")}},re.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Version")}},function(t){function r(){return{_variant:"false",__as_preserve__(){return Ae(this)}}}function n(e){return{_variant:"Ref",value:e,__as_preserve__(){return Ae(this)}}}t.$false=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("EmbeddedTypeName"),variant:e.Symbol.for("false")}},t.Ref=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("EmbeddedTypeName"),variant:e.Symbol.for("Ref")}}}(ne||(ne={})),le.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definitions")}},function(t){function r({pattern0:e,pattern1:t,patternN:r}){return{_variant:"or",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return Te(this)}}}function n({pattern0:e,pattern1:t,patternN:r}){return{_variant:"and",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return Te(this)}}}function i(e){return{_variant:"Pattern",value:e,__as_preserve__(){return Te(this)}}}t.or=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("or")}},t.and=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("and")}},t.Pattern=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Definition"),variant:e.Symbol.for("Pattern")}}}(ie||(ie={})),function(t){function r(e){return{_variant:"SimplePattern",value:e,__as_preserve__(){return xe(this)}}}function n(e){return{_variant:"CompoundPattern",value:e,__as_preserve__(){return xe(this)}}}t.SimplePattern=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Pattern"),variant:e.Symbol.for("SimplePattern")}},t.CompoundPattern=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Pattern"),variant:e.Symbol.for("CompoundPattern")}}}(oe||(oe={})),function(t){function r(){return{_variant:"any",__as_preserve__(){return Ce(this)}}}function n(e){return{_variant:"atom",atomKind:e,__as_preserve__(){return Ce(this)}}}function i(e){return{_variant:"embedded",interface:e,__as_preserve__(){return Ce(this)}}}function o(e){return{_variant:"lit",value:e,__as_preserve__(){return Ce(this)}}}function a(e){return{_variant:"seqof",pattern:e,__as_preserve__(){return Ce(this)}}}function s(e){return{_variant:"setof",pattern:e,__as_preserve__(){return Ce(this)}}}function c({key:e,value:t}){return{_variant:"dictof",key:e,value:t,__as_preserve__(){return Ce(this)}}}function d(e){return{_variant:"Ref",value:e,__as_preserve__(){return Ce(this)}}}t.any=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("any")}},t.atom=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("atom")}},t.embedded=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("embedded")}},t.lit=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("lit")}},t.seqof=a,a.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("seqof")}},t.setof=s,s.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("setof")}},t.dictof=c,c.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("dictof")}},t.Ref=d,d.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("SimplePattern"),variant:e.Symbol.for("Ref")}}}(ae||(ae={})),function(t){function r({label:e,fields:t}){return{_variant:"rec",label:e,fields:t,__as_preserve__(){return Le(this)}}}function n(e){return{_variant:"tuple",patterns:e,__as_preserve__(){return Le(this)}}}function i({fixed:e,variable:t}){return{_variant:"tuplePrefix",fixed:e,variable:t,__as_preserve__(){return Le(this)}}}function o(e){return{_variant:"dict",entries:e,__as_preserve__(){return Le(this)}}}t.rec=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("rec")}},t.tuple=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("tuple")}},t.tuplePrefix=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("tuplePrefix")}},t.dict=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("CompoundPattern"),variant:e.Symbol.for("dict")}}}(se||(se={})),fe.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("DictionaryEntries")}},function(t){function r(){return{_variant:"Boolean",__as_preserve__(){return Ke(this)}}}function n(){return{_variant:"Float",__as_preserve__(){return Ke(this)}}}function i(){return{_variant:"Double",__as_preserve__(){return Ke(this)}}}function o(){return{_variant:"SignedInteger",__as_preserve__(){return Ke(this)}}}function a(){return{_variant:"String",__as_preserve__(){return Ke(this)}}}function s(){return{_variant:"ByteString",__as_preserve__(){return Ke(this)}}}function c(){return{_variant:"Symbol",__as_preserve__(){return Ke(this)}}}t.Boolean=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Boolean")}},t.Float=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Float")}},t.Double=i,i.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Double")}},t.SignedInteger=o,o.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("SignedInteger")}},t.String=a,a.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("String")}},t.ByteString=s,s.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("ByteString")}},t.Symbol=c,c.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("AtomKind"),variant:e.Symbol.for("Symbol")}}}(ce||(ce={})),be.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedAlternative")}},function(t){function r(e){return{_variant:"named",value:e,__as_preserve__(){return je(this)}}}function n(e){return{_variant:"anonymous",value:e,__as_preserve__(){return je(this)}}}t.named=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedSimplePattern"),variant:e.Symbol.for("named")}},t.anonymous=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedSimplePattern"),variant:e.Symbol.for("anonymous")}}}(de||(de={})),function(t){function r(e){return{_variant:"named",value:e,__as_preserve__(){return He(this)}}}function n(e){return{_variant:"anonymous",value:e,__as_preserve__(){return He(this)}}}t.named=r,r.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedPattern"),variant:e.Symbol.for("named")}},t.anonymous=n,n.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("NamedPattern"),variant:e.Symbol.for("anonymous")}}}(ue||(ue={})),me.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Binding")}},ve.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("Ref")}},_e.schema=function(){return{schema:Q(),imports:W,definitionName:e.Symbol.for("ModulePath")}},X.__from_preserve__=pe,ee.__from_preserve__=ye,te.__from_preserve__=ge,re.__from_preserve__=Pe,function(e){e.__from_preserve__=ke}(ne||(ne={})),le.__from_preserve__=Ee,function(e){e.__from_preserve__=De}(ie||(ie={})),function(e){e.__from_preserve__=Ie}(oe||(oe={})),function(e){e.__from_preserve__=Be}(ae||(ae={})),function(e){e.__from_preserve__=Me}(se||(se={})),fe.__from_preserve__=Ve,function(e){e.__from_preserve__=Oe}(ce||(ce={})),be.__from_preserve__=qe,function(e){e.__from_preserve__=Je}(de||(de={})),function(e){e.__from_preserve__=Ue}(ue||(ue={})),me.__from_preserve__=Ye,ve.__from_preserve__=Ge,_e.__from_preserve__=We;const et=new Set(["abstract","await","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete","do","double","else","enum","export","extends","false","final","finally","float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield"]);function tt(e){return e.match(/^(.*\.)?([^.]+)$/)[2]}function rt(e){return/^[a-zA-Z][a-zA-Z_0-9]*$/.test(e)}function nt(e){return e.split(".").every(rt)}function it(e){return/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(e)&&!function(e){return et.has(e)}(e)}function ot(e,t){return at(e.replace("$","$$"),t)}function at(e,t){if(it(e))return e;if(it("$"+e))return"$"+e;if(void 0!==t)return t();throw new Error(`Internal error: jsToken needs to be completed (${e})`)}const st=Symbol.for("&"),ct=Symbol.for("."),dt=Symbol.for("..."),ut=Symbol.for("="),lt=Symbol.for("include"),ft=Symbol.for("/");function bt(e,r){for(const n of e)if(t(n.schemaModulePath,r))return n;return null}function mt(e){return"_i_"+e.schemaModulePath.map((e=>e.description)).join("$")}function vt(e){return e.map((e=>e.description)).join(".")}function _t(e){return[...e.module,e.name].map((e=>e.description)).join(".")}function pt(e){return"named"===e._variant?oe.SimplePattern(e.value.pattern):e.value}function ht(e){return"named"===e._variant?e:ue.anonymous(oe.SimplePattern(e.value))}function yt(e){return"named"===e._variant?e.value.name.description:void 0}function St(e){return ue.anonymous(oe.SimplePattern(e))}function gt(e){return"string"==typeof e?e:"symbol"==typeof e?e.description:"number"==typeof e||"boolean"==typeof e?""+e:void 0}var wt,$t=Object.freeze({__proto__:null,$1:g,$Boolean:w,$ByteString:$,$Double:P,$Float:N,$SignedInteger:k,$String:A,$Symbol:E,$and:R,$any:D,$atom:T,$bundle:I,$definitions:x,$dict:B,$dictof:C,$embedded:M,$embeddedType:L,$lit:V,$named:F,$or:O,$rec:K,$ref:q,$schema:z,$seqof:J,$setof:j,$tuple:U,$tuplePrefix:H,$version:Y,ANDSYM:st,get AtomKind(){return ce},Binding:me,Bundle:X,get CompoundPattern(){return se},DOT:ct,DOTDOTDOT:dt,get Definition(){return ie},Definitions:le,DictionaryEntries:fe,EQUALS:ut,get EmbeddedTypeName(){return ne},INCLUDE:lt,ModulePath:_e,Modules:ee,NamedAlternative:be,get NamedPattern(){return ue},get NamedSimplePattern(){return de},ORSYM:ft,get Pattern(){return oe},Ref:ve,Schema:te,get SimplePattern(){return ae},Version:re,__lit6:Z,_imports:W,_schema:Q,anonymousSimplePattern:St,asAtomKind:function(t){let r=Oe(t);if(void 0===r)throw new TypeError(`Invalid AtomKind: ${e.stringify(t)}`);return r},asBinding:function(t){let r=Ye(t);if(void 0===r)throw new TypeError(`Invalid Binding: ${e.stringify(t)}`);return r},asBundle:function(t){let r=pe(t);if(void 0===r)throw new TypeError(`Invalid Bundle: ${e.stringify(t)}`);return r},asCompoundPattern:function(t){let r=Me(t);if(void 0===r)throw new TypeError(`Invalid CompoundPattern: ${e.stringify(t)}`);return r},asDefinition:function(t){let r=De(t);if(void 0===r)throw new TypeError(`Invalid Definition: ${e.stringify(t)}`);return r},asDefinitions:function(t){let r=Ee(t);if(void 0===r)throw new TypeError(`Invalid Definitions: ${e.stringify(t)}`);return r},asDictionaryEntries:function(t){let r=Ve(t);if(void 0===r)throw new TypeError(`Invalid DictionaryEntries: ${e.stringify(t)}`);return r},asEmbeddedTypeName:function(t){let r=ke(t);if(void 0===r)throw new TypeError(`Invalid EmbeddedTypeName: ${e.stringify(t)}`);return r},asModulePath:function(t){let r=We(t);if(void 0===r)throw new TypeError(`Invalid ModulePath: ${e.stringify(t)}`);return r},asModules:function(t){let r=ye(t);if(void 0===r)throw new TypeError(`Invalid Modules: ${e.stringify(t)}`);return r},asNamedAlternative:function(t){let r=qe(t);if(void 0===r)throw new TypeError(`Invalid NamedAlternative: ${e.stringify(t)}`);return r},asNamedPattern:function(t){let r=Ue(t);if(void 0===r)throw new TypeError(`Invalid NamedPattern: ${e.stringify(t)}`);return r},asNamedSimplePattern:function(t){let r=Je(t);if(void 0===r)throw new TypeError(`Invalid NamedSimplePattern: ${e.stringify(t)}`);return r},asPattern:function(t){let r=Ie(t);if(void 0===r)throw new TypeError(`Invalid Pattern: ${e.stringify(t)}`);return r},asRef:function(t){let r=Ge(t);if(void 0===r)throw new TypeError(`Invalid Ref: ${e.stringify(t)}`);return r},asSchema:function(t){let r=ge(t);if(void 0===r)throw new TypeError(`Invalid Schema: ${e.stringify(t)}`);return r},asSimplePattern:function(t){let r=Be(t);if(void 0===r)throw new TypeError(`Invalid SimplePattern: ${e.stringify(t)}`);return r},asVersion:$e,envLookup:bt,formatModulePath:vt,formatRef:_t,fromAtomKind:Ke,fromBinding:Ze,fromBundle:he,fromCompoundPattern:Le,fromDefinition:Te,fromDefinitions:Re,fromDictionaryEntries:Fe,fromEmbeddedTypeName:Ae,fromModulePath:Xe,fromModules:Se,fromNamedAlternative:ze,fromNamedPattern:He,fromNamedSimplePattern:je,fromPattern:xe,fromRef:Qe,fromSchema:we,fromSimplePattern:Ce,fromVersion:Ne,isValidJsId:it,isValidQid:nt,isValidToken:rt,jsId:ot,jsToken:at,modsymFor:mt,nameFor:yt,namelike:gt,promoteNamedSimplePattern:ht,qidLast:tt,toAtomKind:Oe,toBinding:Ye,toBundle:pe,toCompoundPattern:Me,toDefinition:De,toDefinitions:Ee,toDictionaryEntries:Ve,toEmbeddedTypeName:ke,toModulePath:We,toModules:ye,toNamedAlternative:qe,toNamedPattern:Ue,toNamedSimplePattern:Je,toPattern:Ie,toRef:Ge,toSchema:ge,toSimplePattern:Be,toVersion:Pe,unnamePattern:pt,unnameSimplePattern:function(e){return"named"===e._variant?e.value.pattern:e.value}});function Pt(e){const t=new Nt;return e.definitions.forEach(t.checkDefinition.bind(t)),t.problems.length>0?{ok:!1,problems:t.problems}:{ok:!0,schema:e}}!function(e){e[e.AVAILABLE=0]="AVAILABLE",e[e.NOT_AVAILABLE=1]="NOT_AVAILABLE"}(wt||(wt={}));class Nt{constructor(){this.problems=[]}recordProblem(e,t){this.problems.push(`${t} in ${e}`)}checkBinding(e,t,r){const n=t.description;e.has(n)&&this.recordProblem(r,`duplicate binding named ${JSON.stringify(n)}`),rt(n)||this.recordProblem(r,`invalid binding name ${JSON.stringify(n)}`),e.add(n)}checkDefinition(e,t){switch(e._variant){case"or":{const r=new Set;[e.pattern0,e.pattern1,...e.patternN].forEach((({variantLabel:e,pattern:n})=>{const i=`variant ${e} of ${t.description}`;r.has(e)&&this.recordProblem(i,"duplicate variant label"),rt(e)||this.recordProblem(i,"invalid variant label"),r.add(e),this.checkPattern(new Set,n,i,wt.AVAILABLE)}));break}case"and":{const r=[e.pattern0,e.pattern1,...e.patternN],n=new Set;r.forEach((e=>this.checkNamedPattern(n,e,t.description)));break}case"Pattern":this.checkPattern(new Set,e.value,t.description,wt.AVAILABLE)}}checkNamedPattern(e,t,r){switch(t._variant){case"named":this.checkBinding(e,t.value.name,r),this.checkPattern(e,oe.SimplePattern(t.value.pattern),`${JSON.stringify(t.value.name.description)} of ${r}`,wt.AVAILABLE);break;case"anonymous":this.checkPattern(e,t.value,r,wt.NOT_AVAILABLE)}}checkPattern(e,t,n,i){switch(t._variant){case"SimplePattern":"lit"!==t.value._variant&&i===wt.NOT_AVAILABLE&&this.recordProblem(n,"cannot recover serialization of non-literal pattern"),"Ref"!==t.value._variant||rt(t.value.value.name.description)&&t.value.value.module.every((e=>rt(e.description)))||this.recordProblem(n,"invalid reference name");break;case"CompoundPattern":(t=>{switch(t._variant){case"rec":this.checkNamedPattern(e,t.label,`label of ${n}`),this.checkNamedPattern(e,t.fields,`fields of ${n}`);break;case"tuple":t.patterns.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`)));break;case"tuplePrefix":t.fixed.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`))),this.checkNamedPattern(e,ht(t.variable),`tail of ${n}`);break;case"dict":t.entries.forEach(((t,i)=>this.checkNamedPattern(e,ht(t),`entry ${r(i)} in dictionary in ${n}`)))}})(t.value)}}}class kt extends Error{constructor(e,t){super(e),this.pos=t}}const At=new WeakMap;function Et(e,t){return null===t&&console.error("Internal error in Schema reader: null source position for",e),null!==t&&At.set(e,t),e}function Rt(e){var t;return null!==(t=At.get(e))&&void 0!==t?t:null}function Dt(e,r){const n=[];let i=[];function o(){i.length>0&&(n.push(i),i=[])}for(const n of e)t(n,r)?o():i.push(n);return o(),n}function Tt(e){var t;throw new kt(c`Invalid Schema clause: ${e}`,s(null!==(t=e[0])&&void 0!==t&&t))}function It(e,t,n){throw new kt(`Invalid pattern in ${e}: ${r(t)}`,n)}function xt(e,t){return new i(e,{name:null==t?void 0:t.name,includeAnnotations:!0,embeddedDecode:o}).readToEnd()}function Bt(e,t){const r=Pt(Ct(xt(e,t),null!=t?t:{}));if(r.ok)return r.schema;throw new Error("Schema is not invertible:\n"+r.problems.map((e=>" - "+e)).join("\n"))}function Ct(e,r){let i,o=ne.$false(),d=new n;if(function e(n){const u=Dt(a(n),ct);for(const n of u)if(n.length>=2&&t(n[1],ut)){const e=s(n[0]),t=a(n[0]);if("symbol"!=typeof t&&Tt(n),!rt(t.description))throw new kt(c`Invalid definition name: ${t}`,e);if(d.has(t))throw new kt(c`Duplicate definition: ${n}`,e);d.set(t,Lt(t,e,n.slice(2)))}else if(2===n.length&&t(n[0],Y))i=$e(a(n[1]));else if(2===n.length&&t(n[0],L)){const e=s(n[1]),t=a(n[1]);!1===t?o=ne.$false():"symbol"==typeof t&&nt(t.description)?o=ne.Ref(Kt(t.description,e)):It("embedded type name specification",t,e)}else if(2===n.length&&t(n[0],lt)){const t=s(n[1]),i=a(n[1]);if("string"!=typeof i)throw new kt(c`Invalid include: ${n}`,t);if(void 0===r.readInclude)throw new kt(c`Cannot include files in schema`,t);e(xt(r.readInclude(i),r))}else Tt(n)}(e),void 0===i)throw new kt("Schema: missing version declaration.",null);return te({version:re(),embeddedType:o,definitions:d})}function Mt(e){throw new kt("Named patterns must be Simple patterns",e)}function Lt(e,t,r){function n(r){const n=Ot(r)||Ot(r[0]),i=Ft(e,r);if(!1!==n)return be({variantLabel:n.description,pattern:i});if("CompoundPattern"===i._variant&&"rec"===i.value._variant&&"anonymous"===i.value.label._variant&&"SimplePattern"===i.value.label.value._variant&&"lit"===i.value.label.value.value._variant&&"symbol"==typeof i.value.label.value.value.value&&rt(i.value.label.value.value.value.description))return be({variantLabel:i.value.label.value.value.value.description,pattern:i});if("SimplePattern"===i._variant&&"Ref"===i.value._variant&&nt(i.value.value.name.description))return be({variantLabel:tt(i.value.value.name.description),pattern:i});if("SimplePattern"===i._variant&&"lit"===i.value._variant){const e=gt(i.value.value);if(void 0!==e)return be({variantLabel:e,pattern:i})}throw new kt(c`Name missing for alternative: ${r}`,t)}function i(t){const r=Ot(t)||Ot(t[0]),n=Ft(e,t);return!1!==r?("SimplePattern"!==n._variant&&Mt(s(t[0])),ue.named(me({name:r,pattern:n.value}))):ue.anonymous(n)}const o=Dt(r,st),a=Dt(r,ft);if(0===o.length||0===a.length)throw new kt(c`Invalid Schema clause: ${r}`,t);if(o.length>1&&a.length>1)throw new kt(c`Mixed "or" and "and" clause: ${r}`,t);return o.length>1?ie.and({pattern0:i(o[0]),pattern1:i(o[1]),patternN:o.slice(2).map(i)}):a.length>1?ie.or({pattern0:n(a[0]),pattern1:n(a[1]),patternN:a.slice(2).map(n)}):ie.Pattern(Ft(e,a[0]))}function Vt(e,t){return v(e,..._(t))}function Ft(e,n){function i(n,i,c){const m=s(n),v=a(n);function _(){It(r(e),v,m)}if("symbol"!=typeof v){if(d.isRecord(v)){const e=v.label;return d.isRecord(e)?(0!==e.length&&_(),e.label===V?(1!==v.length&&_(),i(ae.lit(v[0]))):c()):c()}if(Array.isArray(v)&&2===v.length&&t(v[1],dt))return i(ae.seqof(o(v[0])));if(Set.isSet(v)){1!==v.size&&_();const[e]=v.values();return i(ae.setof(o(e)))}if(u.isDictionary(v)&&2===v.size&&v.has(dt)){const e=v.clone();e.delete(dt);const[[t,r]]=e.entries();return i(ae.dictof({key:o(t),value:o(r)}))}return l(v)?c():f(v)?i(ae.embedded(o(v.embeddedValue.generic))):i(ae.lit(b(v)))}{const e=v.description;switch(e){case"any":return i(ae.any());case"bool":return i(ae.atom(ce.Boolean()));case"float":return i(ae.atom(ce.Float()));case"double":return i(ae.atom(ce.Double()));case"int":return i(ae.atom(ce.SignedInteger()));case"string":return i(ae.atom(ce.String()));case"bytes":return i(ae.atom(ce.ByteString()));case"symbol":return i(ae.atom(ce.Symbol()));default:if("="===e[0])return i(ae.lit(Symbol.for(e.slice(1))));if(nt(e))return i(ae.Ref(Kt(e,m)));_()}}}const o=e=>i(e,(e=>e),(()=>{throw new kt("Compound patterns not accepted here",s(e))}));function c(e,t,r,n){return o=>{let a=Ot(o);return!1===a&&void 0!==n&&"symbol"==typeof n&&rt(n.description)&&(a=n),!1===a?t(r(o)):e(me({name:a,pattern:i(o,(e=>e),(()=>Mt(s(o))))}))}}const m=c(ue.named,ue.anonymous,(t=>Ft(e,[t]))),v=c(de.named,de.anonymous,o),_=a(n);return 1!==_.length&&It(r(e),_,_.length>0?s(_[0]):s(_)),i(_[0],oe.SimplePattern,(()=>oe.CompoundPattern(function(n){const i=s(n),l=a(n);function f(){It(r(e),l,i)}if(d.isRecord(l)){const t=l.label;if(!d.isRecord(t))return se.rec({label:ue.anonymous(oe.SimplePattern(ae.lit(t))),fields:ue.anonymous(Ft(e,[Vt([...l],n)]))});if(0!==t.length&&f(),t.label===K)return 2!==l.length&&f(),se.rec({label:m(l[0]),fields:m(l[1])});f()}else{if(Array.isArray(l)&&l.length>2&&t(l[l.length-1],dt)){const e=l[l.length-2],t=Vt([e,dt],e);return se.tuplePrefix({fixed:l.slice(0,l.length-2).map(m),variable:v(t)})}if(Array.isArray(l))return se.tuple(l.map(m));if(u.isDictionary(l)&&!l.has(dt))return se.dict(l.mapEntries((([e,t])=>[b(e),c(de.named,de.anonymous,o,b(e))(t)])));f()}}(_[0]))))}function Ot(e){if(!m.isAnnotated(e))return!1;for(const t of e.annotations){const e=a(t);if("symbol"==typeof e)return rt(e.description)&&e}return!1}function Kt(e,t){const r=e.split(".");return Et(ve({module:r.slice(0,r.length-1).map(Symbol.for),name:Symbol.for(r[r.length-1])}),t)}const qt=80;class zt{constructor(){this.width=qt,this.indentDelta=" ",this.currentIndent="\n",this.buffer=[]}get indentSize(){return this.indentDelta.length}set indentSize(e){this.indentDelta=new Array(e+1).join(" ")}write(e){"string"==typeof e?this.buffer.push(e):e.writeOn(this)}newline(){this.write(this.currentIndent)}toString(){return this.buffer.join("")}withIndent(e){const t=this.currentIndent;try{this.currentIndent=this.currentIndent+this.indentDelta,e()}finally{this.currentIndent=t}}clone(){const e=Object.assign(new zt,this);return e.buffer=[],e}}class Jt{constructor(e){if(e.some((e=>void 0===e)))throw new Error("aiee");this.items=e}get separator(){return""}get terminator(){return""}writeOn(e){let t=!1;this.items.forEach((r=>{t?e.write(this.separator):t=!0,e.write(r)})),e.write(this.terminator)}}class jt extends Jt{get separator(){return", "}}class Ut extends jt{writeHorizontally(e){e.write(this.open),super.writeOn(e),e.write(this.close)}writeVertically(e){e.write(this.open),this.items.length>0&&(e.withIndent((()=>{this.items.forEach(((t,r)=>{e.newline(),e.write(t);const n=r===this.items.length-1?this.terminator:this.separator;e.write(n.trimRight())}))})),e.newline()),e.write(this.close)}writeOn(e){const t=e.clone();this.writeHorizontally(t);const r=t.toString();r.length<=e.width?e.write(r):this.writeVertically(e)}}class Ht extends Ut{get open(){return"("}get close(){return")"}}class Yt extends Ht{constructor(e,t){super(t),this.operator=e}get separator(){return this.operator}}class Zt extends Ut{get open(){return"["}get close(){return"]"}}class Gt extends Ut{get open(){return"<"}get close(){return">"}}class Qt extends Ut{get open(){return"{"}get close(){return"}"}}class Wt extends Qt{get separator(){return"; "}get terminator(){return";"}}const Xt=(...e)=>new Jt(e),er=(...e)=>new Ht(e),tr=(e,t,...r)=>0===r.length?e:new Yt(t,r),rr=(...e)=>new Zt(e),nr=(...e)=>new Gt(e),ir=(...e)=>new Qt(e),or=(...e)=>1===e.length&&e[0]instanceof Wt?e[0]:new Wt(e),ar=(e,t)=>Xt(JSON.stringify(e),": ",t);var sr;!function(e){e.union=e=>({kind:"union",variants:e}),e.unit=()=>({kind:"unit"}),e.ref=(e,t)=>({kind:"ref",typeName:e,ref:t}),e.array=e=>({kind:"array",type:e}),e.set=e=>({kind:"set",type:e}),e.dictionary=(e,t)=>({kind:"dictionary",key:e,value:t}),e.record=e=>({kind:"record",fields:e})}(sr||(sr={}));const cr=sr.ref("_.Value",null);var dr=Object.freeze({__proto__:null,ANY_TYPE:cr,get Type(){return sr}});function ur(e){return ar("_variant",JSON.stringify(e))}function lr(e,t,r){switch(r.kind){case"unit":return[];case"ref":case"set":case"dictionary":case"array":return[ar("value",fr(e,r))];case"record":return Array.from(r.fields).map((([t,r])=>ar(t,fr(e,r))));default:throw new Error("Unreachable")}}function fr(e,t){switch(t.kind){case"union":return tr("never"," | ",...Array.from(t.variants).flatMap((t=>function(e,t,[r,n]){let i=lr(e,0,n);return ir(ur(r),...i)}(e,0,t))));case"unit":case"record":return ir(...lr(e,0,t));case"ref":return null===t.ref&&"_embedded"===t.typeName?t.typeName:Xt(t.typeName,e.genericArgsFor(t));case"set":return Xt("_.KeyedSet",nr(fr(e,t.type),"_embedded"));case"dictionary":return Xt("_.KeyedDictionary",nr(fr(e,t.key),fr(e,t.value),"_embedded"));case"array":return Xt("Array",nr(fr(e,t.type)));default:throw new Error("Unreachable")}}function br(e,t){switch(t._variant){case"or":return sr.union(new Map([t.pattern0,t.pattern1,...t.patternN].map((t=>[t.variantLabel,vr(e,t.pattern)]))));case"and":return mr(e,[t.pattern0,t.pattern1,...t.patternN]);case"Pattern":return vr(e,t.value)}}function mr(e,t){const r=new Map;return t.forEach((t=>pr(r,e,t))),r.size>0?sr.record(r):sr.unit()}function vr(e,t){return"SimplePattern"===t._variant?_r(e,t.value):mr(e,[ue.anonymous(t)])}function _r(e,t){switch(t._variant){case"any":return cr;case"atom":switch(t.atomKind._variant){case"Boolean":return sr.ref("boolean",null);case"Float":case"Double":case"SignedInteger":return sr.ref("number",null);case"String":return sr.ref("string",null);case"ByteString":return sr.ref("_.Bytes",null);case"Symbol":return sr.ref("symbol",null)}case"embedded":return sr.ref("_embedded",null);case"lit":return sr.unit();case"seqof":return sr.array(_r(e,t.pattern));case"setof":return sr.set(_r(e,t.pattern));case"dictof":return sr.dictionary(_r(e,t.key),_r(e,t.value));case"Ref":return e(t.value);default:throw new Error("Unreachable")}}function pr(e,t,r){if("named"===r._variant){const n=_r(t,r.value.pattern);"unit"!==n.kind&&e.set(r.value.name.description,n)}else"CompoundPattern"===r.value._variant&&function(e,t,r){switch(r._variant){case"rec":pr(e,t,r.label),pr(e,t,r.fields);break;case"tuple":r.patterns.forEach((r=>pr(e,t,r)));break;case"tuplePrefix":r.fixed.forEach((r=>pr(e,t,r))),pr(e,t,ht(r.variable));break;case"dict":r.entries.forEach(((r,n)=>pr(e,t,ht(r))));break;default:throw new Error("Unreachable")}}(e,t,r.value.value)}var hr=Object.freeze({__proto__:null,simpleType:_r,typeFor:vr,typeForDefinition:br,typeForIntersection:mr});class yr{constructor(e,t,r,n){switch(this.literals=new u,this.preamble=[],this.typedefs=[],this.functiondefs=[],this.imports=new p,this.env=e,this.modulePath=t,this.schema=r,this.options=n,r.embeddedType._variant){case"false":this.embeddedType="_.GenericEmbedded";break;case"Ref":{const e=this.resolver()(r.embeddedType.value);this.embeddedType=e.typeName;break}}}literal(e){let t=this.literals.get(e);return void 0===t&&(t=ot("$"+r(e),(()=>"__lit"+this.literals.size)),this.literals.set(e,t)),t}derefPattern(e,t=0){if(t>128)throw new Error("Recursion limit exceeded");return"Pattern"===e._variant&&"SimplePattern"===e.value._variant&&"Ref"===e.value.value._variant?this.lookup(e.value.value.value,((e,r)=>this.derefPattern(e,t+1)),((r,n,i,o,a,s)=>this.derefPattern(null!=a?a:e,t+1))):e}definePreamble(e){this.preamble.push(e)}defineType(e){this.typedefs.push(e)}defineFunctions(e,t){this.functiondefs.push(...t(new Sr(this,e)))}resolver(e){return t=>this.lookup(t,((e,r)=>sr.ref(t.name.description,t)),((e,r,n,i,o,a)=>(this.imports.add([e,r,n,i]),sr.ref(`${r}${i}.${t.name.description}`,t))),e)}lookupType(e,t){const r=this.lookup(e,((e,t)=>t),((e,t,r,n,i,o)=>o),t);return r?r():null}lookup(e,t,r,n){const i=e.module.length?e.module:null!=n?n:this.modulePath,o=bt(this.env,i);if(null!==o){const n=null===o.typescriptModuleExpr?"":"."+o.typescriptModuleExpr;if(null===o.schema)return r(i,mt(o),o.typescriptModulePath,n,null,null);{const a=o.schema.definitions.get(e.name);if(void 0!==a){let s=()=>br(this.resolver(i),a);return e.module.length?r(i,mt(o),o.typescriptModulePath,n,a,s):t(a,s)}}}throw new kt(`Undefined reference: ${_t(e)}`,Rt(e))}genericParameters(){return nr(Xt("_embedded = ",this.embeddedType))}genericParametersFor(e){return this.hasEmbedded(e)?this.genericParameters():""}genericArgs(){return"<_embedded>"}genericArgsFor(e){return this.hasEmbedded(e)?this.genericArgs():""}hasEmbedded(e){const t=this,r=new gr(this.modulePath);return function e(n){switch(n.kind){case"union":for(const t of n.variants.values())if(e(t))return!0;return!1;case"unit":return!1;case"array":return e(n.type);case"set":case"dictionary":return!0;case"ref":if(null!==n.ref)return r.cycleCheck(n.ref,(e=>t.lookupType(e,r.modulePath)),(t=>!!t&&e(t)),(()=>!1));switch(n.typeName){case"_embedded":case"_.Value":return!0;default:return!1}case"record":for(const t of n.fields.values())if(e(t))return!0;return!1}}(e)}withAsPreserveMixinType(e,t){return"unit"===t.kind||"record"===t.kind||"union"===t.kind?tr("any"," & ",Xt(e,this.genericArgsFor(t)),ir(Xt("__as_preserve__",this.hasEmbedded(t)?"":this.genericParameters(),"()",": _.Value",this.genericArgs()))):Xt(e,this.genericArgsFor(t))}}class Sr{constructor(e,t){this.tempCounter=0,this.temps=new Map,this.captures=[],this.variantName=void 0,this.mod=e,this.definitionName=t}gentempname(){return"_tmp"+this.tempCounter++}gentemp(e=cr){const t=fr(this.mod,e),r=function(e,t=qt){const r=new zt;return r.width=t,e.forEach((e=>r.write(e))),r.toString()}([t],1/0),n=this.gentempname();let i=this.temps.get(r);return void 0===i&&(i={type:t,names:[]},this.temps.set(r,i)),i.names.push(n),n}block(e){const t=this.temps;this.temps=new Map;const r=e(),n=this.temps;return this.temps=t,or(...Array.from(n).map((([e,{type:t,names:r}])=>Xt("let ",((...e)=>new jt(e))(...r),": (",t,") | undefined"))),...r)}withCapture(e,t,r){void 0!==e&&this.captures.push({fieldName:e,sourceExpr:t});const n=r(t);return void 0!==e&&this.captures.pop(),n}convertCapture(e,t,r){return this.withCapture(e,t,(e=>Xt(`if (${e} !== void 0) `,this.block((()=>r())))))}buildCapturedCompound(e){const t=[...(r=this.variantName,void 0===r?[]:[ur(r)]),...this.captures.map((({fieldName:e,sourceExpr:t})=>ar(e,t))),Xt("__as_preserve__() ",or(`return from${this.definitionName}(this)`))];var r;return Xt(`${e} = `,ir(...t))}}class gr{constructor(e){this.modulePath=e,this.seen=new h(wr)}cycleCheck(e,t,r,n){const i=ve({module:e.module.length?e.module:this.modulePath,name:e.name});if(this.seen.has(i))return n();{this.seen.add(i);const e=t(i),n=this.modulePath;this.modulePath=i.module;const o=r(e);return this.modulePath=n,o}}}function wr(e){return r([...e.module,e.name])}function $r(e,t,r,n){switch(t._variant){case"or":{const o=[t.pattern0,t.pattern1,...t.patternN];function i(t){return e.variantName=o[t].variantLabel,[...Pr(e,o[t].pattern,r,n),...t<o.length-1?[Xt(`if (${n} === void 0) `,e.block((()=>i(t+1))))]:[]]}return i(0)}case"and":{const a=[t.pattern0,t.pattern1,...t.patternN];function i(t){return t<a.length?kr(e,a[t],r,(()=>i(t+1))):[e.buildCapturedCompound(n)]}return i(0)}case"Pattern":return e.variantName=void 0,Pr(e,t.value,r,n)}}function Pr(e,t,r,n){return kr(e,ue.anonymous(t),r,(r=>void 0===r?[e.buildCapturedCompound(n)]:void 0!==e.variantName?"unit"===vr(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[e.withCapture("value",r,(()=>e.buildCapturedCompound(n)))]:"unit"===vr(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[`${n} = ${r}`]))}function Nr(e,t,r,n,i,o){function a(n){if(n<t.length)return kr(e,t[n],`${r}[${n}]`,(()=>a(n+1)));if(void 0===i)return o();{const n=e.gentemp(sr.array(cr));return[t.length>0?`${n} = ${r}.slice(${t.length})`:`${n} = ${r}`,...kr(e,ht(i),n,o,!0)]}}const s=void 0===i?Xt(` && ${r}.length === ${t.length}`):0===t.length?"":Xt(` && ${r}.length >= ${t.length}`);return n?a(0):[Xt(`if (_.isSequence(${r})`,s,") ",e.block((()=>a(0))))]}function kr(e,t,r,n,i=!1){let o=pt(t),a=yt(t);if("SimplePattern"===o._variant){const t=e.gentemp(_r(e.mod.resolver(),o.value));return[...Ar(e,o.value,r,t,i),e.convertCapture(a,t,(()=>n(t)))]}return function(e,t,r,n,i){switch(t._variant){case"rec":return[Xt(`if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(${r})) `,e.block((()=>kr(e,t.label,`${r}.label`,(()=>kr(e,t.fields,r,i,!0))))))];case"tuple":return Nr(e,t.patterns,r,n,void 0,i);case"tuplePrefix":return Nr(e,t.fixed,r,n,t.variable,i);case"dict":{const a=Array.from(t.entries);function o(t){if(t<a.length){const[n,i]=a[t],s=e.gentemp();return[Xt(`if ((${s} = ${r}.get(${e.mod.literal(n)})) !== void 0) `,e.block((()=>kr(e,ht(i),s,(()=>o(t+1))))))]}return i()}return[Xt(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>o(0))))]}default:throw new Error("Unreachable")}}(e,o.value,r,i,(()=>n(void 0)))}function Ar(e,t,r,n,i){switch(t._variant){case"any":return[`${n} = ${r}`];case"atom":{let e,i=`${r}`;switch(t.atomKind._variant){case"Boolean":e=`typeof ${r} === 'boolean'`;break;case"Float":e=`_.Float.isSingle(${r})`,i=`${r}.value`;break;case"Double":e=`_.Float.isDouble(${r})`,i=`${r}.value`;break;case"SignedInteger":e=`typeof ${r} === 'number'`;break;case"String":e=`typeof ${r} === 'string'`;break;case"ByteString":e=`_.Bytes.isBytes(${r})`;break;case"Symbol":e=`typeof ${r} === 'symbol'`}return[Xt(`${n} = `,e," ? ",i," : void 0")]}case"embedded":return[`${n} = _.isEmbedded<_embedded>(${r}) ? ${r}.embeddedValue : void 0`];case"lit":return[`${n} = _.is(${r}, ${e.mod.literal(t.value)}) ? {} : void 0`];case"seqof":{const o=()=>{const i=e.gentempname();return[Xt(`${n} = []`),Xt(`for (const ${i} of ${r}) `,e.block((()=>[...kr(e,St(t.pattern),i,(e=>[`${n}.push(${e})`,"continue"])),Xt(`${n} = void 0`),Xt("break")])))]};return i?o():[`${n} = void 0`,Xt(`if (_.isSequence(${r})) `,e.block(o))]}case"setof":return[`${n} = void 0`,Xt(`if (_.Set.isSet<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname();return[Xt(`${n} = new _.KeyedSet()`),Xt(`for (const ${i} of ${r}) `,e.block((()=>[...kr(e,St(t.pattern),i,(e=>[`${n}.add(${e})`,"continue"])),Xt(`${n} = void 0`),Xt("break")])))]})))];case"dictof":return[`${n} = void 0`,Xt(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname(),o=e.gentempname();return[Xt(`${n} = new _.KeyedDictionary()`),Xt(`for (const [${o}, ${i}] of ${r}) `,e.block((()=>[...kr(e,St(t.key),o,(r=>kr(e,St(t.value),i,(e=>[`${n}.set(${r}, ${e})`,"continue"])))),Xt(`${n} = void 0`),Xt("break")])))]})))];case"Ref":return e.mod.lookup(t.value,((e,i)=>[`${n} = to${t.value.name.description}(${r})`]),((i,o,a,s,c,d)=>(e.mod.imports.add([i,o,a,s]),[`${n} = ${o}${s}.to${t.value.name.description}${e.mod.genericArgs()}(${r})`])));default:throw new Error("Unreachable")}}function Er(e,t,r,n,i,o,a){const s=[];let c=!1;function d(e,t){"unit"!==e.kind&&s.push([t,e])}"record"===i.kind?i.fields.forEach(d):(d(i,"value"),c=void 0===n&&"unit"!==i.kind);const u=void 0!==n?[ar("_variant",JSON.stringify(n))]:[];s.forEach((([e,t])=>u.push(Xt(JSON.stringify(e),": ",ot(e))))),u.push(Xt("__as_preserve__() ",or(`return from${ot(t)}(this)`)));const l=s.length>1?[Xt(ir(...s.map((e=>ot(e[0])))),": ",ir(...s.map((t=>Xt(ot(t[0]),": ",fr(e,t[1]))))))]:s.map((t=>Xt(ot(t[0]),": ",fr(e,t[1]))));return[Xt(`export function ${ot(r)}`,e.genericParametersFor(o),er(...l),": ",a," ",or(Xt("return ",c?"value":ir(...u)))),Xt(`${ot(r)}.schema = function () `,or(Xt("return ",ir("schema: _schema()","imports: _imports",`definitionName: _.Symbol.for(${JSON.stringify(t)})`,...void 0===n?[]:[`variant: _.Symbol.for(${JSON.stringify(n)})`]))))]}function Rr(e,t,r){switch(t._variant){case"or":return[Xt(`switch (${r}._variant) `,or(...[t.pattern0,t.pattern1,...t.patternN].map((t=>Xt("case ",JSON.stringify(t.variantLabel),": ",e.block((()=>{const n="SimplePattern"===t.pattern._variant;return[Xt("return ",Dr(e,t.pattern,n?`${r}.value`:r))]})))))))];case"and":return[Xt("return _.merge",er("(a, b) => (a === b) ? a : void 0",...[t.pattern0,t.pattern1,...t.patternN].flatMap((t=>"anonymous"===t._variant&&"SimplePattern"===t.value._variant?[]:[Ir(e,t,r)]))))];case"Pattern":return[Xt("return ",Dr(e,t.value,`${r}`))]}}function Dr(e,t,r){switch(t._variant){case"SimplePattern":return(t=>{switch(t._variant){case"any":return`${r}`;case"atom":switch(t.atomKind._variant){case"Float":return`_.Single(${r})`;case"Double":return`_.Double(${r})`;default:return`${r}`}case"lit":return e.mod.literal(t.value);case"embedded":return`_.embed(${r})`;case"seqof":return Xt(`${r}.map(v => `,Dr(e,oe.SimplePattern(t.pattern),"v"),")");case"setof":return Xt("new _.Set<_embedded>",er(Xt(`_.Array.from(${r}.values()).map(v => `,Dr(e,oe.SimplePattern(t.pattern),"v"),")")));case"dictof":return Xt("new _.Dictionary<_embedded>",er(Xt(`_.Array.from(${r}.entries()).map(([k, v]) => `,rr(Dr(e,oe.SimplePattern(t.key),"k"),Dr(e,oe.SimplePattern(t.value),"v")),")")));case"Ref":return e.mod.lookup(t.value,((n,i)=>`from${t.value.name.description}${e.mod.genericArgs()}(${r})`),((n,i,o,a,s,c)=>(e.mod.imports.add([n,i,o,a]),`${i}${a}.from${t.value.name.description}${e.mod.genericArgs()}(${r})`)))}})(t.value);case"CompoundPattern":return(t=>{switch(t._variant){case"rec":return Xt("_.Record",er(Ir(e,t.label,r),Ir(e,t.fields,r)));case"tuple":return rr(...t.patterns.map((t=>Ir(e,t,r))));case"tuplePrefix":{const n=Ir(e,ht(t.variable),r);return 0===t.fixed.length?n:rr(...t.fixed.map((t=>Ir(e,t,r))),Xt("... ",n))}case"dict":return Xt("new _.Dictionary<_embedded>",er(rr(...Array.from(t.entries.entries()).map((([t,n])=>rr(e.mod.literal(t),function(e,t,r){if("named"===t._variant){const n=Tr(r,t.value.name.description);return Dr(e,oe.SimplePattern(t.value.pattern),n)}return Dr(e,oe.SimplePattern(t.value),r)}(e,n,r)))))))}})(t.value)}}function Tr(e,t){return`${e}[${JSON.stringify(t)}]`}function Ir(e,t,r){if("named"===t._variant){const n=Tr(r,t.value.name.description);return Dr(e,oe.SimplePattern(t.value.pattern),n)}return Dr(e,t.value,r)}function xr(e,t,n,i={}){var o;const a=new yr(e,t,n,i);a.definePreamble("let __schema: _.Value | null = null;"),a.definePreamble(Xt("export function _schema() ",or(Xt("if (__schema === null) ",or(`__schema = _.decode<_.GenericEmbedded>(_.Bytes.fromHex("${S(we(n)).toHex()}"))`)),"return __schema")));"false"!==n.embeddedType._variant&&a.defineType(Xt("export type _embedded = ",a.embeddedType,";"));for(const[e,t]of n.definitions){const n=br(a.resolver(),t),i=r(e),o=a.withAsPreserveMixinType(i,n);a.defineType(Xt(`export type ${i}`,a.genericParametersFor(n)," = ",fr(a,n),";")),"union"===n.kind?a.defineFunctions(i,(e=>[Xt(`export namespace ${i} `,or(...Array.from(n.variants).flatMap((([e,t])=>Er(a,i,e,e,t,n,o)))))])):a.defineFunctions(i,(e=>Er(a,i,i,void 0,n,n,o)))}for(const[e,t]of n.definitions){const r=br(a.resolver(),t),n=e,i=e.description,o=a.withAsPreserveMixinType(i,r);a.defineFunctions(i,(e=>[Xt(`export function as${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): ",o," ",e.block((()=>[Xt(`let result = to${n.description}(v)`),Xt("if (result === void 0) ",`throw new TypeError(\`Invalid ${n.description}: \${_.stringify(v)}\`)`),Xt("return result")])))])),a.defineFunctions(i,(e=>[Xt(`export function to${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): undefined | ",o," ",e.block((()=>[Xt("let result: undefined | ",o),...$r(e,t,"v","result"),Xt("return result")]))),"union"===r.kind?Xt(`export namespace ${ot(n.description)} `,or(Xt(`export const __from_preserve__ = to${n.description}`))):Xt(`${ot(n.description)}.__from_preserve__ = to${n.description};`)])),a.defineFunctions(i,(e=>[Xt(`export function from${n.description}`,a.genericParameters(),"(_v: ",n.description,a.genericArgsFor(r),"): _.Value<_embedded> ",e.block((()=>Rr(e,t,"_v"))))]))}a.definePreamble(Xt("export const _imports = ",ir(...Array.from(a.imports.values()).map((([e,t,n,i])=>Xt(r(vt(e)),": ",t,i))))));const s=new zt;s.write(`import * as _ from ${JSON.stringify(null!==(o=i.preservesModule)&&void 0!==o?o:"@preserves/core")};\n`),a.imports.forEach((([e,t,r,n])=>{s.write(`import * as ${t} from ${JSON.stringify(r)};\n`)})),s.newline();const c=Array.from(a.literals);c.sort(((e,t)=>e[1]<t[1]?-1:e[1]===t[1]?0:1));for(const[e,t]of c)s.write(Xt(`export const ${t} = `,y(e,{boolean:e=>e.toString(),single:e=>e.toString(),double:e=>e.toString(),integer:e=>e.toString(),string:e=>JSON.stringify(e),bytes:e=>Xt("Uint8Array.from(",rr(...Array.from(e).map((e=>e.toString()))),")"),symbol:e=>`_.Symbol.for(${JSON.stringify(e.description)})`,record:(e,t)=>Xt("_.Record<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>",er(t(e.label),rr(...e.map(t)))),array:(e,t)=>rr(...e.map(t)),set:(e,t)=>Xt("new _.Set<_.Value<_embedded>>",er(rr(...Array.from(e).map(t)))),dictionary:(e,t)=>Xt("new _.Dictionary<_embedded>",er(rr(...Array.from(e).map((([e,r])=>rr(t(e),t(r))))))),annotated:(e,t)=>Xt("_.annotate<_embedded>",er(t(e.item),...e.annotations.map(t))),embedded(e,t){throw new Error(`Cannot emit source code for construction of embedded ${r(e)}`)}}),";\n"));return s.newline(),a.preamble.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.typedefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.functiondefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.toString()}export{hr as GenType,$t as Meta,kt as SchemaSyntaxError,dr as Type,Pt as checkSchema,xr as compile,Ct as parseSchema,Bt as readSchema,Et as recordPosition,Rt as refPosition};

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@preserves/core")):"function"==typeof define&&define.amd?define(["exports","@preserves/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).PreservesSchema={},e.Preserves)}(this,(function(e,t){"use strict";function r(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=r(t);const i=1,o=n.Symbol.for("Boolean"),a=n.Symbol.for("ByteString"),s=n.Symbol.for("Double"),c=n.Symbol.for("Float"),d=n.Symbol.for("SignedInteger"),u=n.Symbol.for("String"),l=n.Symbol.for("Symbol"),f=n.Symbol.for("and"),b=n.Symbol.for("any"),m=n.Symbol.for("atom"),p=n.Symbol.for("bundle"),v=n.Symbol.for("definitions"),_=n.Symbol.for("dict"),h=n.Symbol.for("dictof"),y=n.Symbol.for("embedded"),S=n.Symbol.for("embeddedType"),g=n.Symbol.for("lit"),w=n.Symbol.for("named"),$=n.Symbol.for("or"),P=n.Symbol.for("rec"),N=n.Symbol.for("ref"),k=n.Symbol.for("schema"),A=n.Symbol.for("seqof"),E=n.Symbol.for("setof"),R=n.Symbol.for("tuple"),D=n.Symbol.for("tuplePrefix"),T=n.Symbol.for("version"),x=!1;let I=null;function B(){return null===I&&(I=n.decode(n.Bytes.fromHex("b4b306736368656d61b7b30776657273696f6e91b30b646566696e6974696f6e73b7b303526566b4b303726563b4b3036c6974b30372656684b4b3057475706c65b5b4b3056e616d6564b3066d6f64756c65b4b303726566b584b30a4d6f64756c65506174688484b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484848484b30642756e646c65b4b303726563b4b3036c6974b30662756e646c6584b4b3057475706c65b5b4b3056e616d6564b3076d6f64756c6573b4b303726566b584b3074d6f64756c65738484848484b306536368656d61b4b303726563b4b3036c6974b306736368656d6184b4b3057475706c65b5b4b30464696374b7b30776657273696f6eb4b3056e616d6564b30776657273696f6eb4b303726566b584b30756657273696f6e8484b30b646566696e6974696f6e73b4b3056e616d6564b30b646566696e6974696f6e73b4b303726566b584b30b446566696e6974696f6e738484b30c656d62656464656454797065b4b3056e616d6564b30c656d62656464656454797065b4b303726566b584b310456d626564646564547970654e616d6584848484848484b30742696e64696e67b4b303726563b4b3036c6974b3056e616d656484b4b3057475706c65b5b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e8484848484b3074d6f64756c6573b4b306646963746f66b4b303726566b584b30a4d6f64756c655061746884b4b303726566b584b306536368656d618484b3075061747465726eb4b3026f72b5b5b10d53696d706c655061747465726eb4b303726566b584b30d53696d706c655061747465726e8484b5b10f436f6d706f756e645061747465726eb4b303726566b584b30f436f6d706f756e645061747465726e84848484b30756657273696f6eb4b3036c69749184b30841746f6d4b696e64b4b3026f72b5b5b107426f6f6c65616eb4b3036c6974b307426f6f6c65616e8484b5b105466c6f6174b4b3036c6974b305466c6f61748484b5b106446f75626c65b4b3036c6974b306446f75626c658484b5b10d5369676e6564496e7465676572b4b3036c6974b30d5369676e6564496e74656765728484b5b106537472696e67b4b3036c6974b306537472696e678484b5b10a42797465537472696e67b4b3036c6974b30a42797465537472696e678484b5b10653796d626f6cb4b3036c6974b30653796d626f6c84848484b30a446566696e6974696f6eb4b3026f72b5b5b1026f72b4b303726563b4b3036c6974b3026f7284b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b3104e616d6564416c7465726e61746976658484b4b3056e616d6564b3087061747465726e31b4b303726566b584b3104e616d6564416c7465726e6174697665848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b3104e616d6564416c7465726e61746976658484848484848484b5b103616e64b4b303726563b4b3036c6974b303616e6484b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3087061747465726e31b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e8484848484848484b5b1075061747465726eb4b303726566b584b3075061747465726e84848484b30a4d6f64756c6550617468b4b3057365716f66b4b30461746f6db30653796d626f6c8484b30b446566696e6974696f6e73b4b306646963746f66b4b30461746f6db30653796d626f6c84b4b303726566b584b30a446566696e6974696f6e8484b30c4e616d65645061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b3075061747465726e84848484b30d53696d706c655061747465726eb4b3026f72b5b5b103616e79b4b3036c6974b303616e798484b5b10461746f6db4b303726563b4b3036c6974b30461746f6d84b4b3057475706c65b5b4b3056e616d6564b30861746f6d4b696e64b4b303726566b584b30841746f6d4b696e64848484848484b5b108656d626564646564b4b303726563b4b3036c6974b308656d62656464656484b4b3057475706c65b5b4b3056e616d6564b309696e74657266616365b4b303726566b584b30d53696d706c655061747465726e848484848484b5b1036c6974b4b303726563b4b3036c6974b3036c697484b4b3057475706c65b5b4b3056e616d6564b30576616c7565b303616e798484848484b5b1057365716f66b4b303726563b4b3036c6974b3057365716f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b1057365746f66b4b303726563b4b3036c6974b3057365746f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b106646963746f66b4b303726563b4b3036c6974b306646963746f6684b4b3057475706c65b5b4b3056e616d6564b3036b6579b4b303726566b584b30d53696d706c655061747465726e8484b4b3056e616d6564b30576616c7565b4b303726566b584b30d53696d706c655061747465726e848484848484b5b103526566b4b303726566b584b30352656684848484b30f436f6d706f756e645061747465726eb4b3026f72b5b5b103726563b4b303726563b4b3036c6974b30372656384b4b3057475706c65b5b4b3056e616d6564b3056c6162656cb4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3066669656c6473b4b303726566b584b30c4e616d65645061747465726e848484848484b5b1057475706c65b4b303726563b4b3036c6974b3057475706c6584b4b3057475706c65b5b4b3056e616d6564b3087061747465726e73b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e84848484848484b5b10b7475706c65507265666978b4b303726563b4b3036c6974b30b7475706c6550726566697884b4b3057475706c65b5b4b3056e616d6564b3056669786564b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087661726961626c65b4b303726566b584b3124e616d656453696d706c655061747465726e848484848484b5b10464696374b4b303726563b4b3036c6974b3046469637484b4b3057475706c65b5b4b3056e616d6564b307656e7472696573b4b303726566b584b31144696374696f6e617279456e74726965738484848484848484b310456d626564646564547970654e616d65b4b3026f72b5b5b10566616c7365b4b3036c6974808484b5b103526566b4b303726566b584b30352656684848484b3104e616d6564416c7465726e6174697665b4b3057475706c65b5b4b3056e616d6564b30c76617269616e744c6162656cb4b30461746f6db306537472696e678484b4b3056e616d6564b3077061747465726eb4b303726566b584b3075061747465726e84848484b31144696374696f6e617279456e7472696573b4b306646963746f66b303616e79b4b303726566b584b3124e616d656453696d706c655061747465726e8484b3124e616d656453696d706c655061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b30d53696d706c655061747465726e8484848484b30c656d62656464656454797065808484"))),I}const C={};function O(e){return{modules:e,__as_preserve__(){return te(this)}}}function M(e){return e}function L({version:e,embeddedType:t,definitions:r}){return{version:e,embeddedType:t,definitions:r,__as_preserve__(){return oe(this)}}}function V(){return{__as_preserve__:()=>ce()}}var F,K,q,j,z,J,U,H;function Y(e){return e}function Z(e){return e}function G({variantLabel:e,pattern:t}){return{variantLabel:e,pattern:t,__as_preserve__(){return ke(this)}}}function Q({name:e,pattern:t}){return{name:e,pattern:t,__as_preserve__(){return xe(this)}}}function W({module:e,name:t}){return{module:e,name:t,__as_preserve__(){return Be(this)}}}function X(e){return e}function ee(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,p)?{}:void 0,void 0!==r){let r;r=re(e[0]),void 0!==r&&(t={modules:r,__as_preserve__(){return te(this)}})}}return t}function te(e){return n.Record(p,[ne(e.modules)])}function re(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e=Ce(r),void 0!==e){let r;if(r=ie(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function ne(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[Oe(e),oe(t)])))}function ie(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,k)?{}:void 0,void 0!==r&&n.Dictionary.isDictionary(e[0])){let r;if(void 0!==(r=e[0].get(T))){let n;if(n=se(r),void 0!==n){let r;if(void 0!==(r=e[0].get(S))){let i;if(i=de(r),void 0!==i){let r;if(void 0!==(r=e[0].get(v))){let e;e=le(r),void 0!==e&&(t={version:n,embeddedType:i,definitions:e,__as_preserve__(){return oe(this)}})}}}}}}}return t}function oe(e){return n.Record(k,[new n.Dictionary([[T,ce(e.version)],[S,ue(e.embeddedType)],[v,fe(e.definitions)]])])}function ae(e){let t=se(e);if(void 0===t)throw new TypeError(`Invalid Version: ${n.stringify(e)}`);return t}function se(e){let t,r;return t=n.is(e,i)?{}:void 0,void 0!==t&&(r={__as_preserve__:()=>ce()}),r}function ce(e){return i}function de(e){let t,r;if(t=n.is(e,x)?{}:void 0,void 0!==t&&(r={_variant:"false",__as_preserve__(){return ue(this)}}),void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"Ref",value:t,__as_preserve__(){return ue(this)}})}return r}function ue(e){switch(e._variant){case"false":return x;case"Ref":return Be(e.value)}}function le(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e="symbol"==typeof r?r:void 0,void 0!==e){let r;if(r=be(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function fe(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[e,me(t)])))}function be(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,$)?{}:void 0,void 0!==r&&n.isSequence(e[0])&&e[0].length>=2){let r;if(r=Ne(e[0][0]),void 0!==r){let n;if(n=Ne(e[0][1]),void 0!==n){let i,o;i=e[0].slice(2),o=[];for(const e of i){let t;if(t=Ne(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(t={_variant:"or",pattern0:r,pattern1:n,patternN:o,__as_preserve__(){return me(this)}})}}}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,f)?{}:void 0,void 0!==r&&n.isSequence(e[0])&&e[0].length>=2){let r;if(r=Re(e[0][0]),void 0!==r){let n;if(n=Re(e[0][1]),void 0!==n){let i,o;i=e[0].slice(2),o=[];for(const e of i){let t;if(t=Re(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(t={_variant:"and",pattern0:r,pattern1:n,patternN:o,__as_preserve__(){return me(this)}})}}}}if(void 0===t){let r;r=pe(e),void 0!==r&&(t={_variant:"Pattern",value:r,__as_preserve__(){return me(this)}})}}return t}function me(e){switch(e._variant){case"or":return n.Record($,[[ke(e.pattern0),ke(e.pattern1),...e.patternN.map((e=>ke(e)))]]);case"and":return n.Record(f,[[De(e.pattern0),De(e.pattern1),...e.patternN.map((e=>De(e)))]]);case"Pattern":return ve(e.value)}}function pe(e){let t,r;if(t=_e(e),void 0!==t&&(r={_variant:"SimplePattern",value:t,__as_preserve__(){return ve(this)}}),void 0===r){let t;t=ye(e),void 0!==t&&(r={_variant:"CompoundPattern",value:t,__as_preserve__(){return ve(this)}})}return r}function ve(e){switch(e._variant){case"SimplePattern":return he(e.value);case"CompoundPattern":return Se(e.value)}}function _e(e){let t,r;if(t=n.is(e,b)?{}:void 0,void 0!==t&&(r={_variant:"any",__as_preserve__(){return he(this)}}),void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,m)?{}:void 0,void 0!==t){let t;t=$e(e[0]),void 0!==t&&(r={_variant:"atom",atomKind:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,y)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"embedded",interface:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,g)?{}:void 0,void 0!==t){let t;t=e[0],void 0!==t&&(r={_variant:"lit",value:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,A)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"seqof",pattern:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,E)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"setof",pattern:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,h)?{}:void 0,void 0!==t){let t;if(t=_e(e[0]),void 0!==t){let n;n=_e(e[1]),void 0!==n&&(r={_variant:"dictof",key:t,value:n,__as_preserve__(){return he(this)}})}}}if(void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"Ref",value:t,__as_preserve__(){return he(this)}})}}}}}}}return r}function he(e){switch(e._variant){case"any":return b;case"atom":return n.Record(m,[Pe(e.atomKind)]);case"embedded":return n.Record(y,[he(e.interface)]);case"lit":return n.Record(g,[e.value]);case"seqof":return n.Record(A,[he(e.pattern)]);case"setof":return n.Record(E,[he(e.pattern)]);case"dictof":return n.Record(h,[he(e.key),he(e.value)]);case"Ref":return Be(e.value)}}function ye(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,P)?{}:void 0,void 0!==r){let r;if(r=Re(e[0]),void 0!==r){let n;n=Re(e[1]),void 0!==n&&(t={_variant:"rec",label:r,fields:n,__as_preserve__(){return Se(this)}})}}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,R)?{}:void 0,void 0!==r){let r;if(r=void 0,n.isSequence(e[0])){r=[];for(const t of e[0]){let e;if(e=Re(t),void 0===e){r=void 0;break}r.push(e)}}void 0!==r&&(t={_variant:"tuple",patterns:r,__as_preserve__(){return Se(this)}})}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,D)?{}:void 0,void 0!==r){let r;if(r=void 0,n.isSequence(e[0])){r=[];for(const t of e[0]){let e;if(e=Re(t),void 0===e){r=void 0;break}r.push(e)}}if(void 0!==r){let n;n=Ae(e[1]),void 0!==n&&(t={_variant:"tuplePrefix",fixed:r,variable:n,__as_preserve__(){return Se(this)}})}}}if(void 0===t&&n.Record.isRecord(e)){let r;if(r=n.is(e.label,_)?{}:void 0,void 0!==r){let r;r=ge(e[0]),void 0!==r&&(t={_variant:"dict",entries:r,__as_preserve__(){return Se(this)}})}}}}return t}function Se(e){switch(e._variant){case"rec":return n.Record(P,[De(e.label),De(e.fields)]);case"tuple":return n.Record(R,[e.patterns.map((e=>De(e)))]);case"tuplePrefix":return n.Record(D,[e.fixed.map((e=>De(e))),Ee(e.variable)]);case"dict":return n.Record(_,[we(e.entries)])}}function ge(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e=r,void 0!==e){let r;if(r=Ae(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function we(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[e,Ee(t)])))}function $e(e){let t,r;if(t=n.is(e,o)?{}:void 0,void 0!==t&&(r={_variant:"Boolean",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,c)?{}:void 0,void 0!==t&&(r={_variant:"Float",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,s)?{}:void 0,void 0!==t&&(r={_variant:"Double",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,d)?{}:void 0,void 0!==t&&(r={_variant:"SignedInteger",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,u)?{}:void 0,void 0!==t&&(r={_variant:"String",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,a)?{}:void 0,void 0!==t&&(r={_variant:"ByteString",__as_preserve__(){return Pe(this)}}),void 0===r){let t;t=n.is(e,l)?{}:void 0,void 0!==t&&(r={_variant:"Symbol",__as_preserve__(){return Pe(this)}})}}}}}}return r}function Pe(e){switch(e._variant){case"Boolean":return o;case"Float":return c;case"Double":return s;case"SignedInteger":return d;case"String":return u;case"ByteString":return a;case"Symbol":return l}}function Ne(e){let t;if(n.isSequence(e)&&2===e.length){let r;if(r="string"==typeof e[0]?e[0]:void 0,void 0!==r){let n;n=pe(e[1]),void 0!==n&&(t={variantLabel:r,pattern:n,__as_preserve__(){return ke(this)}})}}return t}function ke(e){return[e.variantLabel,ve(e.pattern)]}function Ae(e){let t,r;if(t=Te(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return Ee(this)}}),void 0===r){let t;t=_e(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return Ee(this)}})}return r}function Ee(e){switch(e._variant){case"named":return xe(e.value);case"anonymous":return he(e.value)}}function Re(e){let t,r;if(t=Te(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return De(this)}}),void 0===r){let t;t=pe(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return De(this)}})}return r}function De(e){switch(e._variant){case"named":return xe(e.value);case"anonymous":return ve(e.value)}}function Te(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,w)?{}:void 0,void 0!==r){let r;if(r="symbol"==typeof e[0]?e[0]:void 0,void 0!==r){let n;n=_e(e[1]),void 0!==n&&(t={name:r,pattern:n,__as_preserve__(){return xe(this)}})}}}return t}function xe(e){return n.Record(w,[e.name,he(e.pattern)])}function Ie(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,N)?{}:void 0,void 0!==r){let r;if(r=Ce(e[0]),void 0!==r){let n;n="symbol"==typeof e[1]?e[1]:void 0,void 0!==n&&(t={module:r,name:n,__as_preserve__(){return Be(this)}})}}}return t}function Be(e){return n.Record(N,[Oe(e.module),e.name])}function Ce(e){let t,r;if(t=void 0,n.isSequence(e)){t=[];for(const r of e){let e;if(e="symbol"==typeof r?r:void 0,void 0===e){t=void 0;break}t.push(e)}}return void 0!==t&&(r=t),r}function Oe(e){return e.map((e=>e))}O.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Bundle")}},M.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Modules")}},L.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Schema")}},V.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Version")}},function(e){function t(){return{_variant:"false",__as_preserve__(){return ue(this)}}}function r(e){return{_variant:"Ref",value:e,__as_preserve__(){return ue(this)}}}e.$false=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("EmbeddedTypeName"),variant:n.Symbol.for("false")}},e.Ref=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("EmbeddedTypeName"),variant:n.Symbol.for("Ref")}}}(F||(F={})),Y.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definitions")}},function(e){function t({pattern0:e,pattern1:t,patternN:r}){return{_variant:"or",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return me(this)}}}function r({pattern0:e,pattern1:t,patternN:r}){return{_variant:"and",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return me(this)}}}function i(e){return{_variant:"Pattern",value:e,__as_preserve__(){return me(this)}}}e.or=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("or")}},e.and=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("and")}},e.Pattern=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("Pattern")}}}(K||(K={})),function(e){function t(e){return{_variant:"SimplePattern",value:e,__as_preserve__(){return ve(this)}}}function r(e){return{_variant:"CompoundPattern",value:e,__as_preserve__(){return ve(this)}}}e.SimplePattern=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Pattern"),variant:n.Symbol.for("SimplePattern")}},e.CompoundPattern=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Pattern"),variant:n.Symbol.for("CompoundPattern")}}}(q||(q={})),function(e){function t(){return{_variant:"any",__as_preserve__(){return he(this)}}}function r(e){return{_variant:"atom",atomKind:e,__as_preserve__(){return he(this)}}}function i(e){return{_variant:"embedded",interface:e,__as_preserve__(){return he(this)}}}function o(e){return{_variant:"lit",value:e,__as_preserve__(){return he(this)}}}function a(e){return{_variant:"seqof",pattern:e,__as_preserve__(){return he(this)}}}function s(e){return{_variant:"setof",pattern:e,__as_preserve__(){return he(this)}}}function c({key:e,value:t}){return{_variant:"dictof",key:e,value:t,__as_preserve__(){return he(this)}}}function d(e){return{_variant:"Ref",value:e,__as_preserve__(){return he(this)}}}e.any=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("any")}},e.atom=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("atom")}},e.embedded=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("embedded")}},e.lit=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("lit")}},e.seqof=a,a.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("seqof")}},e.setof=s,s.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("setof")}},e.dictof=c,c.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("dictof")}},e.Ref=d,d.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("Ref")}}}(j||(j={})),function(e){function t({label:e,fields:t}){return{_variant:"rec",label:e,fields:t,__as_preserve__(){return Se(this)}}}function r(e){return{_variant:"tuple",patterns:e,__as_preserve__(){return Se(this)}}}function i({fixed:e,variable:t}){return{_variant:"tuplePrefix",fixed:e,variable:t,__as_preserve__(){return Se(this)}}}function o(e){return{_variant:"dict",entries:e,__as_preserve__(){return Se(this)}}}e.rec=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("rec")}},e.tuple=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("tuple")}},e.tuplePrefix=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("tuplePrefix")}},e.dict=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("dict")}}}(z||(z={})),Z.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("DictionaryEntries")}},function(e){function t(){return{_variant:"Boolean",__as_preserve__(){return Pe(this)}}}function r(){return{_variant:"Float",__as_preserve__(){return Pe(this)}}}function i(){return{_variant:"Double",__as_preserve__(){return Pe(this)}}}function o(){return{_variant:"SignedInteger",__as_preserve__(){return Pe(this)}}}function a(){return{_variant:"String",__as_preserve__(){return Pe(this)}}}function s(){return{_variant:"ByteString",__as_preserve__(){return Pe(this)}}}function c(){return{_variant:"Symbol",__as_preserve__(){return Pe(this)}}}e.Boolean=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Boolean")}},e.Float=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Float")}},e.Double=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Double")}},e.SignedInteger=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("SignedInteger")}},e.String=a,a.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("String")}},e.ByteString=s,s.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("ByteString")}},e.Symbol=c,c.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Symbol")}}}(J||(J={})),G.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedAlternative")}},function(e){function t(e){return{_variant:"named",value:e,__as_preserve__(){return Ee(this)}}}function r(e){return{_variant:"anonymous",value:e,__as_preserve__(){return Ee(this)}}}e.named=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedSimplePattern"),variant:n.Symbol.for("named")}},e.anonymous=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedSimplePattern"),variant:n.Symbol.for("anonymous")}}}(U||(U={})),function(e){function t(e){return{_variant:"named",value:e,__as_preserve__(){return De(this)}}}function r(e){return{_variant:"anonymous",value:e,__as_preserve__(){return De(this)}}}e.named=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedPattern"),variant:n.Symbol.for("named")}},e.anonymous=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedPattern"),variant:n.Symbol.for("anonymous")}}}(H||(H={})),Q.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Binding")}},W.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Ref")}},X.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("ModulePath")}},O.__from_preserve__=ee,M.__from_preserve__=re,L.__from_preserve__=ie,V.__from_preserve__=se,function(e){e.__from_preserve__=de}(F||(F={})),Y.__from_preserve__=le,function(e){e.__from_preserve__=be}(K||(K={})),function(e){e.__from_preserve__=pe}(q||(q={})),function(e){e.__from_preserve__=_e}(j||(j={})),function(e){e.__from_preserve__=ye}(z||(z={})),Z.__from_preserve__=ge,function(e){e.__from_preserve__=$e}(J||(J={})),G.__from_preserve__=Ne,function(e){e.__from_preserve__=Ae}(U||(U={})),function(e){e.__from_preserve__=Re}(H||(H={})),Q.__from_preserve__=Te,W.__from_preserve__=Ie,X.__from_preserve__=Ce;const Me=new Set(["abstract","await","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete","do","double","else","enum","export","extends","false","final","finally","float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield"]);function Le(e){return e.match(/^(.*\.)?([^.]+)$/)[2]}function Ve(e){return/^[a-zA-Z][a-zA-Z_0-9]*$/.test(e)}function Fe(e){return e.split(".").every(Ve)}function Ke(e){return/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(e)&&!function(e){return Me.has(e)}(e)}function qe(e,t){return je(e.replace("$","$$"),t)}function je(e,t){if(Ke(e))return e;if(Ke("$"+e))return"$"+e;if(void 0!==t)return t();throw new Error(`Internal error: jsToken needs to be completed (${e})`)}const ze=Symbol.for("&"),Je=Symbol.for("."),Ue=Symbol.for("..."),He=Symbol.for("="),Ye=Symbol.for("include"),Ze=Symbol.for("/");function Ge(e,r){for(const n of e)if(t.is(n.schemaModulePath,r))return n;return null}function Qe(e){return"_i_"+e.schemaModulePath.map((e=>e.description)).join("$")}function We(e){return e.map((e=>e.description)).join(".")}function Xe(e){return[...e.module,e.name].map((e=>e.description)).join(".")}function et(e){return"named"===e._variant?q.SimplePattern(e.value.pattern):e.value}function tt(e){return"named"===e._variant?e:H.anonymous(q.SimplePattern(e.value))}function rt(e){return"named"===e._variant?e.value.name.description:void 0}function nt(e){return H.anonymous(q.SimplePattern(e))}function it(e){return"string"==typeof e?e:"symbol"==typeof e?e.description:"number"==typeof e||"boolean"==typeof e?""+e:void 0}var ot,at=Object.freeze({__proto__:null,$1:i,$Boolean:o,$ByteString:a,$Double:s,$Float:c,$SignedInteger:d,$String:u,$Symbol:l,$and:f,$any:b,$atom:m,$bundle:p,$definitions:v,$dict:_,$dictof:h,$embedded:y,$embeddedType:S,$lit:g,$named:w,$or:$,$rec:P,$ref:N,$schema:k,$seqof:A,$setof:E,$tuple:R,$tuplePrefix:D,$version:T,ANDSYM:ze,get AtomKind(){return J},Binding:Q,Bundle:O,get CompoundPattern(){return z},DOT:Je,DOTDOTDOT:Ue,get Definition(){return K},Definitions:Y,DictionaryEntries:Z,EQUALS:He,get EmbeddedTypeName(){return F},INCLUDE:Ye,ModulePath:X,Modules:M,NamedAlternative:G,get NamedPattern(){return H},get NamedSimplePattern(){return U},ORSYM:Ze,get Pattern(){return q},Ref:W,Schema:L,get SimplePattern(){return j},Version:V,__lit6:x,_imports:C,_schema:B,anonymousSimplePattern:nt,asAtomKind:function(e){let t=$e(e);if(void 0===t)throw new TypeError(`Invalid AtomKind: ${n.stringify(e)}`);return t},asBinding:function(e){let t=Te(e);if(void 0===t)throw new TypeError(`Invalid Binding: ${n.stringify(e)}`);return t},asBundle:function(e){let t=ee(e);if(void 0===t)throw new TypeError(`Invalid Bundle: ${n.stringify(e)}`);return t},asCompoundPattern:function(e){let t=ye(e);if(void 0===t)throw new TypeError(`Invalid CompoundPattern: ${n.stringify(e)}`);return t},asDefinition:function(e){let t=be(e);if(void 0===t)throw new TypeError(`Invalid Definition: ${n.stringify(e)}`);return t},asDefinitions:function(e){let t=le(e);if(void 0===t)throw new TypeError(`Invalid Definitions: ${n.stringify(e)}`);return t},asDictionaryEntries:function(e){let t=ge(e);if(void 0===t)throw new TypeError(`Invalid DictionaryEntries: ${n.stringify(e)}`);return t},asEmbeddedTypeName:function(e){let t=de(e);if(void 0===t)throw new TypeError(`Invalid EmbeddedTypeName: ${n.stringify(e)}`);return t},asModulePath:function(e){let t=Ce(e);if(void 0===t)throw new TypeError(`Invalid ModulePath: ${n.stringify(e)}`);return t},asModules:function(e){let t=re(e);if(void 0===t)throw new TypeError(`Invalid Modules: ${n.stringify(e)}`);return t},asNamedAlternative:function(e){let t=Ne(e);if(void 0===t)throw new TypeError(`Invalid NamedAlternative: ${n.stringify(e)}`);return t},asNamedPattern:function(e){let t=Re(e);if(void 0===t)throw new TypeError(`Invalid NamedPattern: ${n.stringify(e)}`);return t},asNamedSimplePattern:function(e){let t=Ae(e);if(void 0===t)throw new TypeError(`Invalid NamedSimplePattern: ${n.stringify(e)}`);return t},asPattern:function(e){let t=pe(e);if(void 0===t)throw new TypeError(`Invalid Pattern: ${n.stringify(e)}`);return t},asRef:function(e){let t=Ie(e);if(void 0===t)throw new TypeError(`Invalid Ref: ${n.stringify(e)}`);return t},asSchema:function(e){let t=ie(e);if(void 0===t)throw new TypeError(`Invalid Schema: ${n.stringify(e)}`);return t},asSimplePattern:function(e){let t=_e(e);if(void 0===t)throw new TypeError(`Invalid SimplePattern: ${n.stringify(e)}`);return t},asVersion:ae,envLookup:Ge,formatModulePath:We,formatRef:Xe,fromAtomKind:Pe,fromBinding:xe,fromBundle:te,fromCompoundPattern:Se,fromDefinition:me,fromDefinitions:fe,fromDictionaryEntries:we,fromEmbeddedTypeName:ue,fromModulePath:Oe,fromModules:ne,fromNamedAlternative:ke,fromNamedPattern:De,fromNamedSimplePattern:Ee,fromPattern:ve,fromRef:Be,fromSchema:oe,fromSimplePattern:he,fromVersion:ce,isValidJsId:Ke,isValidQid:Fe,isValidToken:Ve,jsId:qe,jsToken:je,modsymFor:Qe,nameFor:rt,namelike:it,promoteNamedSimplePattern:tt,qidLast:Le,toAtomKind:$e,toBinding:Te,toBundle:ee,toCompoundPattern:ye,toDefinition:be,toDefinitions:le,toDictionaryEntries:ge,toEmbeddedTypeName:de,toModulePath:Ce,toModules:re,toNamedAlternative:Ne,toNamedPattern:Re,toNamedSimplePattern:Ae,toPattern:pe,toRef:Ie,toSchema:ie,toSimplePattern:_e,toVersion:se,unnamePattern:et,unnameSimplePattern:function(e){return"named"===e._variant?e.value.pattern:e.value}});function st(e){const t=new ct;return e.definitions.forEach(t.checkDefinition.bind(t)),t.problems.length>0?{ok:!1,problems:t.problems}:{ok:!0,schema:e}}!function(e){e[e.AVAILABLE=0]="AVAILABLE",e[e.NOT_AVAILABLE=1]="NOT_AVAILABLE"}(ot||(ot={}));class ct{constructor(){this.problems=[]}recordProblem(e,t){this.problems.push(`${t} in ${e}`)}checkBinding(e,t,r){const n=t.description;e.has(n)&&this.recordProblem(r,`duplicate binding named ${JSON.stringify(n)}`),Ve(n)||this.recordProblem(r,`invalid binding name ${JSON.stringify(n)}`),e.add(n)}checkDefinition(e,t){switch(e._variant){case"or":{const r=new Set;[e.pattern0,e.pattern1,...e.patternN].forEach((({variantLabel:e,pattern:n})=>{const i=`variant ${e} of ${t.description}`;r.has(e)&&this.recordProblem(i,"duplicate variant label"),Ve(e)||this.recordProblem(i,"invalid variant label"),r.add(e),this.checkPattern(new Set,n,i,ot.AVAILABLE)}));break}case"and":{const r=[e.pattern0,e.pattern1,...e.patternN],n=new Set;r.forEach((e=>this.checkNamedPattern(n,e,t.description)));break}case"Pattern":this.checkPattern(new Set,e.value,t.description,ot.AVAILABLE)}}checkNamedPattern(e,t,r){switch(t._variant){case"named":this.checkBinding(e,t.value.name,r),this.checkPattern(e,q.SimplePattern(t.value.pattern),`${JSON.stringify(t.value.name.description)} of ${r}`,ot.AVAILABLE);break;case"anonymous":this.checkPattern(e,t.value,r,ot.NOT_AVAILABLE)}}checkPattern(e,r,n,i){switch(r._variant){case"SimplePattern":"lit"!==r.value._variant&&i===ot.NOT_AVAILABLE&&this.recordProblem(n,"cannot recover serialization of non-literal pattern"),"Ref"!==r.value._variant||Ve(r.value.value.name.description)&&r.value.value.module.every((e=>Ve(e.description)))||this.recordProblem(n,"invalid reference name");break;case"CompoundPattern":(r=>{switch(r._variant){case"rec":this.checkNamedPattern(e,r.label,`label of ${n}`),this.checkNamedPattern(e,r.fields,`fields of ${n}`);break;case"tuple":r.patterns.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`)));break;case"tuplePrefix":r.fixed.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`))),this.checkNamedPattern(e,tt(r.variable),`tail of ${n}`);break;case"dict":r.entries.forEach(((r,i)=>this.checkNamedPattern(e,tt(r),`entry ${t.stringify(i)} in dictionary in ${n}`)))}})(r.value)}}}class dt extends Error{constructor(e,t){super(e),this.pos=t}}const ut=new WeakMap;function lt(e,t){return null===t&&console.error("Internal error in Schema reader: null source position for",e),null!==t&&ut.set(e,t),e}function ft(e){var t;return null!==(t=ut.get(e))&&void 0!==t?t:null}function bt(e,r){const n=[];let i=[];function o(){i.length>0&&(n.push(i),i=[])}for(const n of e)t.is(n,r)?o():i.push(n);return o(),n}function mt(e){var r;throw new dt(t.preserves`Invalid Schema clause: ${e}`,t.position(null!==(r=e[0])&&void 0!==r&&r))}function pt(e,r,n){throw new dt(`Invalid pattern in ${e}: ${t.stringify(r)}`,n)}function vt(e,r){return new t.Reader(e,{name:null==r?void 0:r.name,includeAnnotations:!0,embeddedDecode:t.genericEmbeddedTypeDecode}).readToEnd()}function _t(e,r){let n,i=F.$false(),o=new t.KeyedDictionary;if(function e(a){const s=bt(t.peel(a),Je);for(const a of s)if(a.length>=2&&t.is(a[1],He)){const e=t.position(a[0]),r=t.peel(a[0]);if("symbol"!=typeof r&&mt(a),!Ve(r.description))throw new dt(t.preserves`Invalid definition name: ${r}`,e);if(o.has(r))throw new dt(t.preserves`Duplicate definition: ${a}`,e);o.set(r,yt(r,e,a.slice(2)))}else if(2===a.length&&t.is(a[0],T))n=ae(t.peel(a[1]));else if(2===a.length&&t.is(a[0],S)){const e=t.position(a[1]),r=t.peel(a[1]);!1===r?i=F.$false():"symbol"==typeof r&&Fe(r.description)?i=F.Ref($t(r.description,e)):pt("embedded type name specification",r,e)}else if(2===a.length&&t.is(a[0],Ye)){const n=t.position(a[1]),i=t.peel(a[1]);if("string"!=typeof i)throw new dt(t.preserves`Invalid include: ${a}`,n);if(void 0===r.readInclude)throw new dt(t.preserves`Cannot include files in schema`,n);e(vt(r.readInclude(i),r))}else mt(a)}(e),void 0===n)throw new dt("Schema: missing version declaration.",null);return L({version:V(),embeddedType:i,definitions:o})}function ht(e){throw new dt("Named patterns must be Simple patterns",e)}function yt(e,r,n){function i(n){const i=wt(n)||wt(n[0]),o=gt(e,n);if(!1!==i)return G({variantLabel:i.description,pattern:o});if("CompoundPattern"===o._variant&&"rec"===o.value._variant&&"anonymous"===o.value.label._variant&&"SimplePattern"===o.value.label.value._variant&&"lit"===o.value.label.value.value._variant&&"symbol"==typeof o.value.label.value.value.value&&Ve(o.value.label.value.value.value.description))return G({variantLabel:o.value.label.value.value.value.description,pattern:o});if("SimplePattern"===o._variant&&"Ref"===o.value._variant&&Fe(o.value.value.name.description))return G({variantLabel:Le(o.value.value.name.description),pattern:o});if("SimplePattern"===o._variant&&"lit"===o.value._variant){const e=it(o.value.value);if(void 0!==e)return G({variantLabel:e,pattern:o})}throw new dt(t.preserves`Name missing for alternative: ${n}`,r)}function o(r){const n=wt(r)||wt(r[0]),i=gt(e,r);return!1!==n?("SimplePattern"!==i._variant&&ht(t.position(r[0])),H.named(Q({name:n,pattern:i.value}))):H.anonymous(i)}const a=bt(n,ze),s=bt(n,Ze);if(0===a.length||0===s.length)throw new dt(t.preserves`Invalid Schema clause: ${n}`,r);if(a.length>1&&s.length>1)throw new dt(t.preserves`Mixed "or" and "and" clause: ${n}`,r);return a.length>1?K.and({pattern0:o(a[0]),pattern1:o(a[1]),patternN:a.slice(2).map(o)}):s.length>1?K.or({pattern0:i(s[0]),pattern1:i(s[1]),patternN:s.slice(2).map(i)}):K.Pattern(gt(e,s[0]))}function St(e,r){return t.annotate(e,...t.annotations(r))}function gt(e,r){function n(r,n,o){const a=t.position(r),s=t.peel(r);function c(){pt(t.stringify(e),s,a)}if("symbol"!=typeof s){if(t.Record.isRecord(s)){const e=s.label;return t.Record.isRecord(e)?(0!==e.length&&c(),e.label===g?(1!==s.length&&c(),n(j.lit(s[0]))):o()):o()}if(Array.isArray(s)&&2===s.length&&t.is(s[1],Ue))return n(j.seqof(i(s[0])));if(Set.isSet(s)){1!==s.size&&c();const[e]=s.values();return n(j.setof(i(e)))}if(t.Dictionary.isDictionary(s)&&2===s.size&&s.has(Ue)){const e=s.clone();e.delete(Ue);const[[t,r]]=e.entries();return n(j.dictof({key:i(t),value:i(r)}))}return t.isCompound(s)?o():t.isEmbedded(s)?n(j.embedded(i(s.embeddedValue.generic))):n(j.lit(t.strip(s)))}{const e=s.description;switch(e){case"any":return n(j.any());case"bool":return n(j.atom(J.Boolean()));case"float":return n(j.atom(J.Float()));case"double":return n(j.atom(J.Double()));case"int":return n(j.atom(J.SignedInteger()));case"string":return n(j.atom(J.String()));case"bytes":return n(j.atom(J.ByteString()));case"symbol":return n(j.atom(J.Symbol()));default:if("="===e[0])return n(j.lit(Symbol.for(e.slice(1))));if(Fe(e))return n(j.Ref($t(e,a)));c()}}}const i=e=>n(e,(e=>e),(()=>{throw new dt("Compound patterns not accepted here",t.position(e))}));function o(e,r,i,o){return a=>{let s=wt(a);return!1===s&&void 0!==o&&"symbol"==typeof o&&Ve(o.description)&&(s=o),!1===s?r(i(a)):e(Q({name:s,pattern:n(a,(e=>e),(()=>ht(t.position(a))))}))}}const a=o(H.named,H.anonymous,(t=>gt(e,[t]))),s=o(U.named,U.anonymous,i),c=t.peel(r);return 1!==c.length&&pt(t.stringify(e),c,c.length>0?t.position(c[0]):t.position(c)),n(c[0],q.SimplePattern,(()=>q.CompoundPattern(function(r){const n=t.position(r),c=t.peel(r);function d(){pt(t.stringify(e),c,n)}if(t.Record.isRecord(c)){const n=c.label;if(!t.Record.isRecord(n))return z.rec({label:H.anonymous(q.SimplePattern(j.lit(n))),fields:H.anonymous(gt(e,[St([...c],r)]))});if(0!==n.length&&d(),n.label===P)return 2!==c.length&&d(),z.rec({label:a(c[0]),fields:a(c[1])});d()}else{if(Array.isArray(c)&&c.length>2&&t.is(c[c.length-1],Ue)){const e=c[c.length-2],t=St([e,Ue],e);return z.tuplePrefix({fixed:c.slice(0,c.length-2).map(a),variable:s(t)})}if(Array.isArray(c))return z.tuple(c.map(a));if(t.Dictionary.isDictionary(c)&&!c.has(Ue))return z.dict(c.mapEntries((([e,r])=>[t.strip(e),o(U.named,U.anonymous,i,t.strip(e))(r)])));d()}}(c[0]))))}function wt(e){if(!t.Annotated.isAnnotated(e))return!1;for(const r of e.annotations){const e=t.peel(r);if("symbol"==typeof e)return Ve(e.description)&&e}return!1}function $t(e,t){const r=e.split(".");return lt(W({module:r.slice(0,r.length-1).map(Symbol.for),name:Symbol.for(r[r.length-1])}),t)}const Pt=80;class Nt{constructor(){this.width=Pt,this.indentDelta=" ",this.currentIndent="\n",this.buffer=[]}get indentSize(){return this.indentDelta.length}set indentSize(e){this.indentDelta=new Array(e+1).join(" ")}write(e){"string"==typeof e?this.buffer.push(e):e.writeOn(this)}newline(){this.write(this.currentIndent)}toString(){return this.buffer.join("")}withIndent(e){const t=this.currentIndent;try{this.currentIndent=this.currentIndent+this.indentDelta,e()}finally{this.currentIndent=t}}clone(){const e=Object.assign(new Nt,this);return e.buffer=[],e}}class kt{constructor(e){if(e.some((e=>void 0===e)))throw new Error("aiee");this.items=e}get separator(){return""}get terminator(){return""}writeOn(e){let t=!1;this.items.forEach((r=>{t?e.write(this.separator):t=!0,e.write(r)})),e.write(this.terminator)}}class At extends kt{get separator(){return", "}}class Et extends At{writeHorizontally(e){e.write(this.open),super.writeOn(e),e.write(this.close)}writeVertically(e){e.write(this.open),this.items.length>0&&(e.withIndent((()=>{this.items.forEach(((t,r)=>{e.newline(),e.write(t);const n=r===this.items.length-1?this.terminator:this.separator;e.write(n.trimRight())}))})),e.newline()),e.write(this.close)}writeOn(e){const t=e.clone();this.writeHorizontally(t);const r=t.toString();r.length<=e.width?e.write(r):this.writeVertically(e)}}class Rt extends Et{get open(){return"("}get close(){return")"}}class Dt extends Rt{constructor(e,t){super(t),this.operator=e}get separator(){return this.operator}}class Tt extends Et{get open(){return"["}get close(){return"]"}}class xt extends Et{get open(){return"<"}get close(){return">"}}class It extends Et{get open(){return"{"}get close(){return"}"}}class Bt extends It{get separator(){return"; "}get terminator(){return";"}}const Ct=(...e)=>new kt(e),Ot=(...e)=>new Rt(e),Mt=(e,t,...r)=>0===r.length?e:new Dt(t,r),Lt=(...e)=>new Tt(e),Vt=(...e)=>new xt(e),Ft=(...e)=>new It(e),Kt=(...e)=>1===e.length&&e[0]instanceof Bt?e[0]:new Bt(e),qt=(e,t)=>Ct(JSON.stringify(e),": ",t);var jt;!function(e){e.union=e=>({kind:"union",variants:e}),e.unit=()=>({kind:"unit"}),e.ref=(e,t)=>({kind:"ref",typeName:e,ref:t}),e.array=e=>({kind:"array",type:e}),e.set=e=>({kind:"set",type:e}),e.dictionary=(e,t)=>({kind:"dictionary",key:e,value:t}),e.record=e=>({kind:"record",fields:e})}(jt||(jt={}));const zt=jt.ref("_.Value",null);var Jt=Object.freeze({__proto__:null,ANY_TYPE:zt,get Type(){return jt}});function Ut(e){return qt("_variant",JSON.stringify(e))}function Ht(e,t,r){switch(r.kind){case"unit":return[];case"ref":case"set":case"dictionary":case"array":return[qt("value",Yt(e,r))];case"record":return Array.from(r.fields).map((([t,r])=>qt(t,Yt(e,r))));default:throw new Error("Unreachable")}}function Yt(e,t){switch(t.kind){case"union":return Mt("never"," | ",...Array.from(t.variants).flatMap((t=>function(e,t,[r,n]){let i=Ht(e,0,n);return Ft(Ut(r),...i)}(e,0,t))));case"unit":case"record":return Ft(...Ht(e,0,t));case"ref":return null===t.ref&&"_embedded"===t.typeName?t.typeName:Ct(t.typeName,e.genericArgsFor(t));case"set":return Ct("_.KeyedSet",Vt(Yt(e,t.type),"_embedded"));case"dictionary":return Ct("_.KeyedDictionary",Vt(Yt(e,t.key),Yt(e,t.value),"_embedded"));case"array":return Ct("Array",Vt(Yt(e,t.type)));default:throw new Error("Unreachable")}}function Zt(e,t){switch(t._variant){case"or":return jt.union(new Map([t.pattern0,t.pattern1,...t.patternN].map((t=>[t.variantLabel,Qt(e,t.pattern)]))));case"and":return Gt(e,[t.pattern0,t.pattern1,...t.patternN]);case"Pattern":return Qt(e,t.value)}}function Gt(e,t){const r=new Map;return t.forEach((t=>Xt(r,e,t))),r.size>0?jt.record(r):jt.unit()}function Qt(e,t){return"SimplePattern"===t._variant?Wt(e,t.value):Gt(e,[H.anonymous(t)])}function Wt(e,t){switch(t._variant){case"any":return zt;case"atom":switch(t.atomKind._variant){case"Boolean":return jt.ref("boolean",null);case"Float":case"Double":case"SignedInteger":return jt.ref("number",null);case"String":return jt.ref("string",null);case"ByteString":return jt.ref("_.Bytes",null);case"Symbol":return jt.ref("symbol",null)}case"embedded":return jt.ref("_embedded",null);case"lit":return jt.unit();case"seqof":return jt.array(Wt(e,t.pattern));case"setof":return jt.set(Wt(e,t.pattern));case"dictof":return jt.dictionary(Wt(e,t.key),Wt(e,t.value));case"Ref":return e(t.value);default:throw new Error("Unreachable")}}function Xt(e,t,r){if("named"===r._variant){const n=Wt(t,r.value.pattern);"unit"!==n.kind&&e.set(r.value.name.description,n)}else"CompoundPattern"===r.value._variant&&function(e,t,r){switch(r._variant){case"rec":Xt(e,t,r.label),Xt(e,t,r.fields);break;case"tuple":r.patterns.forEach((r=>Xt(e,t,r)));break;case"tuplePrefix":r.fixed.forEach((r=>Xt(e,t,r))),Xt(e,t,tt(r.variable));break;case"dict":r.entries.forEach(((r,n)=>Xt(e,t,tt(r))));break;default:throw new Error("Unreachable")}}(e,t,r.value.value)}var er=Object.freeze({__proto__:null,simpleType:Wt,typeFor:Qt,typeForDefinition:Zt,typeForIntersection:Gt});class tr{constructor(e,r,n,i){switch(this.literals=new t.Dictionary,this.preamble=[],this.typedefs=[],this.functiondefs=[],this.imports=new t.KeyedSet,this.env=e,this.modulePath=r,this.schema=n,this.options=i,n.embeddedType._variant){case"false":this.embeddedType="_.GenericEmbedded";break;case"Ref":{const e=this.resolver()(n.embeddedType.value);this.embeddedType=e.typeName;break}}}literal(e){let r=this.literals.get(e);return void 0===r&&(r=qe("$"+t.stringify(e),(()=>"__lit"+this.literals.size)),this.literals.set(e,r)),r}derefPattern(e,t=0){if(t>128)throw new Error("Recursion limit exceeded");return"Pattern"===e._variant&&"SimplePattern"===e.value._variant&&"Ref"===e.value.value._variant?this.lookup(e.value.value.value,((e,r)=>this.derefPattern(e,t+1)),((r,n,i,o,a,s)=>this.derefPattern(null!=a?a:e,t+1))):e}definePreamble(e){this.preamble.push(e)}defineType(e){this.typedefs.push(e)}defineFunctions(e,t){this.functiondefs.push(...t(new rr(this,e)))}resolver(e){return t=>this.lookup(t,((e,r)=>jt.ref(t.name.description,t)),((e,r,n,i,o,a)=>(this.imports.add([e,r,n,i]),jt.ref(`${r}${i}.${t.name.description}`,t))),e)}lookupType(e,t){const r=this.lookup(e,((e,t)=>t),((e,t,r,n,i,o)=>o),t);return r?r():null}lookup(e,t,r,n){const i=e.module.length?e.module:null!=n?n:this.modulePath,o=Ge(this.env,i);if(null!==o){const n=null===o.typescriptModuleExpr?"":"."+o.typescriptModuleExpr;if(null===o.schema)return r(i,Qe(o),o.typescriptModulePath,n,null,null);{const a=o.schema.definitions.get(e.name);if(void 0!==a){let s=()=>Zt(this.resolver(i),a);return e.module.length?r(i,Qe(o),o.typescriptModulePath,n,a,s):t(a,s)}}}throw new dt(`Undefined reference: ${Xe(e)}`,ft(e))}genericParameters(){return Vt(Ct("_embedded = ",this.embeddedType))}genericParametersFor(e){return this.hasEmbedded(e)?this.genericParameters():""}genericArgs(){return"<_embedded>"}genericArgsFor(e){return this.hasEmbedded(e)?this.genericArgs():""}hasEmbedded(e){const t=this,r=new nr(this.modulePath);return function e(n){switch(n.kind){case"union":for(const t of n.variants.values())if(e(t))return!0;return!1;case"unit":return!1;case"array":return e(n.type);case"set":case"dictionary":return!0;case"ref":if(null!==n.ref)return r.cycleCheck(n.ref,(e=>t.lookupType(e,r.modulePath)),(t=>!!t&&e(t)),(()=>!1));switch(n.typeName){case"_embedded":case"_.Value":return!0;default:return!1}case"record":for(const t of n.fields.values())if(e(t))return!0;return!1}}(e)}withAsPreserveMixinType(e,t){return"unit"===t.kind||"record"===t.kind||"union"===t.kind?Mt("any"," & ",Ct(e,this.genericArgsFor(t)),Ft(Ct("__as_preserve__",this.hasEmbedded(t)?"":this.genericParameters(),"()",": _.Value",this.genericArgs()))):Ct(e,this.genericArgsFor(t))}}class rr{constructor(e,t){this.tempCounter=0,this.temps=new Map,this.captures=[],this.variantName=void 0,this.mod=e,this.definitionName=t}gentempname(){return"_tmp"+this.tempCounter++}gentemp(e=zt){const t=Yt(this.mod,e),r=function(e,t=Pt){const r=new Nt;return r.width=t,e.forEach((e=>r.write(e))),r.toString()}([t],1/0),n=this.gentempname();let i=this.temps.get(r);return void 0===i&&(i={type:t,names:[]},this.temps.set(r,i)),i.names.push(n),n}block(e){const t=this.temps;this.temps=new Map;const r=e(),n=this.temps;return this.temps=t,Kt(...Array.from(n).map((([e,{type:t,names:r}])=>Ct("let ",((...e)=>new At(e))(...r),": (",t,") | undefined"))),...r)}withCapture(e,t,r){void 0!==e&&this.captures.push({fieldName:e,sourceExpr:t});const n=r(t);return void 0!==e&&this.captures.pop(),n}convertCapture(e,t,r){return this.withCapture(e,t,(e=>Ct(`if (${e} !== void 0) `,this.block((()=>r())))))}buildCapturedCompound(e){const t=[...(r=this.variantName,void 0===r?[]:[Ut(r)]),...this.captures.map((({fieldName:e,sourceExpr:t})=>qt(e,t))),Ct("__as_preserve__() ",Kt(`return from${this.definitionName}(this)`))];var r;return Ct(`${e} = `,Ft(...t))}}class nr{constructor(e){this.modulePath=e,this.seen=new t.FlexSet(ir)}cycleCheck(e,t,r,n){const i=W({module:e.module.length?e.module:this.modulePath,name:e.name});if(this.seen.has(i))return n();{this.seen.add(i);const e=t(i),n=this.modulePath;this.modulePath=i.module;const o=r(e);return this.modulePath=n,o}}}function ir(e){return t.stringify([...e.module,e.name])}function or(e,t,r,n){switch(t._variant){case"or":{const i=[t.pattern0,t.pattern1,...t.patternN];function o(t){return e.variantName=i[t].variantLabel,[...ar(e,i[t].pattern,r,n),...t<i.length-1?[Ct(`if (${n} === void 0) `,e.block((()=>o(t+1))))]:[]]}return o(0)}case"and":{const a=[t.pattern0,t.pattern1,...t.patternN];function s(t){return t<a.length?cr(e,a[t],r,(()=>s(t+1))):[e.buildCapturedCompound(n)]}return s(0)}case"Pattern":return e.variantName=void 0,ar(e,t.value,r,n)}}function ar(e,t,r,n){return cr(e,H.anonymous(t),r,(r=>void 0===r?[e.buildCapturedCompound(n)]:void 0!==e.variantName?"unit"===Qt(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[e.withCapture("value",r,(()=>e.buildCapturedCompound(n)))]:"unit"===Qt(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[`${n} = ${r}`]))}function sr(e,t,r,n,i,o){function a(n){if(n<t.length)return cr(e,t[n],`${r}[${n}]`,(()=>a(n+1)));if(void 0===i)return o();{const n=e.gentemp(jt.array(zt));return[t.length>0?`${n} = ${r}.slice(${t.length})`:`${n} = ${r}`,...cr(e,tt(i),n,o,!0)]}}const s=void 0===i?Ct(` && ${r}.length === ${t.length}`):0===t.length?"":Ct(` && ${r}.length >= ${t.length}`);return n?a(0):[Ct(`if (_.isSequence(${r})`,s,") ",e.block((()=>a(0))))]}function cr(e,t,r,n,i=!1){let o=et(t),a=rt(t);if("SimplePattern"===o._variant){const t=e.gentemp(Wt(e.mod.resolver(),o.value));return[...dr(e,o.value,r,t,i),e.convertCapture(a,t,(()=>n(t)))]}return function(e,t,r,n,i){switch(t._variant){case"rec":return[Ct(`if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(${r})) `,e.block((()=>cr(e,t.label,`${r}.label`,(()=>cr(e,t.fields,r,i,!0))))))];case"tuple":return sr(e,t.patterns,r,n,void 0,i);case"tuplePrefix":return sr(e,t.fixed,r,n,t.variable,i);case"dict":{const o=Array.from(t.entries);function a(t){if(t<o.length){const[n,i]=o[t],s=e.gentemp();return[Ct(`if ((${s} = ${r}.get(${e.mod.literal(n)})) !== void 0) `,e.block((()=>cr(e,tt(i),s,(()=>a(t+1))))))]}return i()}return[Ct(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>a(0))))]}default:throw new Error("Unreachable")}}(e,o.value,r,i,(()=>n(void 0)))}function dr(e,t,r,n,i){switch(t._variant){case"any":return[`${n} = ${r}`];case"atom":{let e,i=`${r}`;switch(t.atomKind._variant){case"Boolean":e=`typeof ${r} === 'boolean'`;break;case"Float":e=`_.Float.isSingle(${r})`,i=`${r}.value`;break;case"Double":e=`_.Float.isDouble(${r})`,i=`${r}.value`;break;case"SignedInteger":e=`typeof ${r} === 'number'`;break;case"String":e=`typeof ${r} === 'string'`;break;case"ByteString":e=`_.Bytes.isBytes(${r})`;break;case"Symbol":e=`typeof ${r} === 'symbol'`}return[Ct(`${n} = `,e," ? ",i," : void 0")]}case"embedded":return[`${n} = _.isEmbedded<_embedded>(${r}) ? ${r}.embeddedValue : void 0`];case"lit":return[`${n} = _.is(${r}, ${e.mod.literal(t.value)}) ? {} : void 0`];case"seqof":{const o=()=>{const i=e.gentempname();return[Ct(`${n} = []`),Ct(`for (const ${i} of ${r}) `,e.block((()=>[...cr(e,nt(t.pattern),i,(e=>[`${n}.push(${e})`,"continue"])),Ct(`${n} = void 0`),Ct("break")])))]};return i?o():[`${n} = void 0`,Ct(`if (_.isSequence(${r})) `,e.block(o))]}case"setof":return[`${n} = void 0`,Ct(`if (_.Set.isSet<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname();return[Ct(`${n} = new _.KeyedSet()`),Ct(`for (const ${i} of ${r}) `,e.block((()=>[...cr(e,nt(t.pattern),i,(e=>[`${n}.add(${e})`,"continue"])),Ct(`${n} = void 0`),Ct("break")])))]})))];case"dictof":return[`${n} = void 0`,Ct(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname(),o=e.gentempname();return[Ct(`${n} = new _.KeyedDictionary()`),Ct(`for (const [${o}, ${i}] of ${r}) `,e.block((()=>[...cr(e,nt(t.key),o,(r=>cr(e,nt(t.value),i,(e=>[`${n}.set(${r}, ${e})`,"continue"])))),Ct(`${n} = void 0`),Ct("break")])))]})))];case"Ref":return e.mod.lookup(t.value,((e,i)=>[`${n} = to${t.value.name.description}(${r})`]),((i,o,a,s,c,d)=>(e.mod.imports.add([i,o,a,s]),[`${n} = ${o}${s}.to${t.value.name.description}${e.mod.genericArgs()}(${r})`])));default:throw new Error("Unreachable")}}function ur(e,t,r,n,i,o,a){const s=[];let c=!1;function d(e,t){"unit"!==e.kind&&s.push([t,e])}"record"===i.kind?i.fields.forEach(d):(d(i,"value"),c=void 0===n&&"unit"!==i.kind);const u=void 0!==n?[qt("_variant",JSON.stringify(n))]:[];s.forEach((([e,t])=>u.push(Ct(JSON.stringify(e),": ",qe(e))))),u.push(Ct("__as_preserve__() ",Kt(`return from${qe(t)}(this)`)));const l=s.length>1?[Ct(Ft(...s.map((e=>qe(e[0])))),": ",Ft(...s.map((t=>Ct(qe(t[0]),": ",Yt(e,t[1]))))))]:s.map((t=>Ct(qe(t[0]),": ",Yt(e,t[1]))));return[Ct(`export function ${qe(r)}`,e.genericParametersFor(o),Ot(...l),": ",a," ",Kt(Ct("return ",c?"value":Ft(...u)))),Ct(`${qe(r)}.schema = function () `,Kt(Ct("return ",Ft("schema: _schema()","imports: _imports",`definitionName: _.Symbol.for(${JSON.stringify(t)})`,...void 0===n?[]:[`variant: _.Symbol.for(${JSON.stringify(n)})`]))))]}function lr(e,t,r){switch(t._variant){case"or":return[Ct(`switch (${r}._variant) `,Kt(...[t.pattern0,t.pattern1,...t.patternN].map((t=>Ct("case ",JSON.stringify(t.variantLabel),": ",e.block((()=>{const n="SimplePattern"===t.pattern._variant;return[Ct("return ",fr(e,t.pattern,n?`${r}.value`:r))]})))))))];case"and":return[Ct("return _.merge",Ot("(a, b) => (a === b) ? a : void 0",...[t.pattern0,t.pattern1,...t.patternN].flatMap((t=>"anonymous"===t._variant&&"SimplePattern"===t.value._variant?[]:[mr(e,t,r)]))))];case"Pattern":return[Ct("return ",fr(e,t.value,`${r}`))]}}function fr(e,t,r){switch(t._variant){case"SimplePattern":return(t=>{switch(t._variant){case"any":return`${r}`;case"atom":switch(t.atomKind._variant){case"Float":return`_.Single(${r})`;case"Double":return`_.Double(${r})`;default:return`${r}`}case"lit":return e.mod.literal(t.value);case"embedded":return`_.embed(${r})`;case"seqof":return Ct(`${r}.map(v => `,fr(e,q.SimplePattern(t.pattern),"v"),")");case"setof":return Ct("new _.Set<_embedded>",Ot(`_.Array.from(${r}.values()).map(v => `,fr(e,q.SimplePattern(t.pattern),"v"),")"));case"dictof":return Ct("new _.Dictionary<_embedded>",Ot(Ct(`_.Array.from(${r}.entries()).map(([k, v]) => `,Lt(fr(e,q.SimplePattern(t.key),"k"),fr(e,q.SimplePattern(t.value),"v")),")")));case"Ref":return e.mod.lookup(t.value,((n,i)=>`from${t.value.name.description}${e.mod.genericArgs()}(${r})`),((n,i,o,a,s,c)=>(e.mod.imports.add([n,i,o,a]),`${i}${a}.from${t.value.name.description}${e.mod.genericArgs()}(${r})`)))}})(t.value);case"CompoundPattern":return(t=>{switch(t._variant){case"rec":return Ct("_.Record",Ot(mr(e,t.label,r),mr(e,t.fields,r)));case"tuple":return Lt(...t.patterns.map((t=>mr(e,t,r))));case"tuplePrefix":{const n=mr(e,tt(t.variable),r);return 0===t.fixed.length?n:Lt(...t.fixed.map((t=>mr(e,t,r))),Ct("... ",n))}case"dict":return Ct("new _.Dictionary<_embedded>",Ot(Lt(...Array.from(t.entries.entries()).map((([t,n])=>Lt(e.mod.literal(t),function(e,t,r){if("named"===t._variant){const n=br(r,t.value.name.description);return fr(e,q.SimplePattern(t.value.pattern),n)}return fr(e,q.SimplePattern(t.value),r)}(e,n,r)))))))}})(t.value)}}function br(e,t){return`${e}[${JSON.stringify(t)}]`}function mr(e,t,r){if("named"===t._variant){const n=br(r,t.value.name.description);return fr(e,q.SimplePattern(t.value.pattern),n)}return fr(e,t.value,r)}e.GenType=er,e.Meta=at,e.SchemaSyntaxError=dt,e.Type=Jt,e.checkSchema=st,e.compile=function(e,r,n,i={}){var o;const a=new tr(e,r,n,i);a.definePreamble("let __schema: _.Value | null = null;"),a.definePreamble(Ct("export function _schema() ",Kt(Ct("if (__schema === null) ",Kt(`__schema = _.decode<_.GenericEmbedded>(_.Bytes.fromHex("${t.encode(oe(n)).toHex()}"))`)),"return __schema"))),"false"!==n.embeddedType._variant&&a.defineType(Ct("export type _embedded = ",a.embeddedType,";"));for(const[e,r]of n.definitions){const n=Zt(a.resolver(),r),i=t.stringify(e),o=a.withAsPreserveMixinType(i,n);a.defineType(Ct(`export type ${i}`,a.genericParametersFor(n)," = ",Yt(a,n),";")),"union"===n.kind?a.defineFunctions(i,(e=>[Ct(`export namespace ${i} `,Kt(...Array.from(n.variants).flatMap((([e,t])=>ur(a,i,e,e,t,n,o)))))])):a.defineFunctions(i,(e=>ur(a,i,i,void 0,n,n,o)))}for(const[e,t]of n.definitions){const r=Zt(a.resolver(),t),n=e,i=e.description,o=a.withAsPreserveMixinType(i,r);a.defineFunctions(i,(e=>[Ct(`export function as${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): ",o," ",e.block((()=>[Ct(`let result = to${n.description}(v)`),Ct("if (result === void 0) ",`throw new TypeError(\`Invalid ${n.description}: \${_.stringify(v)}\`)`),Ct("return result")])))])),a.defineFunctions(i,(e=>[Ct(`export function to${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): undefined | ",o," ",e.block((()=>[Ct("let result: undefined | ",o),...or(e,t,"v","result"),Ct("return result")]))),"union"===r.kind?Ct(`export namespace ${qe(n.description)} `,Kt(Ct(`export const __from_preserve__ = to${n.description}`))):Ct(`${qe(n.description)}.__from_preserve__ = to${n.description};`)])),a.defineFunctions(i,(e=>[Ct(`export function from${n.description}`,a.genericParameters(),"(_v: ",n.description,a.genericArgsFor(r),"): _.Value<_embedded> ",e.block((()=>lr(e,t,"_v"))))]))}a.definePreamble(Ct("export const _imports = ",Ft(...Array.from(a.imports.values()).map((([e,r,n,i])=>Ct(t.stringify(We(e)),": ",r,i))))));const s=new Nt;s.write(`import * as _ from ${JSON.stringify(null!==(o=i.preservesModule)&&void 0!==o?o:"@preserves/core")};\n`),a.imports.forEach((([e,t,r,n])=>{s.write(`import * as ${t} from ${JSON.stringify(r)};\n`)})),s.newline();const c=Array.from(a.literals);c.sort(((e,t)=>e[1]<t[1]?-1:e[1]===t[1]?0:1));for(const[e,r]of c)s.write(Ct(`export const ${r} = `,(d=e,t.fold(d,{boolean:e=>e.toString(),single:e=>e.toString(),double:e=>e.toString(),integer:e=>e.toString(),string:e=>JSON.stringify(e),bytes:e=>Ct("Uint8Array.from(",Lt(...Array.from(e).map((e=>e.toString()))),")"),symbol:e=>`_.Symbol.for(${JSON.stringify(e.description)})`,record:(e,t)=>Ct("_.Record<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>",Ot(t(e.label),Lt(...e.map(t)))),array:(e,t)=>Lt(...e.map(t)),set:(e,t)=>Ct("new _.Set<_.Value<_embedded>>",Ot(Lt(...Array.from(e).map(t)))),dictionary:(e,t)=>Ct("new _.Dictionary<_embedded>",Ot(Lt(...Array.from(e).map((([e,r])=>Lt(t(e),t(r))))))),annotated:(e,t)=>Ct("_.annotate<_embedded>",Ot(t(e.item),...e.annotations.map(t))),embedded(e,r){throw new Error(`Cannot emit source code for construction of embedded ${t.stringify(e)}`)}})),";\n"));var d;return s.newline(),a.preamble.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.typedefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.functiondefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.toString()},e.parseSchema=_t,e.readSchema=function(e,t){const r=st(_t(vt(e,t),null!=t?t:{}));if(r.ok)return r.schema;throw new Error("Schema is not invertible:\n"+r.problems.map((e=>" - "+e)).join("\n"))},e.recordPosition=lt,e.refPosition=ft}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@preserves/core")):"function"==typeof define&&define.amd?define(["exports","@preserves/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).PreservesSchema={},e.Preserves)}(this,(function(e,t){"use strict";function r(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=r(t);const i=1,o=n.Symbol.for("Boolean"),a=n.Symbol.for("ByteString"),s=n.Symbol.for("Double"),c=n.Symbol.for("Float"),d=n.Symbol.for("SignedInteger"),u=n.Symbol.for("String"),l=n.Symbol.for("Symbol"),f=n.Symbol.for("and"),b=n.Symbol.for("any"),m=n.Symbol.for("atom"),p=n.Symbol.for("bundle"),v=n.Symbol.for("definitions"),_=n.Symbol.for("dict"),h=n.Symbol.for("dictof"),y=n.Symbol.for("embedded"),S=n.Symbol.for("embeddedType"),g=n.Symbol.for("lit"),w=n.Symbol.for("named"),$=n.Symbol.for("or"),P=n.Symbol.for("rec"),N=n.Symbol.for("ref"),k=n.Symbol.for("schema"),A=n.Symbol.for("seqof"),E=n.Symbol.for("setof"),R=n.Symbol.for("tuple"),D=n.Symbol.for("tuplePrefix"),T=n.Symbol.for("version"),x=!1;let I=null;function B(){return null===I&&(I=n.decode(n.Bytes.fromHex("b4b306736368656d61b7b30776657273696f6e91b30b646566696e6974696f6e73b7b303526566b4b303726563b4b3036c6974b30372656684b4b3057475706c65b5b4b3056e616d6564b3066d6f64756c65b4b303726566b584b30a4d6f64756c65506174688484b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484848484b30642756e646c65b4b303726563b4b3036c6974b30662756e646c6584b4b3057475706c65b5b4b3056e616d6564b3076d6f64756c6573b4b303726566b584b3074d6f64756c65738484848484b306536368656d61b4b303726563b4b3036c6974b306736368656d6184b4b3057475706c65b5b4b30464696374b7b30776657273696f6eb4b3056e616d6564b30776657273696f6eb4b303726566b584b30756657273696f6e8484b30b646566696e6974696f6e73b4b3056e616d6564b30b646566696e6974696f6e73b4b303726566b584b30b446566696e6974696f6e738484b30c656d62656464656454797065b4b3056e616d6564b30c656d62656464656454797065b4b303726566b584b310456d626564646564547970654e616d6584848484848484b30742696e64696e67b4b303726563b4b3036c6974b3056e616d656484b4b3057475706c65b5b4b3056e616d6564b3046e616d65b4b30461746f6db30653796d626f6c8484b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e8484848484b3074d6f64756c6573b4b306646963746f66b4b303726566b584b30a4d6f64756c655061746884b4b303726566b584b306536368656d618484b3075061747465726eb4b3026f72b5b5b10d53696d706c655061747465726eb4b303726566b584b30d53696d706c655061747465726e8484b5b10f436f6d706f756e645061747465726eb4b303726566b584b30f436f6d706f756e645061747465726e84848484b30756657273696f6eb4b3036c69749184b30841746f6d4b696e64b4b3026f72b5b5b107426f6f6c65616eb4b3036c6974b307426f6f6c65616e8484b5b105466c6f6174b4b3036c6974b305466c6f61748484b5b106446f75626c65b4b3036c6974b306446f75626c658484b5b10d5369676e6564496e7465676572b4b3036c6974b30d5369676e6564496e74656765728484b5b106537472696e67b4b3036c6974b306537472696e678484b5b10a42797465537472696e67b4b3036c6974b30a42797465537472696e678484b5b10653796d626f6cb4b3036c6974b30653796d626f6c84848484b30a446566696e6974696f6eb4b3026f72b5b5b1026f72b4b303726563b4b3036c6974b3026f7284b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b3104e616d6564416c7465726e61746976658484b4b3056e616d6564b3087061747465726e31b4b303726566b584b3104e616d6564416c7465726e6174697665848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b3104e616d6564416c7465726e61746976658484848484848484b5b103616e64b4b303726563b4b3036c6974b303616e6484b4b3057475706c65b5b4b30b7475706c65507265666978b5b4b3056e616d6564b3087061747465726e30b4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3087061747465726e31b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087061747465726e4eb4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e8484848484848484b5b1075061747465726eb4b303726566b584b3075061747465726e84848484b30a4d6f64756c6550617468b4b3057365716f66b4b30461746f6db30653796d626f6c8484b30b446566696e6974696f6e73b4b306646963746f66b4b30461746f6db30653796d626f6c84b4b303726566b584b30a446566696e6974696f6e8484b30c4e616d65645061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b3075061747465726e84848484b30d53696d706c655061747465726eb4b3026f72b5b5b103616e79b4b3036c6974b303616e798484b5b10461746f6db4b303726563b4b3036c6974b30461746f6d84b4b3057475706c65b5b4b3056e616d6564b30861746f6d4b696e64b4b303726566b584b30841746f6d4b696e64848484848484b5b108656d626564646564b4b303726563b4b3036c6974b308656d62656464656484b4b3057475706c65b5b4b3056e616d6564b309696e74657266616365b4b303726566b584b30d53696d706c655061747465726e848484848484b5b1036c6974b4b303726563b4b3036c6974b3036c697484b4b3057475706c65b5b4b3056e616d6564b30576616c7565b303616e798484848484b5b1057365716f66b4b303726563b4b3036c6974b3057365716f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b1057365746f66b4b303726563b4b3036c6974b3057365746f6684b4b3057475706c65b5b4b3056e616d6564b3077061747465726eb4b303726566b584b30d53696d706c655061747465726e848484848484b5b106646963746f66b4b303726563b4b3036c6974b306646963746f6684b4b3057475706c65b5b4b3056e616d6564b3036b6579b4b303726566b584b30d53696d706c655061747465726e8484b4b3056e616d6564b30576616c7565b4b303726566b584b30d53696d706c655061747465726e848484848484b5b103526566b4b303726566b584b30352656684848484b30f436f6d706f756e645061747465726eb4b3026f72b5b5b103726563b4b303726563b4b3036c6974b30372656384b4b3057475706c65b5b4b3056e616d6564b3056c6162656cb4b303726566b584b30c4e616d65645061747465726e8484b4b3056e616d6564b3066669656c6473b4b303726566b584b30c4e616d65645061747465726e848484848484b5b1057475706c65b4b303726563b4b3036c6974b3057475706c6584b4b3057475706c65b5b4b3056e616d6564b3087061747465726e73b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e84848484848484b5b10b7475706c65507265666978b4b303726563b4b3036c6974b30b7475706c6550726566697884b4b3057475706c65b5b4b3056e616d6564b3056669786564b4b3057365716f66b4b303726566b584b30c4e616d65645061747465726e848484b4b3056e616d6564b3087661726961626c65b4b303726566b584b3124e616d656453696d706c655061747465726e848484848484b5b10464696374b4b303726563b4b3036c6974b3046469637484b4b3057475706c65b5b4b3056e616d6564b307656e7472696573b4b303726566b584b31144696374696f6e617279456e74726965738484848484848484b310456d626564646564547970654e616d65b4b3026f72b5b5b10566616c7365b4b3036c6974808484b5b103526566b4b303726566b584b30352656684848484b3104e616d6564416c7465726e6174697665b4b3057475706c65b5b4b3056e616d6564b30c76617269616e744c6162656cb4b30461746f6db306537472696e678484b4b3056e616d6564b3077061747465726eb4b303726566b584b3075061747465726e84848484b31144696374696f6e617279456e7472696573b4b306646963746f66b303616e79b4b303726566b584b3124e616d656453696d706c655061747465726e8484b3124e616d656453696d706c655061747465726eb4b3026f72b5b5b1056e616d6564b4b303726566b584b30742696e64696e678484b5b109616e6f6e796d6f7573b4b303726566b584b30d53696d706c655061747465726e8484848484b30c656d62656464656454797065808484"))),I}const C={};function O(e){return{modules:e,__as_preserve__(){return te(this)}}}function M(e){return e}function L({version:e,embeddedType:t,definitions:r}){return{version:e,embeddedType:t,definitions:r,__as_preserve__(){return oe(this)}}}function V(){return{__as_preserve__:()=>ce()}}var F,K,q,j,z,J,U,H;function Y(e){return e}function Z(e){return e}function G({variantLabel:e,pattern:t}){return{variantLabel:e,pattern:t,__as_preserve__(){return ke(this)}}}function Q({name:e,pattern:t}){return{name:e,pattern:t,__as_preserve__(){return xe(this)}}}function W({module:e,name:t}){return{module:e,name:t,__as_preserve__(){return Be(this)}}}function X(e){return e}function ee(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,p)?{}:void 0,void 0!==r){let r;r=re(e[0]),void 0!==r&&(t={modules:r,__as_preserve__(){return te(this)}})}}return t}function te(e){return n.Record(p,[ne(e.modules)])}function re(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e=Ce(r),void 0!==e){let r;if(r=ie(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function ne(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[Oe(e),oe(t)])))}function ie(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,k)?{}:void 0,void 0!==r&&n.Dictionary.isDictionary(e[0])){let r;if(void 0!==(r=e[0].get(T))){let n;if(n=se(r),void 0!==n){let r;if(void 0!==(r=e[0].get(S))){let i;if(i=de(r),void 0!==i){let r;if(void 0!==(r=e[0].get(v))){let e;e=le(r),void 0!==e&&(t={version:n,embeddedType:i,definitions:e,__as_preserve__(){return oe(this)}})}}}}}}}return t}function oe(e){return n.Record(k,[new n.Dictionary([[T,ce(e.version)],[S,ue(e.embeddedType)],[v,fe(e.definitions)]])])}function ae(e){let t=se(e);if(void 0===t)throw new TypeError(`Invalid Version: ${n.stringify(e)}`);return t}function se(e){let t,r;return t=n.is(e,i)?{}:void 0,void 0!==t&&(r={__as_preserve__:()=>ce()}),r}function ce(e){return i}function de(e){let t,r;if(t=n.is(e,x)?{}:void 0,void 0!==t&&(r={_variant:"false",__as_preserve__(){return ue(this)}}),void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"Ref",value:t,__as_preserve__(){return ue(this)}})}return r}function ue(e){switch(e._variant){case"false":return x;case"Ref":return Be(e.value)}}function le(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e="symbol"==typeof r?r:void 0,void 0!==e){let r;if(r=be(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function fe(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[e,me(t)])))}function be(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,$)?{}:void 0,void 0!==r&&n.isSequence(e[0])&&e[0].length>=2){let r;if(r=Ne(e[0][0]),void 0!==r){let n;if(n=Ne(e[0][1]),void 0!==n){let i,o;i=e[0].slice(2),o=[];for(const e of i){let t;if(t=Ne(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(t={_variant:"or",pattern0:r,pattern1:n,patternN:o,__as_preserve__(){return me(this)}})}}}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,f)?{}:void 0,void 0!==r&&n.isSequence(e[0])&&e[0].length>=2){let r;if(r=Re(e[0][0]),void 0!==r){let n;if(n=Re(e[0][1]),void 0!==n){let i,o;i=e[0].slice(2),o=[];for(const e of i){let t;if(t=Re(e),void 0===t){o=void 0;break}o.push(t)}void 0!==o&&(t={_variant:"and",pattern0:r,pattern1:n,patternN:o,__as_preserve__(){return me(this)}})}}}}if(void 0===t){let r;r=pe(e),void 0!==r&&(t={_variant:"Pattern",value:r,__as_preserve__(){return me(this)}})}}return t}function me(e){switch(e._variant){case"or":return n.Record($,[[ke(e.pattern0),ke(e.pattern1),...e.patternN.map((e=>ke(e)))]]);case"and":return n.Record(f,[[De(e.pattern0),De(e.pattern1),...e.patternN.map((e=>De(e)))]]);case"Pattern":return ve(e.value)}}function pe(e){let t,r;if(t=_e(e),void 0!==t&&(r={_variant:"SimplePattern",value:t,__as_preserve__(){return ve(this)}}),void 0===r){let t;t=ye(e),void 0!==t&&(r={_variant:"CompoundPattern",value:t,__as_preserve__(){return ve(this)}})}return r}function ve(e){switch(e._variant){case"SimplePattern":return he(e.value);case"CompoundPattern":return Se(e.value)}}function _e(e){let t,r;if(t=n.is(e,b)?{}:void 0,void 0!==t&&(r={_variant:"any",__as_preserve__(){return he(this)}}),void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,m)?{}:void 0,void 0!==t){let t;t=$e(e[0]),void 0!==t&&(r={_variant:"atom",atomKind:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,y)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"embedded",interface:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,g)?{}:void 0,void 0!==t){let t;t=e[0],void 0!==t&&(r={_variant:"lit",value:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,A)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"seqof",pattern:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,E)?{}:void 0,void 0!==t){let t;t=_e(e[0]),void 0!==t&&(r={_variant:"setof",pattern:t,__as_preserve__(){return he(this)}})}}if(void 0===r){if(n.Record.isRecord(e)){let t;if(t=n.is(e.label,h)?{}:void 0,void 0!==t){let t;if(t=_e(e[0]),void 0!==t){let n;n=_e(e[1]),void 0!==n&&(r={_variant:"dictof",key:t,value:n,__as_preserve__(){return he(this)}})}}}if(void 0===r){let t;t=Ie(e),void 0!==t&&(r={_variant:"Ref",value:t,__as_preserve__(){return he(this)}})}}}}}}}return r}function he(e){switch(e._variant){case"any":return b;case"atom":return n.Record(m,[Pe(e.atomKind)]);case"embedded":return n.Record(y,[he(e.interface)]);case"lit":return n.Record(g,[e.value]);case"seqof":return n.Record(A,[he(e.pattern)]);case"setof":return n.Record(E,[he(e.pattern)]);case"dictof":return n.Record(h,[he(e.key),he(e.value)]);case"Ref":return Be(e.value)}}function ye(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,P)?{}:void 0,void 0!==r){let r;if(r=Re(e[0]),void 0!==r){let n;n=Re(e[1]),void 0!==n&&(t={_variant:"rec",label:r,fields:n,__as_preserve__(){return Se(this)}})}}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,R)?{}:void 0,void 0!==r){let r;if(r=void 0,n.isSequence(e[0])){r=[];for(const t of e[0]){let e;if(e=Re(t),void 0===e){r=void 0;break}r.push(e)}}void 0!==r&&(t={_variant:"tuple",patterns:r,__as_preserve__(){return Se(this)}})}}if(void 0===t){if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,D)?{}:void 0,void 0!==r){let r;if(r=void 0,n.isSequence(e[0])){r=[];for(const t of e[0]){let e;if(e=Re(t),void 0===e){r=void 0;break}r.push(e)}}if(void 0!==r){let n;n=Ae(e[1]),void 0!==n&&(t={_variant:"tuplePrefix",fixed:r,variable:n,__as_preserve__(){return Se(this)}})}}}if(void 0===t&&n.Record.isRecord(e)){let r;if(r=n.is(e.label,_)?{}:void 0,void 0!==r){let r;r=ge(e[0]),void 0!==r&&(t={_variant:"dict",entries:r,__as_preserve__(){return Se(this)}})}}}}return t}function Se(e){switch(e._variant){case"rec":return n.Record(P,[De(e.label),De(e.fields)]);case"tuple":return n.Record(R,[e.patterns.map((e=>De(e)))]);case"tuplePrefix":return n.Record(D,[e.fixed.map((e=>De(e))),Ee(e.variable)]);case"dict":return n.Record(_,[we(e.entries)])}}function ge(e){let t,r;if(t=void 0,n.Dictionary.isDictionary(e)){t=new n.KeyedDictionary;for(const[r,n]of e){let e;if(e=r,void 0!==e){let r;if(r=Ae(n),void 0!==r){t.set(e,r);continue}}t=void 0;break}}return void 0!==t&&(r=t),r}function we(e){return new n.Dictionary(n.Array.from(e.entries()).map((([e,t])=>[e,Ee(t)])))}function $e(e){let t,r;if(t=n.is(e,o)?{}:void 0,void 0!==t&&(r={_variant:"Boolean",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,c)?{}:void 0,void 0!==t&&(r={_variant:"Float",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,s)?{}:void 0,void 0!==t&&(r={_variant:"Double",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,d)?{}:void 0,void 0!==t&&(r={_variant:"SignedInteger",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,u)?{}:void 0,void 0!==t&&(r={_variant:"String",__as_preserve__(){return Pe(this)}}),void 0===r){let t;if(t=n.is(e,a)?{}:void 0,void 0!==t&&(r={_variant:"ByteString",__as_preserve__(){return Pe(this)}}),void 0===r){let t;t=n.is(e,l)?{}:void 0,void 0!==t&&(r={_variant:"Symbol",__as_preserve__(){return Pe(this)}})}}}}}}return r}function Pe(e){switch(e._variant){case"Boolean":return o;case"Float":return c;case"Double":return s;case"SignedInteger":return d;case"String":return u;case"ByteString":return a;case"Symbol":return l}}function Ne(e){let t;if(n.isSequence(e)&&2===e.length){let r;if(r="string"==typeof e[0]?e[0]:void 0,void 0!==r){let n;n=pe(e[1]),void 0!==n&&(t={variantLabel:r,pattern:n,__as_preserve__(){return ke(this)}})}}return t}function ke(e){return[e.variantLabel,ve(e.pattern)]}function Ae(e){let t,r;if(t=Te(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return Ee(this)}}),void 0===r){let t;t=_e(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return Ee(this)}})}return r}function Ee(e){switch(e._variant){case"named":return xe(e.value);case"anonymous":return he(e.value)}}function Re(e){let t,r;if(t=Te(e),void 0!==t&&(r={_variant:"named",value:t,__as_preserve__(){return De(this)}}),void 0===r){let t;t=pe(e),void 0!==t&&(r={_variant:"anonymous",value:t,__as_preserve__(){return De(this)}})}return r}function De(e){switch(e._variant){case"named":return xe(e.value);case"anonymous":return ve(e.value)}}function Te(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,w)?{}:void 0,void 0!==r){let r;if(r="symbol"==typeof e[0]?e[0]:void 0,void 0!==r){let n;n=_e(e[1]),void 0!==n&&(t={name:r,pattern:n,__as_preserve__(){return xe(this)}})}}}return t}function xe(e){return n.Record(w,[e.name,he(e.pattern)])}function Ie(e){let t;if(n.Record.isRecord(e)){let r;if(r=n.is(e.label,N)?{}:void 0,void 0!==r){let r;if(r=Ce(e[0]),void 0!==r){let n;n="symbol"==typeof e[1]?e[1]:void 0,void 0!==n&&(t={module:r,name:n,__as_preserve__(){return Be(this)}})}}}return t}function Be(e){return n.Record(N,[Oe(e.module),e.name])}function Ce(e){let t,r;if(t=void 0,n.isSequence(e)){t=[];for(const r of e){let e;if(e="symbol"==typeof r?r:void 0,void 0===e){t=void 0;break}t.push(e)}}return void 0!==t&&(r=t),r}function Oe(e){return e.map((e=>e))}O.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Bundle")}},M.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Modules")}},L.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Schema")}},V.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Version")}},function(e){function t(){return{_variant:"false",__as_preserve__(){return ue(this)}}}function r(e){return{_variant:"Ref",value:e,__as_preserve__(){return ue(this)}}}e.$false=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("EmbeddedTypeName"),variant:n.Symbol.for("false")}},e.Ref=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("EmbeddedTypeName"),variant:n.Symbol.for("Ref")}}}(F||(F={})),Y.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definitions")}},function(e){function t({pattern0:e,pattern1:t,patternN:r}){return{_variant:"or",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return me(this)}}}function r({pattern0:e,pattern1:t,patternN:r}){return{_variant:"and",pattern0:e,pattern1:t,patternN:r,__as_preserve__(){return me(this)}}}function i(e){return{_variant:"Pattern",value:e,__as_preserve__(){return me(this)}}}e.or=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("or")}},e.and=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("and")}},e.Pattern=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Definition"),variant:n.Symbol.for("Pattern")}}}(K||(K={})),function(e){function t(e){return{_variant:"SimplePattern",value:e,__as_preserve__(){return ve(this)}}}function r(e){return{_variant:"CompoundPattern",value:e,__as_preserve__(){return ve(this)}}}e.SimplePattern=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Pattern"),variant:n.Symbol.for("SimplePattern")}},e.CompoundPattern=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Pattern"),variant:n.Symbol.for("CompoundPattern")}}}(q||(q={})),function(e){function t(){return{_variant:"any",__as_preserve__(){return he(this)}}}function r(e){return{_variant:"atom",atomKind:e,__as_preserve__(){return he(this)}}}function i(e){return{_variant:"embedded",interface:e,__as_preserve__(){return he(this)}}}function o(e){return{_variant:"lit",value:e,__as_preserve__(){return he(this)}}}function a(e){return{_variant:"seqof",pattern:e,__as_preserve__(){return he(this)}}}function s(e){return{_variant:"setof",pattern:e,__as_preserve__(){return he(this)}}}function c({key:e,value:t}){return{_variant:"dictof",key:e,value:t,__as_preserve__(){return he(this)}}}function d(e){return{_variant:"Ref",value:e,__as_preserve__(){return he(this)}}}e.any=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("any")}},e.atom=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("atom")}},e.embedded=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("embedded")}},e.lit=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("lit")}},e.seqof=a,a.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("seqof")}},e.setof=s,s.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("setof")}},e.dictof=c,c.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("dictof")}},e.Ref=d,d.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("SimplePattern"),variant:n.Symbol.for("Ref")}}}(j||(j={})),function(e){function t({label:e,fields:t}){return{_variant:"rec",label:e,fields:t,__as_preserve__(){return Se(this)}}}function r(e){return{_variant:"tuple",patterns:e,__as_preserve__(){return Se(this)}}}function i({fixed:e,variable:t}){return{_variant:"tuplePrefix",fixed:e,variable:t,__as_preserve__(){return Se(this)}}}function o(e){return{_variant:"dict",entries:e,__as_preserve__(){return Se(this)}}}e.rec=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("rec")}},e.tuple=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("tuple")}},e.tuplePrefix=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("tuplePrefix")}},e.dict=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("CompoundPattern"),variant:n.Symbol.for("dict")}}}(z||(z={})),Z.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("DictionaryEntries")}},function(e){function t(){return{_variant:"Boolean",__as_preserve__(){return Pe(this)}}}function r(){return{_variant:"Float",__as_preserve__(){return Pe(this)}}}function i(){return{_variant:"Double",__as_preserve__(){return Pe(this)}}}function o(){return{_variant:"SignedInteger",__as_preserve__(){return Pe(this)}}}function a(){return{_variant:"String",__as_preserve__(){return Pe(this)}}}function s(){return{_variant:"ByteString",__as_preserve__(){return Pe(this)}}}function c(){return{_variant:"Symbol",__as_preserve__(){return Pe(this)}}}e.Boolean=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Boolean")}},e.Float=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Float")}},e.Double=i,i.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Double")}},e.SignedInteger=o,o.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("SignedInteger")}},e.String=a,a.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("String")}},e.ByteString=s,s.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("ByteString")}},e.Symbol=c,c.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("AtomKind"),variant:n.Symbol.for("Symbol")}}}(J||(J={})),G.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedAlternative")}},function(e){function t(e){return{_variant:"named",value:e,__as_preserve__(){return Ee(this)}}}function r(e){return{_variant:"anonymous",value:e,__as_preserve__(){return Ee(this)}}}e.named=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedSimplePattern"),variant:n.Symbol.for("named")}},e.anonymous=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedSimplePattern"),variant:n.Symbol.for("anonymous")}}}(U||(U={})),function(e){function t(e){return{_variant:"named",value:e,__as_preserve__(){return De(this)}}}function r(e){return{_variant:"anonymous",value:e,__as_preserve__(){return De(this)}}}e.named=t,t.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedPattern"),variant:n.Symbol.for("named")}},e.anonymous=r,r.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("NamedPattern"),variant:n.Symbol.for("anonymous")}}}(H||(H={})),Q.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Binding")}},W.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("Ref")}},X.schema=function(){return{schema:B(),imports:C,definitionName:n.Symbol.for("ModulePath")}},O.__from_preserve__=ee,M.__from_preserve__=re,L.__from_preserve__=ie,V.__from_preserve__=se,function(e){e.__from_preserve__=de}(F||(F={})),Y.__from_preserve__=le,function(e){e.__from_preserve__=be}(K||(K={})),function(e){e.__from_preserve__=pe}(q||(q={})),function(e){e.__from_preserve__=_e}(j||(j={})),function(e){e.__from_preserve__=ye}(z||(z={})),Z.__from_preserve__=ge,function(e){e.__from_preserve__=$e}(J||(J={})),G.__from_preserve__=Ne,function(e){e.__from_preserve__=Ae}(U||(U={})),function(e){e.__from_preserve__=Re}(H||(H={})),Q.__from_preserve__=Te,W.__from_preserve__=Ie,X.__from_preserve__=Ce;const Me=new Set(["abstract","await","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete","do","double","else","enum","export","extends","false","final","finally","float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield"]);function Le(e){return e.match(/^(.*\.)?([^.]+)$/)[2]}function Ve(e){return/^[a-zA-Z][a-zA-Z_0-9]*$/.test(e)}function Fe(e){return e.split(".").every(Ve)}function Ke(e){return/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(e)&&!function(e){return Me.has(e)}(e)}function qe(e,t){return je(e.replace("$","$$"),t)}function je(e,t){if(Ke(e))return e;if(Ke("$"+e))return"$"+e;if(void 0!==t)return t();throw new Error(`Internal error: jsToken needs to be completed (${e})`)}const ze=Symbol.for("&"),Je=Symbol.for("."),Ue=Symbol.for("..."),He=Symbol.for("="),Ye=Symbol.for("include"),Ze=Symbol.for("/");function Ge(e,r){for(const n of e)if(t.is(n.schemaModulePath,r))return n;return null}function Qe(e){return"_i_"+e.schemaModulePath.map((e=>e.description)).join("$")}function We(e){return e.map((e=>e.description)).join(".")}function Xe(e){return[...e.module,e.name].map((e=>e.description)).join(".")}function et(e){return"named"===e._variant?q.SimplePattern(e.value.pattern):e.value}function tt(e){return"named"===e._variant?e:H.anonymous(q.SimplePattern(e.value))}function rt(e){return"named"===e._variant?e.value.name.description:void 0}function nt(e){return H.anonymous(q.SimplePattern(e))}function it(e){return"string"==typeof e?e:"symbol"==typeof e?e.description:"number"==typeof e||"boolean"==typeof e?""+e:void 0}var ot,at=Object.freeze({__proto__:null,$1:i,$Boolean:o,$ByteString:a,$Double:s,$Float:c,$SignedInteger:d,$String:u,$Symbol:l,$and:f,$any:b,$atom:m,$bundle:p,$definitions:v,$dict:_,$dictof:h,$embedded:y,$embeddedType:S,$lit:g,$named:w,$or:$,$rec:P,$ref:N,$schema:k,$seqof:A,$setof:E,$tuple:R,$tuplePrefix:D,$version:T,ANDSYM:ze,get AtomKind(){return J},Binding:Q,Bundle:O,get CompoundPattern(){return z},DOT:Je,DOTDOTDOT:Ue,get Definition(){return K},Definitions:Y,DictionaryEntries:Z,EQUALS:He,get EmbeddedTypeName(){return F},INCLUDE:Ye,ModulePath:X,Modules:M,NamedAlternative:G,get NamedPattern(){return H},get NamedSimplePattern(){return U},ORSYM:Ze,get Pattern(){return q},Ref:W,Schema:L,get SimplePattern(){return j},Version:V,__lit6:x,_imports:C,_schema:B,anonymousSimplePattern:nt,asAtomKind:function(e){let t=$e(e);if(void 0===t)throw new TypeError(`Invalid AtomKind: ${n.stringify(e)}`);return t},asBinding:function(e){let t=Te(e);if(void 0===t)throw new TypeError(`Invalid Binding: ${n.stringify(e)}`);return t},asBundle:function(e){let t=ee(e);if(void 0===t)throw new TypeError(`Invalid Bundle: ${n.stringify(e)}`);return t},asCompoundPattern:function(e){let t=ye(e);if(void 0===t)throw new TypeError(`Invalid CompoundPattern: ${n.stringify(e)}`);return t},asDefinition:function(e){let t=be(e);if(void 0===t)throw new TypeError(`Invalid Definition: ${n.stringify(e)}`);return t},asDefinitions:function(e){let t=le(e);if(void 0===t)throw new TypeError(`Invalid Definitions: ${n.stringify(e)}`);return t},asDictionaryEntries:function(e){let t=ge(e);if(void 0===t)throw new TypeError(`Invalid DictionaryEntries: ${n.stringify(e)}`);return t},asEmbeddedTypeName:function(e){let t=de(e);if(void 0===t)throw new TypeError(`Invalid EmbeddedTypeName: ${n.stringify(e)}`);return t},asModulePath:function(e){let t=Ce(e);if(void 0===t)throw new TypeError(`Invalid ModulePath: ${n.stringify(e)}`);return t},asModules:function(e){let t=re(e);if(void 0===t)throw new TypeError(`Invalid Modules: ${n.stringify(e)}`);return t},asNamedAlternative:function(e){let t=Ne(e);if(void 0===t)throw new TypeError(`Invalid NamedAlternative: ${n.stringify(e)}`);return t},asNamedPattern:function(e){let t=Re(e);if(void 0===t)throw new TypeError(`Invalid NamedPattern: ${n.stringify(e)}`);return t},asNamedSimplePattern:function(e){let t=Ae(e);if(void 0===t)throw new TypeError(`Invalid NamedSimplePattern: ${n.stringify(e)}`);return t},asPattern:function(e){let t=pe(e);if(void 0===t)throw new TypeError(`Invalid Pattern: ${n.stringify(e)}`);return t},asRef:function(e){let t=Ie(e);if(void 0===t)throw new TypeError(`Invalid Ref: ${n.stringify(e)}`);return t},asSchema:function(e){let t=ie(e);if(void 0===t)throw new TypeError(`Invalid Schema: ${n.stringify(e)}`);return t},asSimplePattern:function(e){let t=_e(e);if(void 0===t)throw new TypeError(`Invalid SimplePattern: ${n.stringify(e)}`);return t},asVersion:ae,envLookup:Ge,formatModulePath:We,formatRef:Xe,fromAtomKind:Pe,fromBinding:xe,fromBundle:te,fromCompoundPattern:Se,fromDefinition:me,fromDefinitions:fe,fromDictionaryEntries:we,fromEmbeddedTypeName:ue,fromModulePath:Oe,fromModules:ne,fromNamedAlternative:ke,fromNamedPattern:De,fromNamedSimplePattern:Ee,fromPattern:ve,fromRef:Be,fromSchema:oe,fromSimplePattern:he,fromVersion:ce,isValidJsId:Ke,isValidQid:Fe,isValidToken:Ve,jsId:qe,jsToken:je,modsymFor:Qe,nameFor:rt,namelike:it,promoteNamedSimplePattern:tt,qidLast:Le,toAtomKind:$e,toBinding:Te,toBundle:ee,toCompoundPattern:ye,toDefinition:be,toDefinitions:le,toDictionaryEntries:ge,toEmbeddedTypeName:de,toModulePath:Ce,toModules:re,toNamedAlternative:Ne,toNamedPattern:Re,toNamedSimplePattern:Ae,toPattern:pe,toRef:Ie,toSchema:ie,toSimplePattern:_e,toVersion:se,unnamePattern:et,unnameSimplePattern:function(e){return"named"===e._variant?e.value.pattern:e.value}});function st(e){const t=new ct;return e.definitions.forEach(t.checkDefinition.bind(t)),t.problems.length>0?{ok:!1,problems:t.problems}:{ok:!0,schema:e}}!function(e){e[e.AVAILABLE=0]="AVAILABLE",e[e.NOT_AVAILABLE=1]="NOT_AVAILABLE"}(ot||(ot={}));class ct{constructor(){this.problems=[]}recordProblem(e,t){this.problems.push(`${t} in ${e}`)}checkBinding(e,t,r){const n=t.description;e.has(n)&&this.recordProblem(r,`duplicate binding named ${JSON.stringify(n)}`),Ve(n)||this.recordProblem(r,`invalid binding name ${JSON.stringify(n)}`),e.add(n)}checkDefinition(e,t){switch(e._variant){case"or":{const r=new Set;[e.pattern0,e.pattern1,...e.patternN].forEach((({variantLabel:e,pattern:n})=>{const i=`variant ${e} of ${t.description}`;r.has(e)&&this.recordProblem(i,"duplicate variant label"),Ve(e)||this.recordProblem(i,"invalid variant label"),r.add(e),this.checkPattern(new Set,n,i,ot.AVAILABLE)}));break}case"and":{const r=[e.pattern0,e.pattern1,...e.patternN],n=new Set;r.forEach((e=>this.checkNamedPattern(n,e,t.description)));break}case"Pattern":this.checkPattern(new Set,e.value,t.description,ot.AVAILABLE)}}checkNamedPattern(e,t,r){switch(t._variant){case"named":this.checkBinding(e,t.value.name,r),this.checkPattern(e,q.SimplePattern(t.value.pattern),`${JSON.stringify(t.value.name.description)} of ${r}`,ot.AVAILABLE);break;case"anonymous":this.checkPattern(e,t.value,r,ot.NOT_AVAILABLE)}}checkPattern(e,r,n,i){switch(r._variant){case"SimplePattern":"lit"!==r.value._variant&&i===ot.NOT_AVAILABLE&&this.recordProblem(n,"cannot recover serialization of non-literal pattern"),"Ref"!==r.value._variant||Ve(r.value.value.name.description)&&r.value.value.module.every((e=>Ve(e.description)))||this.recordProblem(n,"invalid reference name");break;case"CompoundPattern":(r=>{switch(r._variant){case"rec":this.checkNamedPattern(e,r.label,`label of ${n}`),this.checkNamedPattern(e,r.fields,`fields of ${n}`);break;case"tuple":r.patterns.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`)));break;case"tuplePrefix":r.fixed.forEach(((t,r)=>this.checkNamedPattern(e,t,`item ${r} of ${n}`))),this.checkNamedPattern(e,tt(r.variable),`tail of ${n}`);break;case"dict":r.entries.forEach(((r,i)=>this.checkNamedPattern(e,tt(r),`entry ${t.stringify(i)} in dictionary in ${n}`)))}})(r.value)}}}class dt extends Error{constructor(e,t){super(e),this.pos=t}}const ut=new WeakMap;function lt(e,t){return null===t&&console.error("Internal error in Schema reader: null source position for",e),null!==t&&ut.set(e,t),e}function ft(e){var t;return null!==(t=ut.get(e))&&void 0!==t?t:null}function bt(e,r){const n=[];let i=[];function o(){i.length>0&&(n.push(i),i=[])}for(const n of e)t.is(n,r)?o():i.push(n);return o(),n}function mt(e){var r;throw new dt(t.preserves`Invalid Schema clause: ${e}`,t.position(null!==(r=e[0])&&void 0!==r&&r))}function pt(e,r,n){throw new dt(`Invalid pattern in ${e}: ${t.stringify(r)}`,n)}function vt(e,r){return new t.Reader(e,{name:null==r?void 0:r.name,includeAnnotations:!0,embeddedDecode:t.genericEmbeddedTypeDecode}).readToEnd()}function _t(e,r){let n,i=F.$false(),o=new t.KeyedDictionary;if(function e(a){const s=bt(t.peel(a),Je);for(const a of s)if(a.length>=2&&t.is(a[1],He)){const e=t.position(a[0]),r=t.peel(a[0]);if("symbol"!=typeof r&&mt(a),!Ve(r.description))throw new dt(t.preserves`Invalid definition name: ${r}`,e);if(o.has(r))throw new dt(t.preserves`Duplicate definition: ${a}`,e);o.set(r,yt(r,e,a.slice(2)))}else if(2===a.length&&t.is(a[0],T))n=ae(t.peel(a[1]));else if(2===a.length&&t.is(a[0],S)){const e=t.position(a[1]),r=t.peel(a[1]);!1===r?i=F.$false():"symbol"==typeof r&&Fe(r.description)?i=F.Ref($t(r.description,e)):pt("embedded type name specification",r,e)}else if(2===a.length&&t.is(a[0],Ye)){const n=t.position(a[1]),i=t.peel(a[1]);if("string"!=typeof i)throw new dt(t.preserves`Invalid include: ${a}`,n);if(void 0===r.readInclude)throw new dt(t.preserves`Cannot include files in schema`,n);e(vt(r.readInclude(i),r))}else mt(a)}(e),void 0===n)throw new dt("Schema: missing version declaration.",null);return L({version:V(),embeddedType:i,definitions:o})}function ht(e){throw new dt("Named patterns must be Simple patterns",e)}function yt(e,r,n){function i(n){const i=wt(n)||wt(n[0]),o=gt(e,n);if(!1!==i)return G({variantLabel:i.description,pattern:o});if("CompoundPattern"===o._variant&&"rec"===o.value._variant&&"anonymous"===o.value.label._variant&&"SimplePattern"===o.value.label.value._variant&&"lit"===o.value.label.value.value._variant&&"symbol"==typeof o.value.label.value.value.value&&Ve(o.value.label.value.value.value.description))return G({variantLabel:o.value.label.value.value.value.description,pattern:o});if("SimplePattern"===o._variant&&"Ref"===o.value._variant&&Fe(o.value.value.name.description))return G({variantLabel:Le(o.value.value.name.description),pattern:o});if("SimplePattern"===o._variant&&"lit"===o.value._variant){const e=it(o.value.value);if(void 0!==e)return G({variantLabel:e,pattern:o})}throw new dt(t.preserves`Name missing for alternative: ${n}`,r)}function o(r){const n=wt(r)||wt(r[0]),i=gt(e,r);return!1!==n?("SimplePattern"!==i._variant&&ht(t.position(r[0])),H.named(Q({name:n,pattern:i.value}))):H.anonymous(i)}const a=bt(n,ze),s=bt(n,Ze);if(0===a.length||0===s.length)throw new dt(t.preserves`Invalid Schema clause: ${n}`,r);if(a.length>1&&s.length>1)throw new dt(t.preserves`Mixed "or" and "and" clause: ${n}`,r);return a.length>1?K.and({pattern0:o(a[0]),pattern1:o(a[1]),patternN:a.slice(2).map(o)}):s.length>1?K.or({pattern0:i(s[0]),pattern1:i(s[1]),patternN:s.slice(2).map(i)}):K.Pattern(gt(e,s[0]))}function St(e,r){return t.annotate(e,...t.annotations(r))}function gt(e,r){function n(r,n,o){const a=t.position(r),s=t.peel(r);function c(){pt(t.stringify(e),s,a)}if("symbol"!=typeof s){if(t.Record.isRecord(s)){const e=s.label;return t.Record.isRecord(e)?(0!==e.length&&c(),e.label===g?(1!==s.length&&c(),n(j.lit(s[0]))):o()):o()}if(Array.isArray(s)&&2===s.length&&t.is(s[1],Ue))return n(j.seqof(i(s[0])));if(Set.isSet(s)){1!==s.size&&c();const[e]=s.values();return n(j.setof(i(e)))}if(t.Dictionary.isDictionary(s)&&2===s.size&&s.has(Ue)){const e=s.clone();e.delete(Ue);const[[t,r]]=e.entries();return n(j.dictof({key:i(t),value:i(r)}))}return t.isCompound(s)?o():t.isEmbedded(s)?n(j.embedded(i(s.embeddedValue.generic))):n(j.lit(t.strip(s)))}{const e=s.description;switch(e){case"any":return n(j.any());case"bool":return n(j.atom(J.Boolean()));case"float":return n(j.atom(J.Float()));case"double":return n(j.atom(J.Double()));case"int":return n(j.atom(J.SignedInteger()));case"string":return n(j.atom(J.String()));case"bytes":return n(j.atom(J.ByteString()));case"symbol":return n(j.atom(J.Symbol()));default:if("="===e[0])return n(j.lit(Symbol.for(e.slice(1))));if(Fe(e))return n(j.Ref($t(e,a)));c()}}}const i=e=>n(e,(e=>e),(()=>{throw new dt("Compound patterns not accepted here",t.position(e))}));function o(e,r,i,o){return a=>{let s=wt(a);return!1===s&&void 0!==o&&"symbol"==typeof o&&Ve(o.description)&&(s=o),!1===s?r(i(a)):e(Q({name:s,pattern:n(a,(e=>e),(()=>ht(t.position(a))))}))}}const a=o(H.named,H.anonymous,(t=>gt(e,[t]))),s=o(U.named,U.anonymous,i),c=t.peel(r);return 1!==c.length&&pt(t.stringify(e),c,c.length>0?t.position(c[0]):t.position(c)),n(c[0],q.SimplePattern,(()=>q.CompoundPattern(function(r){const n=t.position(r),c=t.peel(r);function d(){pt(t.stringify(e),c,n)}if(t.Record.isRecord(c)){const n=c.label;if(!t.Record.isRecord(n))return z.rec({label:H.anonymous(q.SimplePattern(j.lit(n))),fields:H.anonymous(gt(e,[St([...c],r)]))});if(0!==n.length&&d(),n.label===P)return 2!==c.length&&d(),z.rec({label:a(c[0]),fields:a(c[1])});d()}else{if(Array.isArray(c)&&c.length>2&&t.is(c[c.length-1],Ue)){const e=c[c.length-2],t=St([e,Ue],e);return z.tuplePrefix({fixed:c.slice(0,c.length-2).map(a),variable:s(t)})}if(Array.isArray(c))return z.tuple(c.map(a));if(t.Dictionary.isDictionary(c)&&!c.has(Ue))return z.dict(c.mapEntries((([e,r])=>[t.strip(e),o(U.named,U.anonymous,i,t.strip(e))(r)])));d()}}(c[0]))))}function wt(e){if(!t.Annotated.isAnnotated(e))return!1;for(const r of e.annotations){const e=t.peel(r);if("symbol"==typeof e)return Ve(e.description)&&e}return!1}function $t(e,t){const r=e.split(".");return lt(W({module:r.slice(0,r.length-1).map(Symbol.for),name:Symbol.for(r[r.length-1])}),t)}const Pt=80;class Nt{constructor(){this.width=Pt,this.indentDelta=" ",this.currentIndent="\n",this.buffer=[]}get indentSize(){return this.indentDelta.length}set indentSize(e){this.indentDelta=new Array(e+1).join(" ")}write(e){"string"==typeof e?this.buffer.push(e):e.writeOn(this)}newline(){this.write(this.currentIndent)}toString(){return this.buffer.join("")}withIndent(e){const t=this.currentIndent;try{this.currentIndent=this.currentIndent+this.indentDelta,e()}finally{this.currentIndent=t}}clone(){const e=Object.assign(new Nt,this);return e.buffer=[],e}}class kt{constructor(e){if(e.some((e=>void 0===e)))throw new Error("aiee");this.items=e}get separator(){return""}get terminator(){return""}writeOn(e){let t=!1;this.items.forEach((r=>{t?e.write(this.separator):t=!0,e.write(r)})),e.write(this.terminator)}}class At extends kt{get separator(){return", "}}class Et extends At{writeHorizontally(e){e.write(this.open),super.writeOn(e),e.write(this.close)}writeVertically(e){e.write(this.open),this.items.length>0&&(e.withIndent((()=>{this.items.forEach(((t,r)=>{e.newline(),e.write(t);const n=r===this.items.length-1?this.terminator:this.separator;e.write(n.trimRight())}))})),e.newline()),e.write(this.close)}writeOn(e){const t=e.clone();this.writeHorizontally(t);const r=t.toString();r.length<=e.width?e.write(r):this.writeVertically(e)}}class Rt extends Et{get open(){return"("}get close(){return")"}}class Dt extends Rt{constructor(e,t){super(t),this.operator=e}get separator(){return this.operator}}class Tt extends Et{get open(){return"["}get close(){return"]"}}class xt extends Et{get open(){return"<"}get close(){return">"}}class It extends Et{get open(){return"{"}get close(){return"}"}}class Bt extends It{get separator(){return"; "}get terminator(){return";"}}const Ct=(...e)=>new kt(e),Ot=(...e)=>new Rt(e),Mt=(e,t,...r)=>0===r.length?e:new Dt(t,r),Lt=(...e)=>new Tt(e),Vt=(...e)=>new xt(e),Ft=(...e)=>new It(e),Kt=(...e)=>1===e.length&&e[0]instanceof Bt?e[0]:new Bt(e),qt=(e,t)=>Ct(JSON.stringify(e),": ",t);var jt;!function(e){e.union=e=>({kind:"union",variants:e}),e.unit=()=>({kind:"unit"}),e.ref=(e,t)=>({kind:"ref",typeName:e,ref:t}),e.array=e=>({kind:"array",type:e}),e.set=e=>({kind:"set",type:e}),e.dictionary=(e,t)=>({kind:"dictionary",key:e,value:t}),e.record=e=>({kind:"record",fields:e})}(jt||(jt={}));const zt=jt.ref("_.Value",null);var Jt=Object.freeze({__proto__:null,ANY_TYPE:zt,get Type(){return jt}});function Ut(e){return qt("_variant",JSON.stringify(e))}function Ht(e,t,r){switch(r.kind){case"unit":return[];case"ref":case"set":case"dictionary":case"array":return[qt("value",Yt(e,r))];case"record":return Array.from(r.fields).map((([t,r])=>qt(t,Yt(e,r))));default:throw new Error("Unreachable")}}function Yt(e,t){switch(t.kind){case"union":return Mt("never"," | ",...Array.from(t.variants).flatMap((t=>function(e,t,[r,n]){let i=Ht(e,0,n);return Ft(Ut(r),...i)}(e,0,t))));case"unit":case"record":return Ft(...Ht(e,0,t));case"ref":return null===t.ref&&"_embedded"===t.typeName?t.typeName:Ct(t.typeName,e.genericArgsFor(t));case"set":return Ct("_.KeyedSet",Vt(Yt(e,t.type),"_embedded"));case"dictionary":return Ct("_.KeyedDictionary",Vt(Yt(e,t.key),Yt(e,t.value),"_embedded"));case"array":return Ct("Array",Vt(Yt(e,t.type)));default:throw new Error("Unreachable")}}function Zt(e,t){switch(t._variant){case"or":return jt.union(new Map([t.pattern0,t.pattern1,...t.patternN].map((t=>[t.variantLabel,Qt(e,t.pattern)]))));case"and":return Gt(e,[t.pattern0,t.pattern1,...t.patternN]);case"Pattern":return Qt(e,t.value)}}function Gt(e,t){const r=new Map;return t.forEach((t=>Xt(r,e,t))),r.size>0?jt.record(r):jt.unit()}function Qt(e,t){return"SimplePattern"===t._variant?Wt(e,t.value):Gt(e,[H.anonymous(t)])}function Wt(e,t){switch(t._variant){case"any":return zt;case"atom":switch(t.atomKind._variant){case"Boolean":return jt.ref("boolean",null);case"Float":case"Double":case"SignedInteger":return jt.ref("number",null);case"String":return jt.ref("string",null);case"ByteString":return jt.ref("_.Bytes",null);case"Symbol":return jt.ref("symbol",null)}case"embedded":return jt.ref("_embedded",null);case"lit":return jt.unit();case"seqof":return jt.array(Wt(e,t.pattern));case"setof":return jt.set(Wt(e,t.pattern));case"dictof":return jt.dictionary(Wt(e,t.key),Wt(e,t.value));case"Ref":return e(t.value);default:throw new Error("Unreachable")}}function Xt(e,t,r){if("named"===r._variant){const n=Wt(t,r.value.pattern);"unit"!==n.kind&&e.set(r.value.name.description,n)}else"CompoundPattern"===r.value._variant&&function(e,t,r){switch(r._variant){case"rec":Xt(e,t,r.label),Xt(e,t,r.fields);break;case"tuple":r.patterns.forEach((r=>Xt(e,t,r)));break;case"tuplePrefix":r.fixed.forEach((r=>Xt(e,t,r))),Xt(e,t,tt(r.variable));break;case"dict":r.entries.forEach(((r,n)=>Xt(e,t,tt(r))));break;default:throw new Error("Unreachable")}}(e,t,r.value.value)}var er=Object.freeze({__proto__:null,simpleType:Wt,typeFor:Qt,typeForDefinition:Zt,typeForIntersection:Gt});class tr{constructor(e,r,n,i){switch(this.literals=new t.Dictionary,this.preamble=[],this.typedefs=[],this.functiondefs=[],this.imports=new t.KeyedSet,this.env=e,this.modulePath=r,this.schema=n,this.options=i,n.embeddedType._variant){case"false":this.embeddedType="_.GenericEmbedded";break;case"Ref":{const e=this.resolver()(n.embeddedType.value);this.embeddedType=e.typeName;break}}}literal(e){let r=this.literals.get(e);return void 0===r&&(r=qe("$"+t.stringify(e),(()=>"__lit"+this.literals.size)),this.literals.set(e,r)),r}derefPattern(e,t=0){if(t>128)throw new Error("Recursion limit exceeded");return"Pattern"===e._variant&&"SimplePattern"===e.value._variant&&"Ref"===e.value.value._variant?this.lookup(e.value.value.value,((e,r)=>this.derefPattern(e,t+1)),((r,n,i,o,a,s)=>this.derefPattern(null!=a?a:e,t+1))):e}definePreamble(e){this.preamble.push(e)}defineType(e){this.typedefs.push(e)}defineFunctions(e,t){this.functiondefs.push(...t(new rr(this,e)))}resolver(e){return t=>this.lookup(t,((e,r)=>jt.ref(t.name.description,t)),((e,r,n,i,o,a)=>(this.imports.add([e,r,n,i]),jt.ref(`${r}${i}.${t.name.description}`,t))),e)}lookupType(e,t){const r=this.lookup(e,((e,t)=>t),((e,t,r,n,i,o)=>o),t);return r?r():null}lookup(e,t,r,n){const i=e.module.length?e.module:null!=n?n:this.modulePath,o=Ge(this.env,i);if(null!==o){const n=null===o.typescriptModuleExpr?"":"."+o.typescriptModuleExpr;if(null===o.schema)return r(i,Qe(o),o.typescriptModulePath,n,null,null);{const a=o.schema.definitions.get(e.name);if(void 0!==a){let s=()=>Zt(this.resolver(i),a);return e.module.length?r(i,Qe(o),o.typescriptModulePath,n,a,s):t(a,s)}}}throw new dt(`Undefined reference: ${Xe(e)}`,ft(e))}genericParameters(){return Vt(Ct("_embedded = ",this.embeddedType))}genericParametersFor(e){return this.hasEmbedded(e)?this.genericParameters():""}genericArgs(){return"<_embedded>"}genericArgsFor(e){return this.hasEmbedded(e)?this.genericArgs():""}hasEmbedded(e){const t=this,r=new nr(this.modulePath);return function e(n){switch(n.kind){case"union":for(const t of n.variants.values())if(e(t))return!0;return!1;case"unit":return!1;case"array":return e(n.type);case"set":case"dictionary":return!0;case"ref":if(null!==n.ref)return r.cycleCheck(n.ref,(e=>t.lookupType(e,r.modulePath)),(t=>!!t&&e(t)),(()=>!1));switch(n.typeName){case"_embedded":case"_.Value":return!0;default:return!1}case"record":for(const t of n.fields.values())if(e(t))return!0;return!1}}(e)}withAsPreserveMixinType(e,t){return"unit"===t.kind||"record"===t.kind||"union"===t.kind?Mt("any"," & ",Ct(e,this.genericArgsFor(t)),Ft(Ct("__as_preserve__",this.hasEmbedded(t)?"":this.genericParameters(),"()",": _.Value",this.genericArgs()))):Ct(e,this.genericArgsFor(t))}}class rr{constructor(e,t){this.tempCounter=0,this.temps=new Map,this.captures=[],this.variantName=void 0,this.mod=e,this.definitionName=t}gentempname(){return"_tmp"+this.tempCounter++}gentemp(e=zt){const t=Yt(this.mod,e),r=function(e,t=Pt){const r=new Nt;return r.width=t,e.forEach((e=>r.write(e))),r.toString()}([t],1/0),n=this.gentempname();let i=this.temps.get(r);return void 0===i&&(i={type:t,names:[]},this.temps.set(r,i)),i.names.push(n),n}block(e){const t=this.temps;this.temps=new Map;const r=e(),n=this.temps;return this.temps=t,Kt(...Array.from(n).map((([e,{type:t,names:r}])=>Ct("let ",((...e)=>new At(e))(...r),": (",t,") | undefined"))),...r)}withCapture(e,t,r){void 0!==e&&this.captures.push({fieldName:e,sourceExpr:t});const n=r(t);return void 0!==e&&this.captures.pop(),n}convertCapture(e,t,r){return this.withCapture(e,t,(e=>Ct(`if (${e} !== void 0) `,this.block((()=>r())))))}buildCapturedCompound(e){const t=[...(r=this.variantName,void 0===r?[]:[Ut(r)]),...this.captures.map((({fieldName:e,sourceExpr:t})=>qt(e,t))),Ct("__as_preserve__() ",Kt(`return from${this.definitionName}(this)`))];var r;return Ct(`${e} = `,Ft(...t))}}class nr{constructor(e){this.modulePath=e,this.seen=new t.FlexSet(ir)}cycleCheck(e,t,r,n){const i=W({module:e.module.length?e.module:this.modulePath,name:e.name});if(this.seen.has(i))return n();{this.seen.add(i);const e=t(i),n=this.modulePath;this.modulePath=i.module;const o=r(e);return this.modulePath=n,o}}}function ir(e){return t.stringify([...e.module,e.name])}function or(e,t,r,n){switch(t._variant){case"or":{const i=[t.pattern0,t.pattern1,...t.patternN];function o(t){return e.variantName=i[t].variantLabel,[...ar(e,i[t].pattern,r,n),...t<i.length-1?[Ct(`if (${n} === void 0) `,e.block((()=>o(t+1))))]:[]]}return o(0)}case"and":{const a=[t.pattern0,t.pattern1,...t.patternN];function s(t){return t<a.length?cr(e,a[t],r,(()=>s(t+1))):[e.buildCapturedCompound(n)]}return s(0)}case"Pattern":return e.variantName=void 0,ar(e,t.value,r,n)}}function ar(e,t,r,n){return cr(e,H.anonymous(t),r,(r=>void 0===r?[e.buildCapturedCompound(n)]:void 0!==e.variantName?"unit"===Qt(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[e.withCapture("value",r,(()=>e.buildCapturedCompound(n)))]:"unit"===Qt(e.mod.resolver(),t).kind?[e.buildCapturedCompound(n)]:[`${n} = ${r}`]))}function sr(e,t,r,n,i,o){function a(n){if(n<t.length)return cr(e,t[n],`${r}[${n}]`,(()=>a(n+1)));if(void 0===i)return o();{const n=e.gentemp(jt.array(zt));return[t.length>0?`${n} = ${r}.slice(${t.length})`:`${n} = ${r}`,...cr(e,tt(i),n,o,!0)]}}const s=void 0===i?Ct(` && ${r}.length === ${t.length}`):0===t.length?"":Ct(` && ${r}.length >= ${t.length}`);return n?a(0):[Ct(`if (_.isSequence(${r})`,s,") ",e.block((()=>a(0))))]}function cr(e,t,r,n,i=!1){let o=et(t),a=rt(t);if("SimplePattern"===o._variant){const t=e.gentemp(Wt(e.mod.resolver(),o.value));return[...dr(e,o.value,r,t,i),e.convertCapture(a,t,(()=>n(t)))]}return function(e,t,r,n,i){switch(t._variant){case"rec":return[Ct(`if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(${r})) `,e.block((()=>cr(e,t.label,`${r}.label`,(()=>cr(e,t.fields,r,i,!0))))))];case"tuple":return sr(e,t.patterns,r,n,void 0,i);case"tuplePrefix":return sr(e,t.fixed,r,n,t.variable,i);case"dict":{const o=Array.from(t.entries);function a(t){if(t<o.length){const[n,i]=o[t],s=e.gentemp();return[Ct(`if ((${s} = ${r}.get(${e.mod.literal(n)})) !== void 0) `,e.block((()=>cr(e,tt(i),s,(()=>a(t+1))))))]}return i()}return[Ct(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>a(0))))]}default:throw new Error("Unreachable")}}(e,o.value,r,i,(()=>n(void 0)))}function dr(e,t,r,n,i){switch(t._variant){case"any":return[`${n} = ${r}`];case"atom":{let e,i=`${r}`;switch(t.atomKind._variant){case"Boolean":e=`typeof ${r} === 'boolean'`;break;case"Float":e=`_.Float.isSingle(${r})`,i=`${r}.value`;break;case"Double":e=`_.Float.isDouble(${r})`,i=`${r}.value`;break;case"SignedInteger":e=`typeof ${r} === 'number'`;break;case"String":e=`typeof ${r} === 'string'`;break;case"ByteString":e=`_.Bytes.isBytes(${r})`;break;case"Symbol":e=`typeof ${r} === 'symbol'`}return[Ct(`${n} = `,e," ? ",i," : void 0")]}case"embedded":return[`${n} = _.isEmbedded<_embedded>(${r}) ? ${r}.embeddedValue : void 0`];case"lit":return[`${n} = _.is(${r}, ${e.mod.literal(t.value)}) ? {} : void 0`];case"seqof":{const o=()=>{const i=e.gentempname();return[Ct(`${n} = []`),Ct(`for (const ${i} of ${r}) `,e.block((()=>[...cr(e,nt(t.pattern),i,(e=>[`${n}.push(${e})`,"continue"])),Ct(`${n} = void 0`),Ct("break")])))]};return i?o():[`${n} = void 0`,Ct(`if (_.isSequence(${r})) `,e.block(o))]}case"setof":return[`${n} = void 0`,Ct(`if (_.Set.isSet<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname();return[Ct(`${n} = new _.KeyedSet()`),Ct(`for (const ${i} of ${r}) `,e.block((()=>[...cr(e,nt(t.pattern),i,(e=>[`${n}.add(${e})`,"continue"])),Ct(`${n} = void 0`),Ct("break")])))]})))];case"dictof":return[`${n} = void 0`,Ct(`if (_.Dictionary.isDictionary<_embedded>(${r})) `,e.block((()=>{const i=e.gentempname(),o=e.gentempname();return[Ct(`${n} = new _.KeyedDictionary()`),Ct(`for (const [${o}, ${i}] of ${r}) `,e.block((()=>[...cr(e,nt(t.key),o,(r=>cr(e,nt(t.value),i,(e=>[`${n}.set(${r}, ${e})`,"continue"])))),Ct(`${n} = void 0`),Ct("break")])))]})))];case"Ref":return e.mod.lookup(t.value,((e,i)=>[`${n} = to${t.value.name.description}(${r})`]),((i,o,a,s,c,d)=>(e.mod.imports.add([i,o,a,s]),[`${n} = ${o}${s}.to${t.value.name.description}${e.mod.genericArgs()}(${r})`])));default:throw new Error("Unreachable")}}function ur(e,t,r,n,i,o,a){const s=[];let c=!1;function d(e,t){"unit"!==e.kind&&s.push([t,e])}"record"===i.kind?i.fields.forEach(d):(d(i,"value"),c=void 0===n&&"unit"!==i.kind);const u=void 0!==n?[qt("_variant",JSON.stringify(n))]:[];s.forEach((([e,t])=>u.push(Ct(JSON.stringify(e),": ",qe(e))))),u.push(Ct("__as_preserve__() ",Kt(`return from${qe(t)}(this)`)));const l=s.length>1?[Ct(Ft(...s.map((e=>qe(e[0])))),": ",Ft(...s.map((t=>Ct(qe(t[0]),": ",Yt(e,t[1]))))))]:s.map((t=>Ct(qe(t[0]),": ",Yt(e,t[1]))));return[Ct(`export function ${qe(r)}`,e.genericParametersFor(o),Ot(...l),": ",a," ",Kt(Ct("return ",c?"value":Ft(...u)))),Ct(`${qe(r)}.schema = function () `,Kt(Ct("return ",Ft("schema: _schema()","imports: _imports",`definitionName: _.Symbol.for(${JSON.stringify(t)})`,...void 0===n?[]:[`variant: _.Symbol.for(${JSON.stringify(n)})`]))))]}function lr(e,t,r){switch(t._variant){case"or":return[Ct(`switch (${r}._variant) `,Kt(...[t.pattern0,t.pattern1,...t.patternN].map((t=>Ct("case ",JSON.stringify(t.variantLabel),": ",e.block((()=>{const n="SimplePattern"===t.pattern._variant;return[Ct("return ",fr(e,t.pattern,n?`${r}.value`:r))]})))))))];case"and":return[Ct("return _.merge",Ot("(a, b) => (a === b) ? a : void 0",...[t.pattern0,t.pattern1,...t.patternN].flatMap((t=>"anonymous"===t._variant&&"SimplePattern"===t.value._variant?[]:[mr(e,t,r)]))))];case"Pattern":return[Ct("return ",fr(e,t.value,`${r}`))]}}function fr(e,t,r){switch(t._variant){case"SimplePattern":return(t=>{switch(t._variant){case"any":return`${r}`;case"atom":switch(t.atomKind._variant){case"Float":return`_.Single(${r})`;case"Double":return`_.Double(${r})`;default:return`${r}`}case"lit":return e.mod.literal(t.value);case"embedded":return`_.embed(${r})`;case"seqof":return Ct(`${r}.map(v => `,fr(e,q.SimplePattern(t.pattern),"v"),")");case"setof":return Ct("new _.Set<_embedded>",Ot(Ct(`_.Array.from(${r}.values()).map(v => `,fr(e,q.SimplePattern(t.pattern),"v"),")")));case"dictof":return Ct("new _.Dictionary<_embedded>",Ot(Ct(`_.Array.from(${r}.entries()).map(([k, v]) => `,Lt(fr(e,q.SimplePattern(t.key),"k"),fr(e,q.SimplePattern(t.value),"v")),")")));case"Ref":return e.mod.lookup(t.value,((n,i)=>`from${t.value.name.description}${e.mod.genericArgs()}(${r})`),((n,i,o,a,s,c)=>(e.mod.imports.add([n,i,o,a]),`${i}${a}.from${t.value.name.description}${e.mod.genericArgs()}(${r})`)))}})(t.value);case"CompoundPattern":return(t=>{switch(t._variant){case"rec":return Ct("_.Record",Ot(mr(e,t.label,r),mr(e,t.fields,r)));case"tuple":return Lt(...t.patterns.map((t=>mr(e,t,r))));case"tuplePrefix":{const n=mr(e,tt(t.variable),r);return 0===t.fixed.length?n:Lt(...t.fixed.map((t=>mr(e,t,r))),Ct("... ",n))}case"dict":return Ct("new _.Dictionary<_embedded>",Ot(Lt(...Array.from(t.entries.entries()).map((([t,n])=>Lt(e.mod.literal(t),function(e,t,r){if("named"===t._variant){const n=br(r,t.value.name.description);return fr(e,q.SimplePattern(t.value.pattern),n)}return fr(e,q.SimplePattern(t.value),r)}(e,n,r)))))))}})(t.value)}}function br(e,t){return`${e}[${JSON.stringify(t)}]`}function mr(e,t,r){if("named"===t._variant){const n=br(r,t.value.name.description);return fr(e,q.SimplePattern(t.value.pattern),n)}return fr(e,t.value,r)}e.GenType=er,e.Meta=at,e.SchemaSyntaxError=dt,e.Type=Jt,e.checkSchema=st,e.compile=function(e,r,n,i={}){var o;const a=new tr(e,r,n,i);a.definePreamble("let __schema: _.Value | null = null;"),a.definePreamble(Ct("export function _schema() ",Kt(Ct("if (__schema === null) ",Kt(`__schema = _.decode<_.GenericEmbedded>(_.Bytes.fromHex("${t.encode(oe(n)).toHex()}"))`)),"return __schema"))),"false"!==n.embeddedType._variant&&a.defineType(Ct("export type _embedded = ",a.embeddedType,";"));for(const[e,r]of n.definitions){const n=Zt(a.resolver(),r),i=t.stringify(e),o=a.withAsPreserveMixinType(i,n);a.defineType(Ct(`export type ${i}`,a.genericParametersFor(n)," = ",Yt(a,n),";")),"union"===n.kind?a.defineFunctions(i,(e=>[Ct(`export namespace ${i} `,Kt(...Array.from(n.variants).flatMap((([e,t])=>ur(a,i,e,e,t,n,o)))))])):a.defineFunctions(i,(e=>ur(a,i,i,void 0,n,n,o)))}for(const[e,t]of n.definitions){const r=Zt(a.resolver(),t),n=e,i=e.description,o=a.withAsPreserveMixinType(i,r);a.defineFunctions(i,(e=>[Ct(`export function as${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): ",o," ",e.block((()=>[Ct(`let result = to${n.description}(v)`),Ct("if (result === void 0) ",`throw new TypeError(\`Invalid ${n.description}: \${_.stringify(v)}\`)`),Ct("return result")])))])),a.defineFunctions(i,(e=>[Ct(`export function to${n.description}`,a.genericParameters(),"(v: _.Value<_embedded>): undefined | ",o," ",e.block((()=>[Ct("let result: undefined | ",o),...or(e,t,"v","result"),Ct("return result")]))),"union"===r.kind?Ct(`export namespace ${qe(n.description)} `,Kt(Ct(`export const __from_preserve__ = to${n.description}`))):Ct(`${qe(n.description)}.__from_preserve__ = to${n.description};`)])),a.defineFunctions(i,(e=>[Ct(`export function from${n.description}`,a.genericParameters(),"(_v: ",n.description,a.genericArgsFor(r),"): _.Value<_embedded> ",e.block((()=>lr(e,t,"_v"))))]))}a.definePreamble(Ct("export const _imports = ",Ft(...Array.from(a.imports.values()).map((([e,r,n,i])=>Ct(t.stringify(We(e)),": ",r,i))))));const s=new Nt;s.write(`import * as _ from ${JSON.stringify(null!==(o=i.preservesModule)&&void 0!==o?o:"@preserves/core")};\n`),a.imports.forEach((([e,t,r,n])=>{s.write(`import * as ${t} from ${JSON.stringify(r)};\n`)})),s.newline();const c=Array.from(a.literals);c.sort(((e,t)=>e[1]<t[1]?-1:e[1]===t[1]?0:1));for(const[e,r]of c)s.write(Ct(`export const ${r} = `,(d=e,t.fold(d,{boolean:e=>e.toString(),single:e=>e.toString(),double:e=>e.toString(),integer:e=>e.toString(),string:e=>JSON.stringify(e),bytes:e=>Ct("Uint8Array.from(",Lt(...Array.from(e).map((e=>e.toString()))),")"),symbol:e=>`_.Symbol.for(${JSON.stringify(e.description)})`,record:(e,t)=>Ct("_.Record<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>",Ot(t(e.label),Lt(...e.map(t)))),array:(e,t)=>Lt(...e.map(t)),set:(e,t)=>Ct("new _.Set<_.Value<_embedded>>",Ot(Lt(...Array.from(e).map(t)))),dictionary:(e,t)=>Ct("new _.Dictionary<_embedded>",Ot(Lt(...Array.from(e).map((([e,r])=>Lt(t(e),t(r))))))),annotated:(e,t)=>Ct("_.annotate<_embedded>",Ot(t(e.item),...e.annotations.map(t))),embedded(e,r){throw new Error(`Cannot emit source code for construction of embedded ${t.stringify(e)}`)}})),";\n"));var d;return s.newline(),a.preamble.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.typedefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.newline(),a.functiondefs.forEach((e=>{s.write(e),s.newline(),s.newline()})),s.toString()},e.parseSchema=_t,e.readSchema=function(e,t){const r=st(_t(vt(e,t),null!=t?t:{}));if(r.ok)return r.schema;throw new Error("Schema is not invertible:\n"+r.problems.map((e=>" - "+e)).join("\n"))},e.recordPosition=lt,e.refPosition=ft}));

@@ -43,3 +43,3 @@ import * as M from '../meta';

case 'setof':
return seq(`new _.Set<_embedded>`, parens(`_.Array.from(${src}.values()).map(v => `, unconverterFor(ctx, M.Pattern.SimplePattern(p.pattern), 'v'), `)`));
return seq(`new _.Set<_embedded>`, parens(seq(`_.Array.from(${src}.values()).map(v => `, unconverterFor(ctx, M.Pattern.SimplePattern(p.pattern), 'v'), `)`)));
case 'dictof':

@@ -46,0 +46,0 @@ return seq(`new _.Dictionary<_embedded>`, parens(seq(`_.Array.from(${src}.entries()).map(([k, v]) => `, brackets(unconverterFor(ctx, M.Pattern.SimplePattern(p.key), 'k'), unconverterFor(ctx, M.Pattern.SimplePattern(p.value), 'v')), `)`)));

{
"name": "@preserves/schema",
"version": "0.21.9",
"version": "0.21.10",
"description": "Schema support for Preserves data serialization format",

@@ -42,3 +42,3 @@ "homepage": "https://gitlab.com/preserves/preserves",

},
"gitHead": "55d55d9ccfa053842e8c134ba6628fe89ef4d4fa"
"gitHead": "8dc51d70e731b166f590b1a630bd272b6f865479"
}

@@ -56,6 +56,6 @@ import * as M from '../meta';

case 'setof':
return seq(`new _.Set<_embedded>`, parens(
return seq(`new _.Set<_embedded>`, parens(seq(
`_.Array.from(${src}.values()).map(v => `,
unconverterFor(ctx, M.Pattern.SimplePattern(p.pattern), 'v'),
`)`));
`)`)));
case 'dictof':

@@ -62,0 +62,0 @@ return seq(`new _.Dictionary<_embedded>`, parens(seq(

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc