"use strict";(()=>{function h(e,n,t,i){if(t==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof n=="function"?e!==n||!i:!n.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return t==="m"?i:t==="a"?i.call(e):i?i.value:n.get(e)}function v(e,n,t,i,r){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!r)throw new TypeError("Private accessor was defined without a setter");if(typeof n=="function"?e!==n||!r:!n.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?r.call(e,t):r?r.value=t:n.set(e,t),t}var A,E,_,M,T,F,S,V="__TAURI_TO_IPC_KEY__";function U(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}var q=class{constructor(n){A.add(this),E.set(this,void 0),_.set(this,0),M.set(this,[]),T.set(this,void 0),v(this,E,n||(()=>{}),"f"),this.id=U(t=>{let i=t.index;if("end"in t){i==h(this,_,"f")?this.cleanupCallback():v(this,T,i,"f");return}let r=t.message;if(i==h(this,_,"f")){for(h(this,A,"m",F).call(this,r),v(this,_,h(this,_,"f")+1,"f");h(this,_,"f")in h(this,M,"f");){let s=h(this,M,"f")[h(this,_,"f")];h(this,A,"m",F).call(this,s),delete h(this,M,"f")[h(this,_,"f")],v(this,_,h(this,_,"f")+1,"f")}h(this,_,"f")===h(this,T,"f")&&this.cleanupCallback()}else h(this,M,"f")[i]=r})}cleanupCallback(){window.__TAURI_INTERNALS__.unregisterCallback(this.id)}set onmessage(n){v(this,E,n,"f")}get onmessage(){return h(this,E,"f")}[(E=new WeakMap,_=new WeakMap,M=new WeakMap,T=new WeakMap,A=new WeakSet,F=function(t){try{h(this,E,"f").call(this,t)}catch(i){queueMicrotask(()=>{throw i})}},V)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[V]()}};async function f(e,n={},t){return window.__TAURI_INTERNALS__.invoke(e,n,t)}var K=class{get rid(){return h(this,S,"f")}constructor(n){S.set(this,void 0),v(this,S,n,"f")}async close(){return f("plugin:resources|close",{rid:this.rid})}async[(S=new WeakMap,Symbol.asyncDispose)](){await this.close()}};var Y;(function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WINDOW_SUSPENDED="tauri://suspended",e.WINDOW_RESUMED="tauri://resumed",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"})(Y||(Y={}));async function oe(e,n){window.__TAURI_EVENT_PLUGIN_INTERNALS__.unregisterListener(e,n),await f("plugin:event|unlisten",{event:e,eventId:n})}async function B(e,n,t){var i;let r=typeof t?.target=="string"?{kind:"AnyLabel",label:t.target}:(i=t?.target)!==null&&i!==void 0?i:{kind:"Any"};return f("plugin:event|listen",{event:e,target:r,handler:U(n)}).then(s=>async()=>oe(e,s))}var D={openInput:e=>f("plugin:midi|open_input",{id:e}),closeInput:e=>f("plugin:midi|close_input",{id:e}),openOutput:e=>f("plugin:midi|open_output",{id:e}),closeOutput:e=>f("plugin:midi|close_output",{id:e}),outputSend:(e,n,t)=>f("plugin:midi|output_send",{id:e,msg:n,timestamp:t})},z={midiMessage:{listen:e=>B("plugin:midi:midi-message",e)},stateChange:{listen:e=>B("plugin:midi:state-change",e)}},L="tauri-plugin-midi/frame-bridge",ae=50,ce=20,G={openInput:D.openInput,closeInput:D.closeInput,openOutput:D.openOutput,closeOutput:D.closeOutput,outputSend:D.outputSend},w,j,$=null;function Q(e,n){return e instanceof Error?e:new DOMException(String(e),n)}var J,X=new Promise((e,n)=>{let t=setTimeout(()=>n(new DOMException("Failed to initialise WebMIDI","NotSupportedError")),1e4);J=()=>{clearTimeout(t),e(void 0)}});X.catch(()=>{});var ee=[],te=[],R=new Set,ue=new FinalizationRegistry(e=>R.delete(e));function le(e){let n=new WeakRef(e);R.add(n),ue.register(e,n)}function ne(e){for(let n of R){let t=n.deref();t?e(t):R.delete(n)}}function Z(e,n){ee=e,te=n,J(),ne(t=>t.__tauri_statechange(e,n)),$?.stateChange(e,n)}function de(e){let[n,t,i]=e;ne(r=>{let s=r.inputs.get(n);if(!s||s.type!=="input"||s.connection!=="open")return;let o=parseInt(t),a=new N("midimessage",{data:new Uint8Array(i)});Object.defineProperty(a,"timeStamp",{value:o}),Object.defineProperty(a,"receivedTime",{value:o}),s.dispatchEvent(a)}),$?.midiMessage(e)}var k=class extends Event{constructor(n,t){super(n,t),this.port=t?.port||null}},C=class extends EventTarget{constructor(){super();this.sysexEnabled=!0;this.inputs=new Map;this.outputs=new Map;this.portMap=new Map;this.onstatechange=null;this.__tauri_statechange(ee,te),le(this),this.addEventListener("statechange",t=>{if(this.onstatechange)return this.onstatechange(t)})}addEventListener(t,i,r){super.addEventListener(t,i,r)}removeEventListener(t,i,r){super.removeEventListener(t,i,r)}__tauri_get_or_create_port(t,i,r){let s=`${t}:${i}`,o=this.portMap.get(s);return o||(o=t==="input"?new W(this,i,r):new x(this,i,r),this.portMap.set(s,o)),o}__tauri_statechange(t,i){let r=[];for(let[s,o]of this.inputs)t.find(([a,m])=>a===s)||(this.inputs.delete(s),o.__tauri_set_device_state("disconnected")||r.push(o));for(let[s,o]of this.outputs)i.find(([a,m])=>a===s)||(this.outputs.delete(s),o.__tauri_set_device_state("disconnected")||r.push(o));for(let[s,o]of t){if(this.inputs.has(s))continue;let a=this.__tauri_get_or_create_port("input",s,o);this.inputs.set(s,a),a.__tauri_set_device_state("connected")||r.push(a)}for(let[s,o]of i){if(this.outputs.has(s))continue;let a=this.__tauri_get_or_create_port("output",s,o);this.outputs.set(s,a),a.__tauri_set_device_state("connected")||r.push(a)}setTimeout(()=>r.forEach(s=>s.__tauri_dispatch_statechange()),0)}},b=class extends EventTarget{constructor(t,i,r,s){super();this.access=t;this.identifier=i;this.name=r;this.type=s;this.connection="closed";this.manufacturer=null;this.onstatechange=null;this.state="disconnected";this.version=null;this.__tauri_generation=0;this.id=i,this.name=r,this.addEventListener("statechange",o=>{if(this.onstatechange)return this.onstatechange(o)})}__tauri_dispatch_statechange(){this.dispatchEvent(new k("statechange",{port:this})),this.access.dispatchEvent(new k("statechange",{port:this}))}__tauri_set_device_state(t){if(t==="disconnected")return this.__tauri_generation++,this.connection=this.connection==="open"||this.connection==="pending"?"pending":"closed",this.state="disconnected",!1;if(this.connection==="pending"){this.state="connected";let i=this.open(),r=this.__tauri_generation;return i.catch(()=>{r===this.__tauri_generation&&this.connection==="pending"&&(this.connection="closed",this.__tauri_dispatch_statechange())}),!0}return this.state="connected",!1}async open(){let t=++this.__tauri_generation;if(this.connection==="open")return this;if(this.state==="disconnected")return this.connection==="pending"?this:(this.connection="pending",this.__tauri_dispatch_statechange(),this);try{this.type==="input"?await w("openInput",[this.id]):await w("openOutput",[this.id])}catch(r){throw Q(r,"NotAllowedError")}if(t!==this.__tauri_generation)return this;let i=this.state==="disconnected"?"pending":"open";return this.connection!==i&&(this.connection=i,this.__tauri_dispatch_statechange()),this}async close(){let t=++this.__tauri_generation;if(this.connection==="closed")return this;try{this.type==="input"?await w("closeInput",[this.id]):await w("closeOutput",[this.id])}catch(i){throw Q(i,"NotAllowedError")}return t!==this.__tauri_generation?this:(this.connection="closed",this.__tauri_dispatch_statechange(),this)}},N=class extends Event{constructor(n,t){super(n,t),this.data=t?.data??new Uint8Array(new ArrayBuffer(0))}},W=class extends b{constructor(t,i,r){super(t,i,r,"input");this._onmidimessage=null;this.addEventListener("midimessage",s=>{if(this.onmidimessage)return this.onmidimessage(s)})}get onmidimessage(){return this._onmidimessage}set onmidimessage(t){this._onmidimessage=t,t&&this.connection!=="open"&&this.open()}addEventListener(t,i,r){super.addEventListener(t,i,r),t==="midimessage"&&i&&this.state==="connected"&&this.connection!=="open"&&this.open()}},x=class extends b{constructor(n,t,i){super(n,t,i,"output")}send(n,t){if(this.state==="disconnected")throw new DOMException("MIDIOutput is disconnected","InvalidStateError");if(!Array.isArray(n)&&!ArrayBuffer.isView(n))throw new TypeError("MIDIOutput.send() expects an array of MIDI bytes");let i=this.state==="connected"&&this.connection==="closed"?this.open():Promise.resolve(),r=t?Math.trunc(performance.timeOrigin+t):null;i.then(()=>w("outputSend",[this.id,n,r?r.toString():null])).catch(s=>{console.error("Failed to send MIDI message:",s)})}};function pe(){globalThis.MIDIConnectionEvent=k,globalThis.MIDIAccess=C,globalThis.MIDIPort=b,globalThis.MIDIMessageEvent=N,globalThis.MIDIInput=W,globalThis.MIDIOutput=x,navigator.requestMIDIAccess=()=>X.then(()=>new C)}function ie(){let e=new Uint8Array(16),n=globalThis.crypto;if(n?.getRandomValues)n.getRandomValues(e);else for(let t=0;t<e.length;t++)e[t]=Math.floor(Math.random()*256);return Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}function se(e){return typeof e=="string"&&e!==""&&e!=="null"}function he(){let e=globalThis,n=e.location.origin,t=se(n),i=new Map,r=null,s=(g,u,p)=>{try{g.postMessage({[L]:!0,...p},u)}catch{i.delete(g)}},o=g=>g?.closed===!0?(i.delete(g),!0):!1,a=(g,u)=>{for(let[p,l]of i)o(p)||s(p,l.origin,{kind:"event",token:l.token,name:g,payload:u})},m=g=>{let u=g?.data;if(!u||typeof u!="object"||u[L]!==!0||!t||g.origin!==n)return;let p=g.source;if(p){if(u.kind==="hello"){if(typeof u.client!="string")return;let l=i.get(p);l||(l={token:ie(),client:u.client,origin:n},i.set(p,l)),s(p,l.origin,{kind:"welcome",token:l.token,client:u.client,origin:n}),r&&s(p,l.origin,{kind:"event",token:l.token,name:"state-change",payload:r});return}if(u.kind==="bye"){let l=i.get(p);l&&(u.token===l.token||typeof u.client=="string"&&u.client===l.client)&&i.delete(p);return}if(u.kind==="invoke"){let l=i.get(p);if(!l||l.token!==u.token||typeof u.id!="string")return;let O=Object.prototype.hasOwnProperty.call(G,u.command)?G[u.command]:void 0;if(!O)return;let P=Array.isArray(u.args)?u.args:[];Promise.resolve().then(()=>O(...P)).then(I=>s(p,l.origin,{kind:"result",token:l.token,id:u.id,ok:!0,value:I??null}),I=>{let d=String(I?.message??I),c=I instanceof DOMException?I.name:"NotAllowedError";s(p,l.origin,{kind:"result",token:l.token,id:u.id,ok:!1,message:d,name:c})})}}};return e.addEventListener("message",m),{stateChange(g,u){r={inputs:g,outputs:u},a("state-change",r)},midiMessage(g){a("midi-message",g)}}}function ge(e,n){let t=globalThis,i=ie(),r=new Map,s=[],o={"state-change":new Set,"midi-message":new Set},a=null,m=null,g=0,u=0,p=d=>{try{e.postMessage({[L]:!0,...d},n)}catch{}},l=()=>{a||g>=ce||(g++,p({kind:"hello",client:i}),a||(m=setTimeout(l,ae)))},O=()=>{for(let d of s.splice(0))P(d)},P=d=>{let c=`${i}:${++u}`;r.set(c,{resolve:d.resolve,reject:d.reject}),p({kind:"invoke",token:a,id:c,command:d.command,args:d.args})},I=d=>{let c=d?.data;if(!(!c||typeof c!="object"||c[L]!==!0)&&d.source===e&&d.origin===n){if(c.kind==="welcome"){if(c.client!==i||typeof c.token!="string")return;a=c.token,m&&(clearTimeout(m),m=null),O();return}if(!(typeof a!="string"||c.token!==a)){if(c.kind==="event"){if(!Object.prototype.hasOwnProperty.call(o,c.name))return;for(let y of o[c.name])try{y(c.payload)}catch{}return}if(c.kind==="result"){let y=r.get(c.id);if(!y)return;r.delete(c.id),c.ok?y.resolve(c.value??null):y.reject(new DOMException(String(c.message),String(c.name??"NotAllowedError")))}}}};return t.addEventListener("message",I),t.addEventListener("pagehide",d=>{d?.persisted||p({kind:"bye",client:i,token:a})}),t.addEventListener("pageshow",d=>{d?.persisted&&p({kind:"hello",client:i})}),l(),{invoke(d,c){return new Promise((y,re)=>{let H={command:d,args:c,resolve:y,reject:re};a?P(H):s.push(H)})},listen(d,c){return o[d].add(c),()=>o[d].delete(c)}}}function _e(e){try{let n=e.location.origin;return se(n)?n:null}catch{return null}}function fe(){let e=globalThis,n=!!globalThis.__TAURI_INTERNALS__,t=null;try{t=e.top}catch{}let i=!t||t===e.self||t===e,r=i?null:_e(t);if(!(!i&&r===null)&&!(i&&!n)){if(n)$=he(),w=(s,o)=>G[s](...o),j=s=>z.midiMessage.listen(o=>s(o.payload)),z.stateChange.listen(s=>Z(s.payload.inputs,s.payload.outputs));else{let s=ge(t,r);w=(o,a)=>s.invoke(o,a),j=o=>Promise.resolve(s.listen("midi-message",o)),s.listen("state-change",o=>Z(o.inputs,o.outputs))}j(de),pe()}}fe();})();