"use strict";var S=Object.create;var d=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var l=(t,e)=>d(t,"name",{value:e,configurable:!0});var v=(t,e)=>{for(var a in e)d(t,a,{get:e[a],enumerable:!0})},w=(t,e,a,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of g(e))!I.call(t,i)&&i!==a&&d(t,i,{get:()=>e[i],enumerable:!(r=b(e,i))||r.enumerable});return t};var R=(t,e,a)=>(a=t!=null?S(C(t)):{},w(e||!t||!t.__esModule?d(a,"default",{value:t,enumerable:!0}):a,t)),N=t=>w(d({},"__esModule",{value:!0}),t);var A={};v(A,{default:()=>p});module.exports=N(A);var n=R(require("lodash")),h=require("serialport"),f=require("stream");async function $(){let t=n.default.find(await h.SerialPort.list(),{vendorId:"6868",productId:"8686"});if(n.default.isNil(t))throw new Error("device not found");return t?.path}l($,"findDevicePath");var p=class{static{l(this,"SerialPortAdapter")}duplex;name="adapter";ultra;#t(e,...a){this.ultra?.emitter.emit("debug","serial",e,...a)}async install(e,a={}){let r=this.ultra=e.ultra;n.default.isNil(r.$adapter)||await r.disconnect(new Error("adapter replaced"));let i={isSupported:l(()=>!n.default.isNil(h.SerialPort),"isSupported")};return r.addHook("connect",async(P,s)=>{if(r.$adapter!==i)return await s();try{if(!i.isSupported())throw new Error("SerialPort not supported");let o=a?.baudRate??115200,u=a?.path??await $();return this.duplex=await new Promise((c,x)=>{let y=new h.SerialPort({baudRate:o,path:u},m=>{n.default.isNil(m)?c(y):x(m)})}),this.duplex?.once("close",()=>{r.disconnect(new Error("SerialPort closed"))}),this.#t(`port connected, path = ${u}, baudRate = ${o}`),r.port=n.default.merge(f.Duplex.toWeb(this.duplex),{isOpen:l(()=>this.duplex?.isOpen??!1,"isOpen"),isDfu:l(()=>!1,"isDfu")}),await s()}catch(o){throw this.#t(o),o}}),r.addHook("disconnect",async(P,s)=>{if(r.$adapter!==i||n.default.isNil(this.duplex))return await s();await s(),await new Promise((o,u)=>{this.duplex?.close(c=>{n.default.isNil(c)?o():u(c)})}),delete this.duplex}),i}};((globalThis??{}).ChameleonUltraJS??{}).SerialPortAdapter=p;
//# sourceMappingURL=SerialPortAdapter.js.map