/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@simconnect.js/api@1.0.7/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{c(i.next(e))}catch(e){o(e)}}function a(e){try{c(i.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}c((i=i.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SIMCONNECT_API=exports.SIM_NOT_CONNECTED=exports.SystemEvents=exports.SimVariables=void 0;const simconnect_js_1=require("simconnect.js"),index_1=require("./simvars/index"),index_2=require("./system-events/index"),exceptions_1=require("./utils/exceptions");exports.SimVariables=index_1.SimVars,exports.SystemEvents=Object.assign({},index_2.SystemEvents),exports.SIM_NOT_CONNECTED="Not connected to the simulator.";const codeSafe=e=>e.replaceAll(" ","_");class SIMCONNECT_API{constructor(e="SimConnect API"){this.appName=e,this.simulatorName,this.eventListeners={},this.id=1,this.reserved=new Set}connect(e){var t,n,i;return __awaiter(this,void 0,void 0,(function*(){null!==(t=e.autoReconnect)&&void 0!==t||(e.autoReconnect=!1),null!==(n=e.retries)&&void 0!==n||(e.retries=0),null!==(i=e.retryInterval)&&void 0!==i||(e.retryInterval=2);const{host:s,port:o}=e;try{const t=this.remote=s?{host:s,port:null!=o?o:500}:void 0,{handle:n,recvOpen:i}=yield(0,simconnect_js_1.open)(this.appName,simconnect_js_1.Protocol.FSX_SP2,t);n||(this.connected=!1,e.onException("No connection handle to the simulator.")),this.handle=n,this.connected=!0,this.simulatorName=i.applicationName,n.on("event",(e=>this.handleSystemEvent(e))),n.on("close",(()=>e.autoReconnect&&this.connect(e))),n.on("exception",(t=>{this.connected=!1,e.onException(exceptions_1.SIMCONNECT_EXCEPTION[t.exception])})),e.onConnect(n,i)}catch(t){e.retries?(e.retries--,e.onRetry(e.retries,e.retryInterval),setTimeout((()=>this.connect(e)),1e3*e.retryInterval)):(this.connected=!1,e.onException("No connection to the simulator."))}}))}nextId(){this.id>900&&(this.id=0);let e=this.id++;for(;this.reserved.has(e);)e=this.id++;return this.reserved.add(e),e}releaseId(e){this.reserved.delete(e)}addEventListener(e,t){const{eventListeners:n}=this;if(n[e]){n[e].handlers.push(t);const{data:i}=n[e];i&&t(i)}else{const i=this.nextId();this.handle.subscribeToSystemEvent(i,e),n[e]={eventID:i,eventName:e,data:void 0,handlers:[t]},n[i]=n[e]}}removeEventListener(e,t){const{eventListeners:n}=this,i=n[e],s=i.handlers.findIndex((e=>e===t));s>-1&&i.handlers.splice(s,1)}handleSystemEvent(e){const{clientEventId:t,data:n}=e,i=this.eventListeners[t];if(!i)return console.log(`handling data for id ${t} without an event handler entry??`);i.data=n,i.handlers.forEach((e=>e(n)))}on(e,t){if(!this.connected)throw new Error(exports.SIM_NOT_CONNECTED);if(!e)return console.error("on() called without an event definition"),void console.trace();const{name:n}=e;return this.addEventListener(n,t),()=>this.off(n,t)}off(e,t){this.removeEventListener(e,t)}trigger(e,t){if(!this.connected)throw new Error(exports.SIM_NOT_CONNECTED);const{handle:n}=this,i=this.nextId();n.mapClientEventToSimEvent(i,e);try{n.transmitClientEvent(simconnect_js_1.SimConnectConstants.OBJECT_ID_USER,i,t,1,16)}catch(e){console.warn(e)}}addDataDefinitions(e,t,n){const{handle:i}=this;t.forEach(((t,s)=>{const o=n[s];if(void 0===o)throw i.clearDataDefinition(e),this.releaseId(e),new Error(`Cannot get SimVar: "${t}" unknown.`);i.addToDataDefinition(e,t,o.units,o.data_type,0,simconnect_js_1.SimConnectConstants.UNUSED)}))}generateGetPromise(e,t,n,i){const{handle:s}=this;return new Promise((o=>{const r=({requestID:a,data:c})=>{if(a===t){s.off("simObjectData",r),s.clearDataDefinition(e);const t={};n.forEach(((e,n)=>{t[codeSafe(e)]=i[n].read(c)})),o(t),this.releaseId(e)}};s.on("simObjectData",r),s.requestDataOnSimObject(t,e,simconnect_js_1.SimConnectConstants.OBJECT_ID_USER,simconnect_js_1.SimConnectPeriod.ONCE,0,0,0,0)}))}get(e){if(!this.connected)throw new Error(exports.SIM_NOT_CONNECTED);const t=this.nextId(),n=t,i=(e=e.map((e=>e.replaceAll("_"," ")))).map((e=>index_1.SimVars[e]));return this.addDataDefinitions(t,e,i),this.generateGetPromise(t,n,e,i)}set(e,t){if(!this.connected)throw new Error(exports.SIM_NOT_CONNECTED);const{handle:n}=this;e=e.replaceAll("_"," "),t==parseFloat(t)&&(t=parseFloat(t));const i=this.nextId(),s=index_1.SimVars[e];if(void 0===s)throw this.releaseId(i),new Error(`Cannot set SimVar: "${e}" unknown.`);const o={buffer:s.write(new simconnect_js_1.RawBuffer(100),t),arrayCount:0,tagged:!1};n.addToDataDefinition(i,e,s.units,s.data_type),n.setDataOnSimObject(i,simconnect_js_1.SimConnectConstants.OBJECT_ID_USER,o),setTimeout((()=>{this.releaseId(i),n.clearDataDefinition(i)}),500)}schedule(e,t,n,i){if(!this.connected)throw new Error(exports.SIM_NOT_CONNECTED);let s=i;const o=()=>__awaiter(this,void 0,void 0,(function*(){e(yield this.get(n)),s&&setTimeout(o,t)}));return o(),{start:()=>{s=!0,o()},stop:()=>s=!1}}}exports.SIMCONNECT_API=SIMCONNECT_API;
//# sourceMappingURL=/sm/7167ae6a0a32ad1e4c9a994056ff66d1793d34590a18035e8eaedba6898b1381.map