import{A as C,C as w,a as p,b as P}from"./chunk-ULPRULB6.js";import{P as b,S as v,U as S}from"./chunk-RXF2JDJ3.js";var m=class{constructor(t){this.loaded=!1;this.preinitted=!1;this.started=!1;this.romtype="cart";this.running=!1;this.initluavars=!1;this.mainElement=t,this.timer=new S(20,this.poll.bind(this))}luacall(t){return this.js_lua_string||(this.js_lua_string=Module.cwrap("_Z13js_lua_stringPKc","string",["string"])),this.js_lua_string(t||"")}_pause(){this.running=!1,this.timer.stop()}pause(){this.loaded&&this.running&&(this.luacall("emu.pause()"),this._pause())}_resume(){this.luacall("emu.unpause()"),this.running=!0,this.timer.start()}resume(){this.loaded&&!this.running&&this._resume()}reset(){this.loaded&&(this.luacall("manager:machine():soft_reset()"),this.running=!0,this.initluavars=!1)}isRunning(){return this.running}bufferConsoleOutput(t){typeof t=="string"&&console.log(t)}startModule(t,e){this.started=!0;var r=this.romfn=this.romfn||e.romfn,a=this.romdata=this.romdata||e.romdata||new v(e.romsize).mem,o=this.romtype=this.romtype||e.romtype,n=this.video=new b(this.mainElement,e.width,e.height);n.create(),$(n.canvas).attr("id","canvas"),console.log("loading",e.jsfile);var u=[e.driver,"-debug","-debugger","none","-verbose","-window","-nokeepaspect","-resolution",n.canvas.width+"x"+n.canvas.height];r&&u.push("-"+o,r),e.extraargs&&(u=u.concat(e.extraargs)),console.log(u),window.JSMESS={},window.Module={arguments:u,screenIsReadOnly:!0,print:this.bufferConsoleOutput,canvas:n.canvas,doNotCaptureKeyboard:!0,keyboardListeningElement:n.canvas,preInit:()=>{console.log("loading FS"),ENV.SDL_EMSCRIPTEN_KEYBOARD_ELEMENT="canvas",e.cfgfile&&(FS.mkdir("/cfg"),FS.writeFile("/cfg/"+e.cfgfile,e.cfgdata,{encoding:"utf8"})),e.biosfile&&(FS.mkdir("/roms"),FS.mkdir("/roms/"+e.driver),FS.writeFile("/roms/"+e.biosfile,e.biosdata,{encoding:"binary"})),FS.mkdir("/emulator"),r&&FS.writeFile(r,a,{encoding:"binary"}),e.preInit&&e.preInit(self),this.preinitted=!0},preRun:[()=>{$(n.canvas).click(i=>{n.canvas.focus()}),this.loaded=!0,console.log("about to run...")}]};var d,c,g=$.Deferred(),f=$.Deferred();if(e.cfgfile&&(d=$.get("mame/cfg/"+e.cfgfile,i=>{e.cfgdata=i,console.log("loaded "+e.cfgfile)},"text")),e.biosfile){var s=new XMLHttpRequest;s.open("GET","mame/roms/"+e.biosfile,!0),s.responseType="arraybuffer",s.onload=i=>{e.biosdata=new Uint8Array(s.response),console.log("loaded "+e.biosfile),g.resolve()},s.ontimeout=function(i){throw Error("Timeout loading "+e.biosfile)},s.send()}else g.resolve();c=$.get("mame/debugger.lua",i=>{this.luadebugscript=i,console.log("loaded debugger.lua")},"text");{var l=new XMLHttpRequest;l.open("GET","mame/"+e.jsfile.replace(".js",".wasm"),!0),l.responseType="arraybuffer",l.onload=i=>{console.log("loaded WASM file"),window.Module.wasmBinary=new Uint8Array(l.response),f.resolve()},l.ontimeout=function(i){throw Error("Timeout loading "+e.jsfile)},l.send()}$.when(c,d,g,f).done(()=>{var i=document.createElement("script");i.src="mame/"+e.jsfile,document.getElementsByTagName("head")[0].appendChild(i),console.log("created script element")}),window.mamelua=i=>(this.initlua(),[i,this.luacall(i)])}loadROMFile(t){this.romdata=t,this.preinitted&&this.romfn&&FS.writeFile(this.romfn,t,{encoding:"binary"})}loadRegion(t,e){if(this.loaded&&e.length>0){for(var r='rgn = manager:machine():memory().regions["'+t+`"] `,a=0;a