import{e,w as t,i as r,d as n}from"./core-7nKZylPm.js";const i=new WeakSet,o=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if("mpy"!==n||!i.is_pyterminal())return;const{pyterminal_ready:o,pyterminal_read:a,pyterminal_write:s}=i;e.registerJsModule("_pyscript_input",{input:a}),r(["from _pyscript_input import input","from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));const l=new Uint8Array([13]);t.stdout=e=>{10===e[0]&&s(l),s(e)},t.stderr=e=>{s(String(e.message||e))},i.pyterminal_stream_write=()=>{},e.registerJsModule("code",{interact(){const t=new TextEncoderStream;t.readable.pipeTo(new WritableStream({write(t){for(const r of t)e.replProcessChar(r)}}));const r=t.writable.getWriter();i.pyterminal_stream_write=e=>r.write(e),e.replInit()}}),o()};var a=async a=>{const[{Terminal:s},{FitAddon:l},{WebLinksAddon:m}]=await Promise.all([import("./xterm-Bdboj2r8.js"),import("./xterm_addon-fit-43qXDVCl.js"),import("./xterm_addon-web-links-DVH-i-UR.js")]),d={disableStdin:!1,cursorBlink:!0,cursorStyle:"block",lineHeight:1.2};let p;const c=()=>{let e=a;const t=e.getAttribute("target");if(t){if(e=document.getElementById(t)||document.querySelector(t),!e)throw new Error(`Unknown target ${t}`)}else e=document.createElement("py-terminal"),e.style.display="block",a.after(e);const r=new s({theme:{background:"#191A19",foreground:"#F5F2E7"},...d}),i=new l;return r.loadAddon(i),r.loadAddon(new m),r.open(e),i.fit(),r.focus(),n(a,{terminal:{value:r},process:{value:async e=>{for(const t of e.split(/(?:\r\n|\r|\n)/))await p.write(`${t}\r`)}}}),r};a.hasAttribute("worker")?(e.main.onWorker.add(function r(n,o){if(i.has(o))return;i.add(o),e.main.onWorker.delete(r);const a=c(),{sync:s}=o;let l=null,m="";s.is_pyterminal=()=>!0,s.pyterminal_read=e=>(a.write(e),l=t(),l.promise),s.pyterminal_write=e=>{l||a.write(e)},s.pyterminal_ready=()=>{let e=Promise.resolve();p={write:t=>e=e.then(()=>s.pyterminal_stream_write(t))},a.onData(e=>{l?(""===e?m.length?(m=m.slice(0,-1),e="\b \b"):e="":m+=e,e&&(a.write(e),m.endsWith("\r")&&(a.write("\n"),l.resolve(m.slice(0,-1)),l=null,m=""))):p.write(e)})}}),e.worker.onReady.add(o)):(e.main.codeBeforeRun.delete(r),e.main.onReady.add(function t({interpreter:r,io:n,run:i,type:o}){if("mpy"!==o)return;e.main.onReady.delete(t);const a=c(),s=new Uint8Array([13]);n.stdout=e=>{10===e[0]&&a.write(s),a.write(e)},globalThis.__py_terminal__=a,i(["from js import prompt as input","from js import __py_terminal__ as __terminal__"].join(";")),delete globalThis.__py_terminal__,r.registerJsModule("code",{interact(){const e=new TextEncoderStream;e.readable.pipeTo(new WritableStream({write(e){for(const t of e)r.replProcessChar(t)}})),p=e.writable.getWriter(),a.onData(e=>p.write(e)),r.replInit()}})}))};export{a as default};
//# sourceMappingURL=mpy-BC-zLHrK.js.map
