import{a as e,g as t}from"./core-BuLtL7jM.js";const{get:r}=Reflect,a=["preventDefault","stopPropagation","stopImmediatePropagation"],n={once:!0};class o{#e;constructor(e){this.#e=e}get(e,t){return"currentTarget"===t?this.#e:((e,t)=>"function"==typeof t?t.bind(e):t)(e,r(e,t))}}const{stringify:s}=JSON,c=(e,t)=>`${e}(code, ${t.join(", ")})`,i=({type:e="py",persistent:r,terminal:i,config:_,serviceWorker:l})=>{const u=r?["globals()","__locals__"]:[i?'{"__terminal__":__terminal__}':"{}","{}"],p=URL.createObjectURL(new Blob([["from pyscript import sync, config",'__message__ = lambda e,v: f"[31m[1m{e.__name__}[0m: {v}"',"__locals__ = {}",'if config["type"] == "py":',"\timport sys","\tdef __error__(_):","\t\tinfo = sys.exc_info()","\t\treturn __message__(info[0], info[1])","else:","\t__error__ = lambda e: __message__(e.__class__, e.value)","def execute(code):",`\ttry: return ${c("exec",u)};`,"\texcept Exception as e: print(__error__(e));","def evaluate(code):",`\ttry: return ${c("eval",u)};`,"\texcept Exception as e: print(__error__(e));","sync.execute = execute","sync.evaluate = evaluate"].join("\n")])),m=t(document.createElement("script"),{type:e,src:p});return m.toggleAttribute("worker",!0),m.toggleAttribute("terminal",!0),i&&m.setAttribute("target",i),_&&m.setAttribute("config","string"==typeof _?_:s(_)),l&&m.setAttribute("service-worker",l),((e,t,r=null)=>new Promise((s,c)=>{const i=new o(e);if(r.signal){const e=e=>c(new Proxy(e,i));if(r.signal.addEventListener("abort",e,n),r.signal.aborted)return r.signal.dispatchEvent(new Event("abort"))}e.addEventListener(t,e=>{for(const t of a)r[t]&&e[t]();s(new Proxy(e,i))},{...r,...n})}))(document.body.appendChild(m),`${e}:done`,{stopPropagation:!0}).then(()=>(URL.revokeObjectURL(p),m))},_=async e=>{const t=await i(e),{xworker:r,process:a,terminal:n}=t,{execute:o,evaluate:s}=r.sync;return t.remove(),{xworker:r,process:a,terminal:n,execute:o,evaluate:s}};var l=async(t={})=>{let r=await _(t),a=!1;const n=()=>{r&&(r.xworker.terminate(),r.terminal.dispose(),r=null),a=!1},o=async()=>{n(),r=await _(t)},s=t=>async n=>{a&&await o(),a=!0;try{return await r[t](e(n))}catch(e){console.error(e)}finally{a=!1}},c=e=>async()=>{a?await o():r?.terminal[e]()};return{process:s("process"),execute:s("execute"),evaluate:s("evaluate"),clear:c("clear"),reset:c("reset"),kill:n}};export{l as default};
//# sourceMappingURL=donkey-2hW3ZLW0.js.map
