import{TYPES as e,hooks as t}from"./core.js";import{notify as r}from"./error-96hMSEw8.js";const n=[...e.keys()].map((e=>`script[type="${e}"][terminal],${e}-script[terminal]`)).join(","),o=e=>{throw r(e),new Error(e)},i=async()=>{const e=document.querySelectorAll(n);if(!e.length)return;s.disconnect(),e.length>1&&o("You can use at most 1 terminal.");const[r]=e;r.matches('script[type="mpy"],mpy-script')&&o("Unsupported terminal."),document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:new URL("./xterm.css",import.meta.url)}));const[{Terminal:i},{Readline:a}]=await Promise.all([import("./xterm-f2QfYNGL.js"),import("./xterm-readline-ONk85xtH.js")]),d=new a,l=e=>{let t=r;const n=r.getAttribute("target");if(n){if(t=document.getElementById(n)||document.querySelector(n),!t)throw new Error(`Unknown target ${n}`)}else t=document.createElement("py-terminal"),t.style.display="block",r.after(t);const o=new i({theme:{background:"#191A19",foreground:"#F5F2E7"},...e});o.loadAddon(d),o.open(t),o.focus()};if(r.hasAttribute("worker")){const e=({interpreter:e},{sync:t})=>{t.pyterminal_drop_hooks();const r=new TextDecoder;let n="";const o={isatty:!0,write:e=>(n=r.decode(e),t.pyterminal_write(n),e.length)};e.setStdout(o),e.setStderr(o),e.setStdin({isatty:!0,stdin:()=>t.pyterminal_read(n)})};t.main.onWorker.add((function r(n,o){t.main.onWorker.delete(r),l({disableStdin:!1,cursorBlink:!0,cursorStyle:"block"}),o.sync.pyterminal_read=d.read.bind(d),o.sync.pyterminal_write=d.write.bind(d),o.sync.pyterminal_drop_hooks=()=>{t.worker.onReady.delete(e)}})),t.worker.onReady.add(e)}else t.main.onReady.add((function e({io:r}){console.warn("py-terminal is read only on main thread"),t.main.onReady.delete(e),l({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),r.stdout=e=>{d.write(`${e}\n`)},r.stderr=e=>{d.write(`${e.message||e}\n`)}}))},s=new MutationObserver(i);s.observe(document,{childList:!0,subtree:!0});var a=i();export{a as default};
//# sourceMappingURL=py-terminal-XWbSa71s.js.map
