try{let e=typeof window<`u`?window:typeof global<`u`?global:typeof globalThis<`u`?globalThis:typeof self<`u`?self:{},t=new e.Error().stack;t&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[t]=`e5d94e05-e05f-46bc-ad77-7f50ef159040`,e._sentryDebugIdIdentifier=`sentry-dbid-e5d94e05-e05f-46bc-ad77-7f50ef159040`)}catch{}import"./rolldown-runtime-nx40z1zs.js";import{Jd as e,yg as t}from"./entry.client-mpghtlrl.js";var n=class{PAIR_POLLING_INTERVAL=1e3;PAIR_POLLING_TIMEOUT=10*1e3;PAIR_POLLING_MAX_TRIES=Math.floor(this.PAIR_POLLING_TIMEOUT/this.PAIR_POLLING_INTERVAL);SCAN_POLLING_INTERVAL=1e3;logger=new t(`KeplerBluetoothAdaptor`);scanPollTimer=null;pairPollTimer=null;pairTries=0;isScanning=!1;async deviceScan(t,n,r){this.logger.info(`Starting Bluetooth scan`),r.addEventListener(`abort`,async()=>{this.logger.info(`Scanning aborted by caller`),this.isScanning=!1,this.scanPollTimer&&=(clearTimeout(this.scanPollTimer),null),await e(`stopScan`)}),this.isScanning=!0;let i=await e(`scan`);if(i.error)n(Error(`Error starting scan: ${i.error}`));else{let n=async()=>{if(r.aborted){this.logger.info(`Device scan aborted by caller`);return}this.logger.debug(`Polling for devices`);let i=await e(`devicePoll`);this.isScanning?(Array.isArray(i.devices)?(this.logger.debug(`Found ${i.devices.length} devices`),t(i.devices.map(e=>({name:e.name,macAddress:e.identity})))):this.logger.info(`No devices found`),this.scanPollTimer=setTimeout(n,this.SCAN_POLLING_INTERVAL)):this.logger.info(`Aborting in-progress device poll`)};n()}}async pairDevice(t,n,r,i){this.logger.info(`Pairing with device`),i.addEventListener(`abort`,()=>{this.pairPollTimer&&=(this.logger.info(`Pairing aborted by caller`),clearTimeout(this.pairPollTimer),null)}),this.pairTries=0;let a=await e(`pair`,{id:t});if(a.error)r(Error(`Error during pairing: ${a.error}`));else{let t=async()=>{if(i.aborted){this.logger.info(`Pairing aborted by caller`);return}switch(this.logger.info(`Polling for pairing status`),this.pairTries++,(await e(`pairPoll`)).status){case`pairing`:this.pairTries<this.PAIR_POLLING_MAX_TRIES?(this.logger.debug(`Pairing attempt ${this.pairTries}`),setTimeout(t,this.PAIR_POLLING_INTERVAL)):r(Error(`Pairing timed out`));break;case`paired`:this.logger.info(`Pairing successful`),this.pairPollTimer&&=(clearTimeout(this.pairPollTimer),null),n();break;case`pairingAborted`:r(Error(`Pairing aborted by the bluetooth system`));break;default:this.pairTries<this.PAIR_POLLING_MAX_TRIES?(this.logger.debug(`Pairing attempt ${this.pairTries} with unknown status`),this.pairPollTimer=setTimeout(t,this.PAIR_POLLING_INTERVAL)):r(Error(`Pairing timed out with unknown status`));break}};this.pairPollTimer=setTimeout(t,this.PAIR_POLLING_INTERVAL)}}};export{n as KeplerBluetoothAdaptor};