"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class d extends Error{constructor(t){super(t)}}const C=new d("end of input stream reached");class Z{constructor(t){this.index=0,this.array=t}isEof(){return this.index>=this.array.length}read(){if(this.isEof())throw C;const t=this.array[this.index];return this.index++,t}readArray(t){if(t<0)throw new d("invalid read amount");if(t===0)return new Uint8Array([]);if(this.isEof())throw C;const e=Math.min(this.array.length,this.index+t),i=new Uint8Array(this.array.subarray(this.index,e));return this.index=e,i}peek(){if(this.isEof())throw C;return this.array[this.index]}skip(t){if(this.isEof())throw C;if(t<0)throw new d("invalid skip amount");this.index+=t}}class E{constructor(t,e){this.enabled=t,this.maxStrings=e,this.strings=[],this.stringMap={},this.reset()}reset(){this.strings=[],this.stringMap={}}addString(t){if(!this.enabled||new TextEncoder().encode(t).length>64)return-1;if(t in this.stringMap)return this.stringMap[t];this.strings.length>=this.maxStrings&&this.reset();const i=this.strings.length;return this.strings.push(t),this.stringMap[t]=i,i}getReference(t){return this.enabled&&t in this.stringMap?this.stringMap[t]:-1}getString(t){if(!this.enabled)throw new d("shared strings are not enabled");if(t>=this.strings.length)throw new d("shared string reference out of range");return this.strings[t]}}function T(h){return typeof h=="bigint"&&h>=BigInt(Number.MIN_SAFE_INTEGER)&&h<=BigInt(Number.MAX_SAFE_INTEGER)?Number(h):h}function M(h,t,e){return t>e?Math.ceil(h*t/e):Math.floor(h*t/e)}class F{static decode(t){if(t<0)throw new d("illegal zigzag value");const e=BigInt(t);return e%BigInt(2)===BigInt(1)?T(-(e+BigInt(1))/BigInt(2)):T(e/BigInt(2))}static encode(t){const e=BigInt(t);return e<0?T(-e*BigInt(2)-BigInt(1)):T(e*BigInt(2))}}const $=new TextDecoder("ascii"),q=new TextEncoder;class S{static decode(t){return $.decode(t)}static read(t,e){const i=t.readArray(e);return S.decode(i)}static encode(t){return q.encode(t)}static write(t,e){t.write(S.encode(e))}static isASCII(t){for(let e=0;e127)return!1;return!0}}const H=new TextDecoder("utf8"),J=new TextEncoder;class m{static decode(t){return H.decode(t)}static read(t,e){const i=t.readArray(e);return m.decode(i)}static encode(t){return J.encode(t)}static write(t,e){t.write(m.encode(e))}}class v{static read(t){const e=[];for(;;){const i=t.read();if(i===252)break;e.push(i)}return new Uint8Array(e)}static readASCII(t){return S.decode(v.read(t))}static readUTF8(t){return m.decode(v.read(t))}static write(t,e){t.write(e),t.write(252)}static writeASCII(t,e){v.write(t,S.encode(e))}static writeUTF8(t,e){v.write(t,m.encode(e))}}var K={exports:{}};(function(h){(function(t){var e=function(r,n,u){var a=r instanceof ArrayBuffer||typeof Buffer<"u"&&r instanceof Buffer;if(!a)throw new Error("Must specify a valid ArrayBuffer or Buffer.");n=n||0,u=u||r.byteLength||r.length,this._view=new Uint8Array(r.buffer||r,n,u),this.bigEndian=!1};e._scratch=new DataView(new ArrayBuffer(8)),Object.defineProperty(e.prototype,"buffer",{get:function(){return typeof Buffer<"u"?Buffer.from(this._view.buffer):this._view.buffer},enumerable:!0,configurable:!1}),Object.defineProperty(e.prototype,"byteLength",{get:function(){return this._view.length},enumerable:!0,configurable:!1}),e.prototype._setBit=function(r,n){n?this._view[r>>3]|=1<<(r&7):this._view[r>>3]&=~(1<<(r&7))},e.prototype.getBits=function(r,n,u){var a=this._view.length*8-r;if(n>a)throw new Error("Cannot get "+n+" bit(s) from offset "+r+", "+a+" available");for(var f=0,w=0;w>3],y=Math.min(V,8-I),U,_;this.bigEndian?(U=~(255<>8-y-I&U,f<<=y,f|=_):(U=~(255<>I&U,f|=_<>>0},e.prototype.setBits=function(r,n,u){var a=this._view.length*8-r;if(u>a)throw new Error("Cannot set "+u+" bit(s) from offset "+r+", "+a+" available");for(var f=0;f>3,g=Math.min(w,8-V),y,U,_;if(this.bigEndian){y=~(-1<>u-f-g&y;var j=8-V-g;_=~(y<>=g,_=~(y<this._length)throw new Error("Trying to read past the end of the stream");var u=this._view[r](this._index);return this._index+=n,u}},s=function(r,n){return function(u){this._view[r](this._index,u),this._index+=n}};function c(r,n){return p(r,n,!1)}function l(r,n){return p(r,n,!0)}function p(r,n,u){if(n===0)return"";var a=0,f=[],w=!0,V=!!n;for(n||(n=Math.floor((r._length-r._index)/8));a>6|192),n.push(a&63|128)):a<=65535?(n.push(a>>12|224),n.push(a>>6&63|128),n.push(a&63|128)):(n.push(a>>18|240),n.push(a>>12&63|128),n.push(a>>6&63|128),n.push(a&63|128));return n}var o=function(r,n,u){var a=r instanceof ArrayBuffer||typeof Buffer<"u"&&r instanceof Buffer;if(!(r instanceof e)&&!a)throw new Error("Must specify a valid BitView, ArrayBuffer or Buffer");a?this._view=new e(r,n,u):this._view=r,this._index=0,this._startIndex=0,this._length=this._view.byteLength*8};Object.defineProperty(o.prototype,"index",{get:function(){return this._index-this._startIndex},set:function(r){this._index=r+this._startIndex},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"length",{get:function(){return this._length-this._startIndex},set:function(r){this._length=r+this._startIndex},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"bitsLeft",{get:function(){return this._length-this._index},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"byteIndex",{get:function(){return Math.ceil(this._index/8)},set:function(r){this._index=r*8},enumerable:!0,configurable:!0}),Object.defineProperty(o.prototype,"buffer",{get:function(){return this._view.buffer},enumerable:!0,configurable:!1}),Object.defineProperty(o.prototype,"view",{get:function(){return this._view},enumerable:!0,configurable:!1}),Object.defineProperty(o.prototype,"bigEndian",{get:function(){return this._view.bigEndian},set:function(r){this._view.bigEndian=r},enumerable:!0,configurable:!1}),o.prototype.readBits=function(r,n){var u=this._view.getBits(this._index,r,n);return this._index+=r,u},o.prototype.writeBits=function(r,n){this._view.setBits(this._index,r,n),this._index+=n},o.prototype.readBoolean=i("getBoolean",1),o.prototype.readInt8=i("getInt8",8),o.prototype.readUint8=i("getUint8",8),o.prototype.readInt16=i("getInt16",16),o.prototype.readUint16=i("getUint16",16),o.prototype.readInt32=i("getInt32",32),o.prototype.readUint32=i("getUint32",32),o.prototype.readFloat32=i("getFloat32",32),o.prototype.readFloat64=i("getFloat64",64),o.prototype.writeBoolean=s("setBoolean",1),o.prototype.writeInt8=s("setInt8",8),o.prototype.writeUint8=s("setUint8",8),o.prototype.writeInt16=s("setInt16",16),o.prototype.writeUint16=s("setUint16",16),o.prototype.writeInt32=s("setInt32",32),o.prototype.writeUint32=s("setUint32",32),o.prototype.writeFloat32=s("setFloat32",32),o.prototype.writeFloat64=s("setFloat64",64),o.prototype.readASCIIString=function(r){return c(this,r)},o.prototype.readUTF8String=function(r){return l(this,r)},o.prototype.writeASCIIString=function(r,n){N(this,r,n)},o.prototype.writeUTF8String=function(r,n){A(this,r,n)},o.prototype.readBitStream=function(r){var n=new o(this._view);return n._startIndex=this._index,n._index=this._index,n.length=r,this._index+=r,n},o.prototype.writeBitStream=function(r,n){n||(n=r.bitsLeft);for(var u;n>0;)u=Math.min(n,32),this.writeBits(r.readBits(u),u),n-=u},o.prototype.readArrayBuffer=function(r){var n=this._view.getArrayBuffer(this._index,r);return this._index+=r*8,n},o.prototype.writeArrayBuffer=function(r,n){this.writeBitStream(new o(r),n*8)},h.exports&&(h.exports={BitView:e,BitStream:o})})()})(K);var P=K.exports;function z(h){const t=new ArrayBuffer(h.length),e=new DataView(t);for(let i=0;i0;){const N=i.getBits(s,l);p.push(N),s+=l,c-=l,l=7}return new Uint8Array(p)}static write(t,e){t.write(b.encode(e))}static writeFloat32(t,e){b.write(t,D.encode(e))}static writeFloat64(t,e){b.write(t,O.encode(e))}}class B{static decode(t){if(t.length<=0)throw new d("invalid VInt");let e=BigInt(0);for(let i=0;i0;)e.length===0?(e.push(Number(i%BigInt(64))|128),i/=BigInt(64)):(e.push(Number(i%BigInt(128))),i/=BigInt(128));return e.length===0&&e.push(128),e.reverse(),new Uint8Array(e)}static write(t,e){const i=B.encode(e);t.write(i)}}class R{static decode(t){let e=BigInt(0);if(t.length===0)return e;const i=(t[0]&128)===128;for(let s=0;s0;){const c=i/BigInt(256),l=Number(i%BigInt(256));s.push(e?l^255:l),i=c}return e?(s[s.length-1]&128)!==128&&s.push(255):s[s.length-1]&128&&s.push(0),s.reverse(),new Uint8Array(s)}}class x{static decode(t){const e=M(t.length,7,8),i=new ArrayBuffer(e),s=new P.BitView(i);s.bigEndian=!0;let c=0,l=e*8;for(let p=0;p0;){const l=Math.min(c,7),p=i.getBits(s,l);e.push(p),s+=l,c-=l}return new Uint8Array(e)}static write(t,e){B.write(t,e.length),t.write(x.encode(e))}static writeBigInt(t,e){x.write(t,R.encode(e))}}class Q{static read(t){const e=F.decode(B.read(t));if(typeof e=="bigint")throw new d("invalid scale");const i=x.readBigInt(t);return Number(i)*Math.pow(10,-e)}}const L={};function Y(h,t){return new W(h,t).decode()}function k(h,t){return new W(h,t).decode()}class W{constructor(t,e){this.inputStream=new Z(t),e!==void 0?this.options={...L,...e}:this.options=L,this.sharedPropertyName=!1,this.sharedStringValue=!1,this.rawBinary=!1,this.version=0,this.sharedPropertyNames=new E(!1,1024),this.sharedStringValues=new E(!1,1024)}decode(){const t=this.inputStream.read(),e=this.inputStream.read(),i=this.inputStream.read();if(t!==58||e!==41||i!==10)throw new d("invalid Smile header");const s=this.inputStream.read();return this.sharedPropertyName=(s&1)===1,this.sharedStringValue=(s&2)===2,this.rawBinary=(s&4)===4,this.version=s>>4,this.sharedPropertyNames=new E(this.sharedPropertyName,1024),this.sharedStringValues=new E(this.sharedStringValue,1024),this.readValue()}readValue(){const t=this.inputStream.read(),e=t>>5,i=t&31;switch(e){case 0:return this.sharedStringValues.getString(i-1);case 1:return this.readSimpleLiteralValue(t);case 2:{const s=i+1,c=S.read(this.inputStream,s);return this.sharedStringValues.addString(c),c}case 3:{const s=i+33,c=S.read(this.inputStream,s);return this.sharedStringValues.addString(c),c}case 4:{const s=i+2,c=m.read(this.inputStream,s);return this.sharedStringValues.addString(c),c}case 5:{const s=i+34,c=m.read(this.inputStream,s);return this.sharedStringValues.addString(c),c}case 6:return F.decode(i);case 7:return this.readBinaryLongTextStructureValues(t);default:throw new d(`unknown token class: ${e}`)}}readSimpleLiteralValue(t){if(t===32)return"";if(t===33)return null;if(t===34)return!1;if(t===35)return!0;if(t===36)return F.decode(B.read(this.inputStream));if(t===37)return F.decode(B.read(this.inputStream));if(t===38)return x.readBigInt(this.inputStream);if(t===40)return b.readFloat32(this.inputStream);if(t===41)return b.readFloat64(this.inputStream);if(t===42)return Q.read(this.inputStream);throw new d("invalid value token 0x"+t.toString(16))}readBinaryLongTextStructureValues(t){if(t===224)return v.readASCII(this.inputStream);if(t===228)return v.readUTF8(this.inputStream);if(t===232)return x.read(this.inputStream);if(t>=236&&t<=239){const e=(t&3)<<8|this.inputStream.read();return this.sharedStringValues.getString(e)}else if(t===248){const e=[];for(;this.inputStream.peek()!==249;)e.push(this.readValue());return this.inputStream.read(),e}else if(t===250){const e={};for(;this.inputStream.peek()!==251;){const i=this.readKey(),s=this.readValue();e[i]=s}return this.inputStream.read(),e}else if(t===253){const e=B.read(this.inputStream);if(typeof e=="bigint")throw new d("invalid length");return this.inputStream.readArray(e)}else throw new d("invalid value token 0x"+t.toString(16))}readKey(){const t=this.inputStream.read();if(t===32)return"";if(t>=48&&t<=51){const e=(t&3)<<8|this.inputStream.read();return this.sharedPropertyNames.getString(e)}else{if(t===52)return v.readUTF8(this.inputStream);if(t>=64&&t<=127){const e=t&63;return this.sharedPropertyNames.getString(e)}else if(t>=128&&t<=191){const e=(t&63)+1,i=S.read(this.inputStream,e);return this.sharedPropertyNames.addString(i),i}else if(t>=192&&t<=247){const e=(t&63)+2,i=m.read(this.inputStream,e);return this.sharedPropertyNames.addString(i),i}else throw new d("invalid key token 0x"+t.toString(16))}}}class tt{constructor(){this.buffer=[]}write(t){Array.isArray(t)||t instanceof Uint8Array?this.buffer=this.buffer.concat(Array.from(t)):this.buffer.push(t)}toUint8Array(){return new Uint8Array(this.buffer)}}const G={sharedPropertyName:!0,sharedStringValue:!1,rawBinary:!1},et=0,rt=2147483647,it=-2147483648;BigInt("9223372036854775807");BigInt("-9223372036854775808");function nt(h,t){return new st(h,t).encode()}class st{constructor(t,e){this.value=t,e!==void 0?this.options={...G,...e}:this.options=G,this.outputStream=new tt,this.sharedPropertyNames=new E(this.options.sharedPropertyName,1024),this.sharedStringValues=new E(this.options.sharedStringValue,1024)}encode(){this.outputStream.write([58,41,10]);let t=et<<4;return this.options.sharedPropertyName&&(t|=1),this.options.sharedStringValue&&(t|=2),this.options.rawBinary&&(t|=4),this.outputStream.write(t),this.writeValue(this.value),this.outputStream.toUint8Array()}writeValue(t){if(t==null){this.writeNull();return}else if(t instanceof Uint8Array){this.writeBinary(t);return}else if(Array.isArray(t)){this.writeArray(t);return}else{const e=typeof t;switch(e){case"undefined":this.writeNull();return;case"boolean":this.writeBoolean(t);return;case"string":this.writeString(t);return;case"number":this.writeNumber(t);return;case"bigint":this.writeBigInt(t);return;case"object":this.writeObject(t);return;default:throw new d("unsupported type: "+e)}}}writeNull(){this.outputStream.write(33)}writeBoolean(t){t?this.outputStream.write(35):this.outputStream.write(34)}writeString(t){if(t===""){this.outputStream.write(32);return}const e=this.sharedStringValues.getReference(t);if(e>=0)if(e<31){this.outputStream.write(0|e+1);return}else{this.outputStream.write(236|e>>8&3),this.outputStream.write(e&255);return}else if(this.sharedStringValues.addString(t),S.isASCII(t))if(t.length<=32){this.outputStream.write(64|t.length-1),S.write(this.outputStream,t);return}else if(t.length<=64){this.outputStream.write(96|t.length-33),S.write(this.outputStream,t);return}else{this.outputStream.write(224),v.writeASCII(this.outputStream,t);return}else{const i=m.encode(t);if(i.length<=33){this.outputStream.write(128|i.length-2),m.write(this.outputStream,t);return}else if(i.length<=64){this.outputStream.write(160|i.length-34),m.write(this.outputStream,t);return}else{this.outputStream.write(228),v.writeUTF8(this.outputStream,t);return}}}writeNumber(t){if(Number.isSafeInteger(t))if(t>=-16&&t<=15){const e=F.encode(t);if(typeof e=="number"&&e<32)this.outputStream.write(192|e);else throw new d("unexpected error")}else t>=it&&t<=rt?(this.outputStream.write(36),B.write(this.outputStream,F.encode(t))):t>=Number.MIN_SAFE_INTEGER&&t<=Number.MAX_SAFE_INTEGER?(this.outputStream.write(37),B.write(this.outputStream,F.encode(t))):(this.outputStream.write(37),B.write(this.outputStream,F.encode(t)));else this.outputStream.write(41),b.writeFloat64(this.outputStream,t)}writeBigInt(t){this.outputStream.write(38),x.writeBigInt(this.outputStream,t)}writeArray(t){this.outputStream.write(248);for(let e=0;e=0)if(e<64){this.outputStream.write(64|e);return}else{this.outputStream.write(48|e>>8&3),this.outputStream.write(e&255);return}else if(this.sharedPropertyNames.addString(t),S.isASCII(t))if(t.length<=64){this.outputStream.write(128|t.length-1),S.write(this.outputStream,t);return}else{this.outputStream.write(52),v.writeUTF8(this.outputStream,t);return}else{const i=m.encode(t);if(i.length<=56){this.outputStream.write(192|i.length-2),m.write(this.outputStream,t);return}else{this.outputStream.write(52),v.writeUTF8(this.outputStream,t);return}}}}exports.SmileError=d;exports.decode=k;exports.encode=nt;exports.parse=Y; //# sourceMappingURL=smile-js.cjs.map