"use strict";var T=Object.create;var d=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var o=(t,e)=>d(t,"name",{value:e,configurable:!0});var $=(t,e)=>()=>(t&&(e=t(t=0)),e);var _=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),A=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},y=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of k(e))!F.call(t,n)&&n!==r&&d(t,n,{get:()=>e[n],enumerable:!(i=R(e,n))||i.enumerable});return t};var h=(t,e,r)=>(r=t!=null?T(v(t)):{},y(e||!t||!t.__esModule?d(r,"default",{value:t,enumerable:!0}):r,t)),U=t=>y(d({},"__esModule",{value:!0}),t);var l=$(()=>{"use strict"});var g=_((V,S)=>{"use strict";l();S.exports={}});var M={};A(M,{default:()=>u,slipDecode:()=>N,slipEncode:()=>D});module.exports=U(M);l();var s=h(require("lodash")),m=h(g()),x=require("web-serial-polyfill");l();var W=h(require("lodash"));async function b(t){let e;await new Promise(r=>{e=setTimeout(r,t)}),e!==void 0&&(clearTimeout(e),e=void 0)}o(b,"sleep");var C=[{usbVendorId:26728,usbProductId:34438},{usbVendorId:6421,usbProductId:21023}];function I(t){return s.default.toUpper(`000${t.toString(16)}`.slice(-4))}o(I,"u16ToHex");var u=class{static{o(this,"WebserialAdapter")}#e=!1;#t=!1;name="adapter";port;#r;#i;#n;#o;ultra;constructor(){let e=globalThis?.navigator??{};this.#n=globalThis?.TransformStream??m.TransformStream,this.#o=globalThis?.WritableStream??m.WritableStream,this.#i=e.serial??("usb"in e?x.serial:null),this.#r=r=>{this.ultra?.emitter.emit("error",s.default.set(new Error(r.message),"originalError",r))}}#a(e,...r){this.ultra?.emitter.emit("debug","webserial",e,...r)}async install(e,r){let i=this.ultra=e.ultra,n=e.Buffer;s.default.isNil(i.$adapter)||await i.disconnect(new Error("adapter replaced"));let a={isSupported:o(()=>!s.default.isNil(this.#i),"isSupported")};return i.addHook("connect",async(B,f)=>{if(i.$adapter!==a)return await f();try{if(!a.isSupported())throw new Error("WebSerial not supported");if(this.port=await this.#i.requestPort({filters:C}),s.default.isNil(this.port))throw new Error("user canceled");let p=await this.port.getInfo();for(this.#a(`port selected, usbVendorId = 0x${I(p.usbVendorId)}, usbProductId = 0x${I(p.usbProductId)}`),this.#e=s.default.isMatch(p,C[1]),await this.port.open({baudRate:115200});s.default.isNil(this.port.readable)||s.default.isNil(this.port.writable);)await b(10);return this.#t=!0,this.port.addEventListener("disconnect",()=>{i.disconnect(new Error("Webserial disconnect"))}),this.#e?i.port={isOpen:o(()=>this.#t,"isOpen"),isDfu:o(()=>this.#e,"isDfu"),isSlip:o(()=>!0,"isSlip"),readable:this.port.readable.pipeThrough(new this.#n(new w(n))),writable:new this.#o({write:o(async P=>{let c=this.port?.writable.getWriter();if(s.default.isNil(c))throw new Error("Failed to getWriter(). Did you remember to use adapter plugin?");await c.write(D(P,n)),c.releaseLock()},"write")})}:i.port=s.default.merge(this.port,{isOpen:o(()=>this.#t,"isOpen"),isDfu:o(()=>this.#e,"isDfu")}),await f()}catch(p){throw this.#r(p),p}}),i.addHook("disconnect",async(B,f)=>{if(i.$adapter!==a||s.default.isNil(this.port))return await f();await f().catch(this.#r),await this.port.close().catch(this.#r),this.#t=!1,this.#e=!1,delete this.port}),a}};((globalThis??{}).ChameleonUltraJS??{}).WebserialAdapter=u;var w=class{static{o(this,"SlipDecodeTransformer")}#e=[];#t;constructor(e){this.#t=e}transform(e,r){this.#t.isBuffer(e)||(e=this.#t.fromView(e)),this.#e.push(e);let i=this.#t.concat(this.#e.splice(0,this.#e.length));try{for(;i.length>0;){let n=i.indexOf(192);if(n<0)break;let a=N(i.subarray(0,n+1));a.length>0&&r.enqueue(a),i=i.subarray(n+1)}}finally{i.length>0&&this.#e.push(i)}}};function D(t,e){let r=t.length;for(let a of t)(a===192||a===219)&&r++;let i=e.alloc(r+1),n=0;for(let a of t)a===192?(i[n++]=219,i[n++]=220):a===219?(i[n++]=219,i[n++]=221):i[n++]=a;return i[n]=192,i}o(D,"slipEncode");function N(t){let e=0;for(let r=0;r<t.length;r++)if(t[r]===219){if(++r>=t.length)break;t[r]===220?t[e++]=192:t[r]===221&&(t[e++]=219)}else{if(t[r]===192)break;t[e++]=t[r]}return t.slice(0,e)}o(N,"slipDecode");0&&(module.exports={slipDecode,slipEncode});
//# sourceMappingURL=WebserialAdapter.js.map