nsd-ble 0.3.3 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./Objects-WdcC281c.cjs"),i=require("./enums.cjs"),m=require("./elements.cjs");class v{data=[];size=0;total=0}const E=7,g=new Map;function _(t){return g.get(t)}function M(t){g.delete(t)}function R(t){const e=t.getUint8(i.Data.CACHE),n=t.getUint16(i.Data.DATA_SIZE,!0),r=t.getUint16(i.Data.POSITION,!0),o=t.byteLength-E;if(!g.has(e)){const a=new v;a.size=n,g.set(e,a)}const s=g.get(e);for(let a=0;a<o;a++)s.data[a+r]=t.getUint8(E+a);s.total+=o}function L(t){const e=g.get(t);return e.total==e.size||e.size==0}function P(){return{add:R,get:_,remove:M,isDone:L}}const{serviceUuid:$,handshakeCharUuid:k}=c.useBluetoothSettings(),D=new c.libExports.SyncEvent,{add:H,get:O,remove:G,isDone:z}=P();function F(){return new Promise((t,e)=>{c.useBluetooth().subscribe($,k,V).then(()=>t()).catch(n=>e(n))})}function V(t){console.log(t,t.buffer.byteLength);const e=t.getUint8(i.Data.CACHE);H(t),z(e)&&(D.post(W(e)),G(e))}function W(t){const e=O(t),n=new Map;let r=1;for(;r<e.size;){const o=e.data[r++],s=e.data[r++],a=[];for(let u=0;u<s;u++)a.push(e.data[r++]);n.set(o,a)}return n}function U(){return{subscribe:F,onNodes:D}}const{onNodes:q}=U(),{onMessageDone:j,onNode:Z,onNodeLost:K}=c.useFloodCharacteristic(),{onPlusButton:X,onMinButton:J,onEitherButton:Q}=c.useMessageCharacteristic(),{connect:Y,reconnect:tt,onConnect:et,onDisconnect:nt}=c.useBluetooth();function rt(){return new Promise((t,e)=>{Y(c.useBluetoothSettings().options).then(()=>b(t)).catch(n=>e(n))})}function ot(){return new Promise((t,e)=>{tt().then(()=>b(t)).catch(n=>e(n))})}function b(t){Promise.all([U().subscribe(),c.useMessageCharacteristic().subscribe(),c.useFloodCharacteristic().subscribe()]).then(t)}function x(){return{connect:rt,onConnect:et,onDisconnect:nt,reconnect:ot,onNodes:q,onNode:Z,onNodeLost:K,onPlusButton:X,onMinButton:J,onEitherButton:Q,onMessageDone:j}}function st(t){return{upload:(e,n)=>{const r=new Uint8Array(n.length+1);return r.set([e],0),r.set(n,1),new c.Buffer(i.DataType.UPLOAD,t.id,r)},clear:()=>new c.Buffer(i.DataType.CLEAR_GRAPHICS,t.id)}}class it{id;buffers;totalPackets=0;onProgress=new c.libExports.SyncEvent;constructor(e,n){this.id=n,this.buffers=e,this.buffers.forEach(r=>{this.totalPackets+=r.packets.length})}async send(){const e=this.totalPackets&255,n=this.totalPackets>>8&255,r=new Uint8Array([e,n]),o=new c.Buffer(i.DataType.LOADER,this.id,r),s=this.buffers.length;let a=0;await o.send();for(const u of this.buffers)await u.send(),a++,this.onProgress.post({current:a,total:s,percentage:a/s})}}function at(t){return{load:e=>new it(e,t.id)}}const{onNodes:ct,onNode:ut,onNodeLost:ht}=x();ct.attach(ft);ut.attach(dt);ht.attach(lt);const h=[],w=new c.libExports.SyncEvent;function ft(t){console.log("standees",t);for(const[e,n]of t)B(e),h.find(r=>r.id===e).neighbors=n;w.post(h)}function dt(t){console.log("nodes",t);const e=t.keys().next().value,n=t.get(e);B(e),h.find(r=>r.id===e).neighbors=n,w.post(h)}function lt(t){gt(t),w.post(h)}function B(t){h.some(e=>e.id===t)||h.push(new c.Standee(t))}function gt(t){const e=h.findIndex(n=>n.id===t);e!==-1&&h.splice(e,1)}function pt(){return{standees:h,onChange:w}}function yt(t){const e=()=>{let r=Math.floor(Math.random()*256);return n(r)?e():r},n=r=>t.some(o=>o.id===r);return{generate:e}}function wt(t){return{add:r=>(r.id=yt(t.elements).generate(),t.elements.push(r),r),remove:r=>{t.elements=t.elements.filter(o=>o.id!==r)}}}function At(t){return new c.Buffer(i.DataType.CLEAR_SCREEN,t.id)}function mt(t){let e=new Uint8Array;for(const n of t.elements)n.dirty!==!1&&(n instanceof m.Image&&(e=T(e,Tt(n))),n instanceof m.Rectangle&&(e=T(e,Et(n))),n instanceof m.Text&&(e=T(e,Dt(n))),n.dirty=!1);return new c.Buffer(i.DataType.ALL,t.id,e)}function T(t,e){const n=new Uint8Array(t.length+e.length);return n.set(t,0),n.set(e,t.length),n}function Tt(t){return new Uint8Array([i.DataType.DRAW_IMAGE,7,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.data])}function Et(t){return new Uint8Array([i.DataType.DRAW_RECTANGLE,9,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.radius,t.fill?1:0,t.colorBlack?0:1])}function Dt(t){const e=new TextEncoder().encode(t.text),n=new Uint8Array(9+e.length);return n.set([i.DataType.DRAW_TEXT,7+e.length,t.id,t.show?1:0,t.x,t.y,t.font,t.colorBlack?0:1,t.center?1:0],0),n.set(e,9),n}function Ut(){return{clear:At,draw:mt}}function bt(t,e){const n=Math.ceil(t.length/e),r=Math.ceil(e/8),o=new Uint8Array(r*n);for(let s=0;s<n;s++)for(let a=0;a<r;a++){let u=0;for(let l=0;l<8;l++){const p=a*8+l,y=s*e+p;p<e&&y<t.length&&t[y]&&(u|=1<<7-l)}o[s*r+a]=I(u)}return o}function I(t){return t=(t&240)>>4|(t&15)<<4,t=(t&204)>>2|(t&51)<<2,t=(t&170)>>1|(t&85)<<1,t}function xt(t){const e=t.reduce((o,s)=>o+s.length,0),n=new Uint8Array(e);let r=0;for(const o of t)n.set(o,r),r+=o.length;return n}function A(){return{pixelsToBytes:bt,bitswap:I,flattenUint8Arrays:xt}}const{flattenUint8Arrays:C}=A();class d{element_id;property;constructor(e,n){this.element_id=e,this.property=n}}class f{target;constructor(e){this.target=e}format(){return new Uint8Array}}class Bt extends f{format(){return new Uint8Array([i.ActionType.INCREMENT,2,this.target.element_id,this.target.property])}}class It extends f{format(){return new Uint8Array([i.ActionType.DECREMENT,2,this.target.element_id,this.target.property])}}class Ct extends f{format(){return new Uint8Array([i.ActionType.SHOW,1,this.target.element_id])}}class Nt extends f{format(){return new Uint8Array([i.ActionType.HIDE,1,this.target.element_id])}}class St extends f{value;constructor(e,n){super(e),this.value=n}format(){const e=this.dataType(),n=this.convertValue(e),r=new Uint8Array(5+n.length),o=[i.ActionType.SET,n.length+3,e,this.target.element_id,this.target.property];return r.set(o,0),r.set(n,5),r}dataType(){return typeof this.value==="string"?i.ActionDataType.STRING:i.ActionDataType.NUMBER}convertValue(e){return e===i.ActionDataType.STRING?new TextEncoder().encode(this.value):e===i.ActionDataType.NUMBER?new Uint8Array([this.value]):new Uint8Array}}class vt extends f{format(){return this.target.element_id!==0&&this.target.property!==0?new Uint8Array([i.ActionType.BROADCAST,2,this.target.element_id,this.target.property]):new Uint8Array([i.ActionType.BROADCAST,0])}}class _t extends f{format(){return new Uint8Array([i.ActionType.BREAK,0])}}let Mt=class extends f{fromLow;fromHigh;toLow;toHigh;value;constructor(e,n,r,o,s,a){super(a),this.value=e,this.fromLow=n,this.fromHigh=r,this.toLow=o,this.toHigh=s}format(){return new Uint8Array([i.ActionType.MAP,8,this.value.element_id,this.value.property,this.fromLow,this.fromHigh,this.toLow,this.toHigh,this.target.element_id,this.target.property])}};class Rt extends f{deciseconds;resetable;onComplete;constructor(e,n,r){super(new d(0,0)),this.deciseconds=e,this.resetable=n,this.onComplete=r}format(){const e=C(this.onComplete.map(o=>o.format())),n=new Uint8Array(4+e.length),r=[i.ActionType.TIMER,e.length+2,this.resetable?1:0,this.deciseconds];return n.set(r,0),e.forEach((o,s)=>n.set([o],s+4)),n}}class Lt extends f{left;operator;right;actions;constructor(e,n,r,o){super(new d(0,0)),this.actions=o,this.left=e,this.operator=n,this.right=r}convertValue(e){if(typeof e=="number")return new Uint8Array([i.ActionDataType.NUMBER,1,e]);if(typeof e=="string"){const n=new TextEncoder().encode(e);return new Uint8Array([i.ActionDataType.STRING,n.length,...n])}return e instanceof d?new Uint8Array([i.ActionDataType.ELEMENT,2,e.element_id,e.property]):new Uint8Array}format(){const e=C(this.actions.map(u=>u.format())),n=this.convertValue(this.left),r=this.convertValue(this.right),o=4+n.length+r.length+e.length,s=new Uint8Array(o),a=[i.ActionType.CONDITION,o-2,n.length+r.length+1];return s.set(a,0),s.set([this.operator],3),s.set(n,4),s.set(r,4+n.length),s.set(e,4+n.length+r.length),s}}function Pt(){return{target(t,e){return new d(t,e)},increment(t){return new Bt(t)},decrement(t){return new It(t)},show(t){return new Ct(new d(t,0))},hide(t){return new Nt(new d(t,0))},set(t,e){return new St(t,e)},broadcast(t){return new vt(t||new d(0,0))},timer(t,e,n){return new Rt(t,e,n)},stop(){return new _t(new d(0,0))},condition(t,e,n,r){return new Lt(t,e,n,r)},map(t,e,n,r,o,s){return new Mt(t,e,n,r,o,s)}}}const{flattenUint8Arrays:$t}=A();function kt(t){return{setButton(e,n){const r=$t(n.map(s=>s.format())),o=new Uint8Array(1+r.length);return o.set([e],0),r.forEach((s,a)=>o.set([s],a+1)),new c.Buffer(i.DataType.ACTIONS,t.id,o)}}}function Ht(t){return new Promise((e,n)=>{const r=new Image;r.onload=()=>e(r),r.onerror=n,r.src=t})}function Ot(t){const e=document.createElement("canvas"),n=e.getContext("2d");if(e.width=t.width,e.height=t.height,!n)throw new Error("Failed to get 2D context for image data.");return n.drawImage(t,0,0),n.getImageData(0,0,t.width,t.height)}function Gt(t){const e=[],n=t.data,r=t.width,o=t.height;for(let s=0;s<o;s++)for(let a=0;a<r;a++){const u=(s*r+a)*4,l=n[u],p=n[u+1],y=n[u+2],S=.2126*l+.7152*p+.0722*y;e.push(S<128?0:1)}return{width:r,height:o,pixels:e}}function N(){return{createImage:Ht,getImageData:Ot,imageDataToPixels:Gt}}const{createImage:zt,getImageData:Ft,imageDataToPixels:Vt}=N(),{pixelsToBytes:Wt}=A();function qt(t){return new Promise((e,n)=>{zt(t).then(r=>Ft(r)).then(r=>Vt(r)).then(r=>Wt(r.pixels,r.width)).then(r=>e(r)).catch(r=>n(r))})}function jt(){return{imageToBytes:qt}}function Zt(t,e,n,r,o){return(t-e)/(n-e)*(o-r)+r}function Kt(){return{map:Zt}}exports.useActions=Pt;exports.useBinary=A;exports.useButtons=kt;exports.useConverter=jt;exports.useDisplay=Ut;exports.useGraphics=st;exports.useImage=N;exports.useLoader=at;exports.useMesh=x;exports.useStandeeElements=wt;exports.useStandees=pt;exports.useUtils=Kt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./Objects-Zntmn07s.cjs"),s=require("./enums.cjs"),O=require("./elements.cjs");class ut{data=[];size=0;total=0}const G=7,S=new Map;function lt(t){return S.get(t)}function ft(t){S.delete(t)}function ht(t){const e=t.getUint8(s.Data.CACHE),o=t.getUint16(s.Data.DATA_SIZE,!0),n=t.getUint16(s.Data.POSITION,!0),r=t.byteLength-G;if(!S.has(e)){const i=new ut;i.size=o,S.set(e,i)}const a=S.get(e);for(let i=0;i<r;i++)a.data[i+n]=t.getUint8(G+i);a.total+=r}function dt(t){const e=S.get(t);return e.total==e.size||e.size==0}function gt(){return{add:ht,get:lt,remove:ft,isDone:dt}}const{serviceUuid:pt,handshakeCharUuid:At}=c.useBluetoothSettings(),V=new c.libExports.SyncEvent,{add:yt,get:Dt,remove:wt,isDone:Tt}=gt();function mt(){return new Promise((t,e)=>{c.useBluetooth().subscribe(pt,At,Et).then(()=>t()).catch(o=>e(o))})}function Et(t){console.log(t,t.buffer.byteLength);const e=t.getUint8(s.Data.CACHE);yt(t),Tt(e)&&(V.post(It(e)),wt(e))}function It(t){const e=Dt(t),o=new Map;let n=1;for(;n<e.size;){const r=e.data[n++],a=e.data[n++],i=[];for(let u=0;u<a;u++)i.push(e.data[n++]);o.set(r,i)}return o}function Z(){return{subscribe:mt,onNodes:V}}const{onNodes:bt}=Z(),{onMessageDone:St,onNode:Ct,onNodeLost:Ut}=c.useFloodCharacteristic(),{onPlusButton:_t,onMinButton:Nt,onEitherButton:Rt}=c.useMessageCharacteristic(),{connect:Bt,reconnect:Y,onConnect:xt,onDisconnect:q}=c.useBluetooth(),K=new c.libExports.SyncEvent,X=new c.libExports.SyncEvent;let B=!1,b=!1,C=!1;q.attach(()=>{B&&!b&&(c.useMeshWriter().abortAll(new Error("BLE disconnected")),Lt())});async function Pt(){C=!0,await Bt(c.useBluetoothSettings().options),await z(),B=!0}async function Mt(){C=!0,await Y(),await z(),B=!0}function z(){return Promise.all([Z().subscribe(),c.useMessageCharacteristic().subscribe(),c.useFloodCharacteristic().subscribe()]).then(()=>{})}async function Lt(){const{RECONNECT_DELAY:t,RECONNECT_MAX_RETRIES:e}=c.useBluetoothSettings();b=!0,C=!1;for(let o=1;o<=e;o++){const n=Math.min(t*Math.pow(2,o-1),3e4);if(K.post({attempt:o,maxRetries:e,delay:n}),await vt(n),C){b=!1,C=!1;return}try{await Y(),await z(),b=!1;return}catch(r){console.warn(`Auto-reconnect attempt ${o}/${e} failed:`,r)}}b=!1,X.post()}function vt(t){return new Promise(e=>setTimeout(e,t))}function Ot(){B=!1,b=!1,C=!1}function j(){return{connect:Pt,onConnect:xt,onDisconnect:q,onReconnecting:K,onReconnectFailed:X,reconnect:Mt,onNodes:bt,onNode:Ct,onNodeLost:Ut,onPlusButton:_t,onMinButton:Nt,onEitherButton:Rt,onMessageDone:St,_resetForTesting:Ot}}function kt(t){return{upload:(e,o)=>{const n=new Uint8Array(o.length+1);return n.set([e],0),n.set(o,1),new c.Buffer(s.DataType.UPLOAD,t.id,n)},clear:()=>new c.Buffer(s.DataType.CLEAR_GRAPHICS,t.id)}}class $t{id;buffers;totalPackets=0;onProgress=new c.libExports.SyncEvent;onError=new c.libExports.SyncEvent;constructor(e,o){this.id=o,this.buffers=e,this.buffers.forEach(n=>{this.totalPackets+=n.packets.length})}async send(){const e=this.totalPackets&255,o=this.totalPackets>>8&255,n=new Uint8Array([e,o]),r=new c.Buffer(s.DataType.LOADER,this.id,n),a=this.buffers.length;let i=0;try{await r.send();for(const u of this.buffers)await u.send(),i++,this.onProgress.post({current:i,total:a,percentage:i/a})}catch(u){throw this.onError.post({buffer:i,error:u}),u}}}function zt(t){return{load:e=>new $t(e,t.id)}}const{onNodes:Ht,onNode:Ft,onNodeLost:Gt}=j();Ht.attach(Wt);Ft.attach(Vt);Gt.attach(Zt);const p=[],x=new c.libExports.SyncEvent;function Wt(t){console.log("standees",t);for(const[e,o]of t)J(e),p.find(n=>n.id===e).neighbors=o;x.post(p)}function Vt(t){console.log("nodes",t);const e=t.keys().next().value,o=t.get(e);J(e),p.find(n=>n.id===e).neighbors=o,x.post(p)}function Zt(t){Yt(t),x.post(p)}function J(t){p.some(e=>e.id===t)||p.push(new c.Standee(t))}function Yt(t){const e=p.findIndex(o=>o.id===t);e!==-1&&p.splice(e,1)}function qt(){return{standees:p,onChange:x}}function Kt(t){const e=()=>{let n=Math.floor(Math.random()*256);return o(n)?e():n},o=n=>t.some(r=>r.id===n);return{generate:e}}function Xt(t){return{add:n=>(n.id=Kt(t.elements).generate(),t.elements.push(n),n),remove:n=>{t.elements=t.elements.filter(r=>r.id!==n)}}}function jt(t){return new c.Buffer(s.DataType.CLEAR_SCREEN,t.id)}function Jt(t){let e=new Uint8Array;for(const o of t.elements)o.dirty!==!1&&(o instanceof O.Image&&(e=k(e,Qt(o))),o instanceof O.Rectangle&&(e=k(e,te(o))),o instanceof O.Text&&(e=k(e,ee(o))),o.dirty=!1);return new c.Buffer(s.DataType.ALL,t.id,e)}function k(t,e){const o=new Uint8Array(t.length+e.length);return o.set(t,0),o.set(e,t.length),o}function Qt(t){return new Uint8Array([s.DataType.DRAW_IMAGE,7,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.data])}function te(t){return new Uint8Array([s.DataType.DRAW_RECTANGLE,9,t.id,t.show?1:0,t.x,t.y,t.width,t.height,t.radius,t.fill?1:0,t.colorBlack?0:1])}function ee(t){const e=new TextEncoder().encode(t.text),o=new Uint8Array(9+e.length);return o.set([s.DataType.DRAW_TEXT,7+e.length,t.id,t.show?1:0,t.x,t.y,t.font,t.colorBlack?0:1,t.center?1:0],0),o.set(e,9),o}function ne(){return{clear:jt,draw:Jt}}function oe(t,e){const o=Math.ceil(t.length/e),n=Math.ceil(e/8),r=new Uint8Array(n*o);for(let a=0;a<o;a++)for(let i=0;i<n;i++){let u=0;for(let l=0;l<8;l++){const f=i*8+l,w=a*e+f;f<e&&w<t.length&&t[w]&&(u|=1<<7-l)}r[a*n+i]=Q(u)}return r}function Q(t){return t=(t&240)>>4|(t&15)<<4,t=(t&204)>>2|(t&51)<<2,t=(t&170)>>1|(t&85)<<1,t}function se(t){const e=t.reduce((r,a)=>r+a.length,0),o=new Uint8Array(e);let n=0;for(const r of t)o.set(r,n),n+=r.length;return o}function P(){return{pixelsToBytes:oe,bitswap:Q,flattenUint8Arrays:se}}const{flattenUint8Arrays:tt}=P();class T{element_id;property;constructor(e,o){this.element_id=e,this.property=o}}class A{target;constructor(e){this.target=e}format(){return new Uint8Array}}class re extends A{format(){return new Uint8Array([s.ActionType.INCREMENT,2,this.target.element_id,this.target.property])}}class ae extends A{format(){return new Uint8Array([s.ActionType.DECREMENT,2,this.target.element_id,this.target.property])}}class ie extends A{format(){return new Uint8Array([s.ActionType.SHOW,1,this.target.element_id])}}class ce extends A{format(){return new Uint8Array([s.ActionType.HIDE,1,this.target.element_id])}}let ue=class extends A{value;constructor(e,o){super(e),this.value=o}format(){const e=this.dataType(),o=this.convertValue(e),n=new Uint8Array(5+o.length),r=[s.ActionType.SET,o.length+3,e,this.target.element_id,this.target.property];return n.set(r,0),n.set(o,5),n}dataType(){return typeof this.value==="string"?s.ActionDataType.STRING:s.ActionDataType.NUMBER}convertValue(e){return e===s.ActionDataType.STRING?new TextEncoder().encode(this.value):e===s.ActionDataType.NUMBER?new Uint8Array([this.value]):new Uint8Array}};class le extends A{format(){return this.target.element_id!==0&&this.target.property!==0?new Uint8Array([s.ActionType.BROADCAST,2,this.target.element_id,this.target.property]):new Uint8Array([s.ActionType.BROADCAST,0])}}class fe extends A{format(){return new Uint8Array([s.ActionType.BREAK,0])}}let he=class extends A{fromLow;fromHigh;toLow;toHigh;value;constructor(e,o,n,r,a,i){super(i),this.value=e,this.fromLow=o,this.fromHigh=n,this.toLow=r,this.toHigh=a}format(){return new Uint8Array([s.ActionType.MAP,8,this.value.element_id,this.value.property,this.fromLow,this.fromHigh,this.toLow,this.toHigh,this.target.element_id,this.target.property])}};class de extends A{deciseconds;resetable;onComplete;constructor(e,o,n){super(new T(0,0)),this.deciseconds=e,this.resetable=o,this.onComplete=n}format(){const e=tt(this.onComplete.map(r=>r.format())),o=new Uint8Array(4+e.length),n=[s.ActionType.TIMER,e.length+2,this.resetable?1:0,this.deciseconds];return o.set(n,0),e.forEach((r,a)=>o.set([r],a+4)),o}}class ge extends A{left;operator;right;actions;constructor(e,o,n,r){super(new T(0,0)),this.actions=r,this.left=e,this.operator=o,this.right=n}convertValue(e){if(typeof e=="number")return new Uint8Array([s.ActionDataType.NUMBER,1,e]);if(typeof e=="string"){const o=new TextEncoder().encode(e);return new Uint8Array([s.ActionDataType.STRING,o.length,...o])}return e instanceof T?new Uint8Array([s.ActionDataType.ELEMENT,2,e.element_id,e.property]):new Uint8Array}format(){const e=tt(this.actions.map(u=>u.format())),o=this.convertValue(this.left),n=this.convertValue(this.right),r=4+o.length+n.length+e.length,a=new Uint8Array(r),i=[s.ActionType.CONDITION,r-2,o.length+n.length+1];return a.set(i,0),a.set([this.operator],3),a.set(o,4),a.set(n,4+o.length),a.set(e,4+o.length+n.length),a}}function pe(){return{target(t,e){return new T(t,e)},increment(t){return new re(t)},decrement(t){return new ae(t)},show(t){return new ie(new T(t,0))},hide(t){return new ce(new T(t,0))},set(t,e){return new ue(t,e)},broadcast(t){return new le(t||new T(0,0))},timer(t,e,o){return new de(t,e,o)},stop(){return new fe(new T(0,0))},condition(t,e,o,n){return new ge(t,e,o,n)},map(t,e,o,n,r,a){return new he(t,e,o,n,r,a)}}}const{flattenUint8Arrays:Ae}=P();function ye(t){return{setButton(e,o){const n=Ae(o.map(a=>a.format())),r=new Uint8Array(1+n.length);return r.set([e],0),n.forEach((a,i)=>r.set([a],i+1)),new c.Buffer(s.DataType.ACTIONS,t.id,r)}}}function De(t){return new Promise((e,o)=>{const n=new Image;n.onload=()=>e(n),n.onerror=o,n.src=t})}function we(t){const e=document.createElement("canvas"),o=e.getContext("2d");if(e.width=t.width,e.height=t.height,!o)throw new Error("Failed to get 2D context for image data.");return o.drawImage(t,0,0),o.getImageData(0,0,t.width,t.height)}function Te(t){const e=[],o=t.data,n=t.width,r=t.height;for(let a=0;a<r;a++)for(let i=0;i<n;i++){const u=(a*n+i)*4,l=o[u],f=o[u+1],w=o[u+2],v=.2126*l+.7152*f+.0722*w;e.push(v<128?0:1)}return{width:n,height:r,pixels:e}}function et(){return{createImage:De,getImageData:we,imageDataToPixels:Te}}const{createImage:me,getImageData:Ee,imageDataToPixels:Ie}=et(),{pixelsToBytes:be}=P();function Se(t){return new Promise((e,o)=>{me(t).then(n=>Ee(n)).then(n=>Ie(n)).then(n=>be(n.pixels,n.width)).then(n=>e(n)).catch(n=>o(n))})}function Ce(){return{imageToBytes:Se}}function Ue(t,e,o,n,r){return(t-e)/(o-e)*(r-n)+n}function _e(){return{map:Ue}}let h=!1;const E=[],D=new Map;let _=500,$=!1;const nt=new c.libExports.SyncEvent,N=new c.libExports.SyncEvent,ot=new Map([[s.DataType.UPLOAD,"UPLOAD"],[s.DataType.DRAW_IMAGE,"DRAW_IMAGE"],[s.DataType.DRAW_RECTANGLE,"DRAW_RECTANGLE"],[s.DataType.BUTTON_PRESS,"BUTTON_PRESS"],[s.DataType.DRAW_TEXT,"DRAW_TEXT"],[s.DataType.ALL,"ALL"],[s.DataType.CLEAR_GRAPHICS,"CLEAR_GRAPHICS"],[s.DataType.ACTIONS,"ACTIONS"],[s.DataType.CLEAR_SCREEN,"CLEAR_SCREEN"],[s.DataType.LOADER,"LOADER"]]);function Ne(){h=!0,$||($e(),$=!0)}function Re(){h=!1}function Be(){return h}function xe(t){_=t,st()}function Pe(){return E}function Me(){return Array.from(D.values())}function Le(t){return D.get(t)}function ve(){E.length=0,D.clear()}function Oe(t){const e=t.length>s.Data.TYPE?t[s.Data.TYPE]:0;return{cacheId:t[s.Data.CACHE],targetId:t[s.Data.ID],ttl:t[s.Data.TTL],position:t.length>4?t[s.Data.POSITION]|t[s.Data.POSITION+1]<<8:0,dataSize:t.length>6?t[s.Data.DATA_SIZE]|t[s.Data.DATA_SIZE+1]<<8:0,typeName:ot.get(e)??`UNKNOWN(${e})`,typeValue:e,payloadBytes:Math.max(0,t.length-s.Data.DATA),raw:Array.from(t)}}function g(t,e){if(!h)return;const o={type:t,timestamp:Date.now(),data:e};E.push(o),st(),nt.post(o)}function st(){E.length>_&&E.splice(0,E.length-_)}function ke(t,e){if(!h)return;const o=D.get(t);o&&(Object.assign(o,e),o.completedAt&&o.queuedAt&&(o.latencyMs=o.completedAt-o.queuedAt),N.post(o))}function $e(){const t=c.useMeshWriter(),e=c.useFloodCharacteristic(),o=c.useBluetooth();t.onMessageQueued.attach(n=>{h&&(D.set(n.cacheId,{cacheId:n.cacheId,queuedAt:n.timestamp,firstPacketAt:null,completedAt:null,latencyMs:null,retransmitCount:0,timeoutCount:0,packetsSent:0,totalPackets:n.packetCount}),g("message_queued",{cacheId:n.cacheId,typeName:ot.get(n.type)??`UNKNOWN(${n.type})`,targetId:n.targetId,packetCount:n.packetCount,totalBytes:n.totalBytes}))}),t.onPacketSent.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.packetsSent++,r.firstPacketAt===null&&(r.firstPacketAt=n.timestamp)),g("packet_sent",{cacheId:n.cacheId,position:n.position,dataSize:n.dataSize,batchIndex:n.batchIndex})}),t.onRetransmit.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.retransmitCount++,N.post(r)),g("retransmit",{cacheId:n.cacheId,packetCount:n.packetCount,reason:n.reason})}),t.onTimeout.attach(n=>{if(!h)return;const r=D.get(n.cacheId);r&&(r.timeoutCount++,N.post(r)),g("timeout",{cacheId:n.cacheId,retryCount:n.retryCount,maxRetries:n.maxRetries,willRetry:n.willRetry})}),t.onMessageComplete.attach(n=>{h&&(ke(n.cacheId,{completedAt:n.timestamp}),g("message_complete",{cacheId:n.cacheId}))}),e.onMessageDone.attach(n=>{h&&g("message_done",{cacheId:n})}),e.onReceivedPackets.attach(n=>{h&&g("received_packets",{incompleteCaches:n.filter(r=>r!==0)})}),e.onNode.attach(n=>{if(h)for(const[r,a]of n)g("node_discovered",{nodeId:r,neighbors:a})}),e.onNodeLost.attach(n=>{h&&g("node_lost",{nodeId:n})}),o.onConnect.attach(()=>{h&&g("connected",{})}),o.onDisconnect.attach(()=>{h&&g("disconnected",{})})}function ze(){h=!1,E.length=0,D.clear(),$=!1,_=500}function He(){return{enable:Ne,disable:Re,isEnabled:Be,clear:ve,getLog:Pe,getTimings:Me,getTimingForCache:Le,setMaxLogSize:xe,decodePacket:Oe,onDebugEvent:nt,onTimingUpdate:N,_resetForTesting:ze}}const{messageCharUuid:rt,messageFloodCharUuid:M,handshakeCharUuid:at,BATCH_SIZE:Fe,PACKET_SIZE:R}=c.useBluetoothSettings();let H=!1,U=!1,L=!0,d={standees:[]};const y=new Map,m=new Map;let I=0;const W=7;function Ge(t){d={responseDelay:10,dropRate:0,partialDelivery:!1,...t},H=!0,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(Qe)}function it(){H=!1,U=!1,L=!0,y.clear(),m.clear(),I=0,c.setBluetoothProvider(null)}function We(){return H}function Ve(){return d}function Ze(t){d={...d,...t}}function Ye(t,e,o){const n=y.get(rt);if(!n)return;const r=e==="plus"?s.Button.PLUS:e==="minus"?s.Button.MINUS:s.Button.EITHER,a=o?new TextEncoder().encode(o):new Uint8Array(0),i=s.Data.DATA+1+a.length,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,t),l.setUint8(s.Data.TYPE,s.DataType.BUTTON_PRESS),l.setUint8(s.Data.DATA,r);for(let f=0;f<a.length;f++)l.setUint8(s.Data.DATA+1+f,a[f]);n(l)}function qe(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.ID,t),n.setUint8(s.Data.TYPE,s.FloodTypes.LOST_NODE),n.setUint8(s.Data.DATA,t),e(n)}function Ke(t){const e=y.get(M);e&&e(nn(t))}function Xe(){U=!1,y.clear(),m.clear(),I=0,c.useBluetooth().onDisconnect.post()}function je(t){L=t}function Je(){it()}const Qe={async connect(t){U=!0,c.useBluetooth().onConnect.post()},async reconnect(){if(!L)throw new Error("No devices found");U=!0,c.useBluetooth().onConnect.post()},async readCharacteristic(t,e){},async writeCharacteristic(t,e,o){if(!U||e!==rt||d.dropRate!==void 0&&d.dropRate>0&&Math.random()<d.dropRate)return;const n=o[s.Data.CACHE],r=o[s.Data.POSITION]|o[s.Data.POSITION+1]<<8,a=o[s.Data.DATA_SIZE]|o[s.Data.DATA_SIZE+1]<<8;m.has(n)||m.set(n,{received:new Set,dataSize:a});const i=m.get(n);i.received.add(r),I++;const u=Math.ceil(a/R);if(i.received.size>=u){const f=d.responseDelay??10;setTimeout(()=>{tn(n)},f);return}if(d.partialDelivery&&I>=Fe){I=0;const f=d.responseDelay??10;setTimeout(()=>{const w=[];for(const[v,F]of m){const ct=Math.ceil(F.dataSize/R);F.received.size<ct&&w.push(v)}w.length>0&&en(w)},f)}},async subscribe(t,e,o){y.set(e,o),e===at&&on()}};function tn(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+1),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.MESSAGE_DONE),n.setUint8(s.Data.DATA,t),m.delete(t),I=0,e(n)}function en(t){const e=y.get(M);if(!e)return;const o=new ArrayBuffer(s.Data.DATA+t.length),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.DATA_FEEDBACK);for(let r=0;r<t.length;r++)n.setUint8(s.Data.DATA+r,t[r]);e(n)}function nn(t){const e=s.Data.DATA+1+t.neighbors.length,o=new ArrayBuffer(e),n=new DataView(o);n.setUint8(s.Data.TYPE,s.FloodTypes.NODES),n.setUint8(s.Data.DATA,t.id);for(let r=0;r<t.neighbors.length;r++)n.setUint8(s.Data.DATA+1+r,t.neighbors[r]);return n}function on(){const t=y.get(at);if(!t||d.standees.length===0)return;const e=[d.standees.length];for(const r of d.standees)e.push(r.id,r.neighbors.length,...r.neighbors);const o=200,n=e.length;for(let r=0;r<n;r+=R){const a=Math.min(R,n-r),i=W+a,u=new ArrayBuffer(i),l=new DataView(u);l.setUint8(s.Data.ID,0),l.setUint8(s.Data.CACHE,o),l.setUint8(s.Data.TTL,10),l.setUint16(s.Data.POSITION,r,!0),l.setUint16(s.Data.DATA_SIZE,n,!0);for(let f=0;f<a;f++)l.setUint8(W+f,e[r+f]);t(l)}}function sn(){return{activate:Ge,deactivate:it,isActive:We,getConfig:Ve,updateConfig:Ze,pressButton:Ye,simulateNodeLost:qe,simulateNodeJoin:Ke,disconnect:Xe,reconnectable:je,_resetForTesting:Je}}exports.useActions=pe;exports.useBinary=P;exports.useButtons=ye;exports.useConverter=Ce;exports.useDebug=He;exports.useDisplay=ne;exports.useGraphics=kt;exports.useImage=et;exports.useLoader=zt;exports.useMesh=j;exports.useMockBluetooth=sn;exports.useStandeeElements=Xt;exports.useStandees=qt;exports.useUtils=_e;