var C=Object.defineProperty;var R=(r,e,o)=>e in r?C(r,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):r[e]=o;var y=(r,e,o)=>R(r,typeof e!="symbol"?e+"":e,o);import{j as w}from"./jsx-runtime-CLpGMVip.js";const E=r=>{const e=r.split(".");return e[0]==="0"||e[0]==="1"||e[0]==="2"?`https://python-editor-${encodeURIComponent(r.replace(/[.]/g,"-"))}.microbit.org`:`https://python.microbit.org/v/${r}`},_=(r,e,o,s)=>{const c=new URL(E(r));if(e&&c.searchParams.set("l",e),o&&c.searchParams.set("controller",o.toString()),s)for(const[f,u]of Object.entries(s))c.searchParams.set(f,u);return c.toString()};class N{constructor(e,o){y(this,"ready",!1);y(this,"messageQueue",[]);y(this,"nextId",0);y(this,"pendingResponses",new Map);y(this,"_expectedOrigin");y(this,"expectedOrigin",()=>{var o;if(this._expectedOrigin)return this._expectedOrigin;const e=(o=this.iframe())==null?void 0:o.src;if(e)return this._expectedOrigin=new URL(e).origin,this._expectedOrigin});y(this,"listener",e=>{var c,f,u,g,x,F;const o=this.expectedOrigin();if(!o||e.origin!==o)return;const{data:s}=e;if(typeof s=="object"&&((s.type==="iframeclientready"||s.type==="pyeditor")&&(this.ready=!0,this.messageQueue.forEach(j=>this.sendMessageNoReadyCheck(j)),this.messageQueue.length=0),s.type==="pyeditor"))switch(this.handleWorkspaceSync(s),s.action){case"workspacesave":return(f=(c=this.options).onWorkspaceSave)==null?void 0:f.call(c,s);case"workspacesync":return(g=(u=this.options).onWorkspaceSync)==null?void 0:g.call(u,s);case"workspaceloaded":return(F=(x=this.options).onWorkspaceLoaded)==null?void 0:F.call(x,s)}});y(this,"sendRequest",e=>{e.response=!0,e.id||(e.id=(this.nextId++).toString());const o=e.id,s=new Promise((c,f)=>{this.pendingResponses.set(o,{resolve:c,reject:f,message:e})});return this.sendMessage(e),s});y(this,"sendMessage",e=>{this.ready?this.sendMessageNoReadyCheck(e):this.messageQueue.push(e)});y(this,"sendMessageNoReadyCheck",e=>{var o,s;(s=(o=this.iframe())==null?void 0:o.contentWindow)==null||s.postMessage(e,"*")});this.options=e,this.iframe=o}initialize(){var e,o;window.addEventListener("message",this.listener),(o=(e=this.iframe())==null?void 0:e.contentWindow)==null||o.postMessage({type:"iframeclientready"},"*")}dispose(){window.removeEventListener("message",this.listener)}setOptions(e){this.options=e}async importProject(e){await this.sendRequest({type:"pyeditor",action:"importproject",...e})}async handleWorkspaceSync(e){try{if(e.action==="workspacesync"){const o=await this.options.initialProjects();this.sendMessageNoReadyCheck({...e,success:!0,projects:o})}}catch(o){console.error(o)}}}var v={},A;function L(){if(A)return v;A=1,v.byteLength=g,v.toByteArray=F,v.fromByteArray=k;for(var r=[],e=[],o=typeof Uint8Array<"u"?Uint8Array:Array,s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c=0,f=s.length;c<f;++c)r[c]=s[c],e[s.charCodeAt(c)]=c;e[45]=62,e[95]=63;function u(t){var n=t.length;if(n%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var i=t.indexOf("=");i===-1&&(i=n);var d=i===n?0:4-i%4;return[i,d]}function g(t){var n=u(t),i=n[0],d=n[1];return(i+d)*3/4-d}function x(t,n,i){return(n+i)*3/4-i}function F(t){var n,i=u(t),d=i[0],p=i[1],a=new o(x(t,d,p)),h=0,m=p>0?d-4:d,l;for(l=0;l<m;l+=4)n=e[t.charCodeAt(l)]<<18|e[t.charCodeAt(l+1)]<<12|e[t.charCodeAt(l+2)]<<6|e[t.charCodeAt(l+3)],a[h++]=n>>16&255,a[h++]=n>>8&255,a[h++]=n&255;return p===2&&(n=e[t.charCodeAt(l)]<<2|e[t.charCodeAt(l+1)]>>4,a[h++]=n&255),p===1&&(n=e[t.charCodeAt(l)]<<10|e[t.charCodeAt(l+1)]<<4|e[t.charCodeAt(l+2)]>>2,a[h++]=n>>8&255,a[h++]=n&255),a}function j(t){return r[t>>18&63]+r[t>>12&63]+r[t>>6&63]+r[t&63]}function P(t,n,i){for(var d,p=[],a=n;a<i;a+=3)d=(t[a]<<16&16711680)+(t[a+1]<<8&65280)+(t[a+2]&255),p.push(j(d));return p.join("")}function k(t){for(var n,i=t.length,d=i%3,p=[],a=16383,h=0,m=i-d;h<m;h+=a)p.push(P(t,h,h+a>m?m:h+a));return d===1?(n=t[i-1],p.push(r[n>>2]+r[n<<4&63]+"==")):d===2&&(n=(t[i-2]<<8)+t[i-1],p.push(r[n>>10]+r[n>>4&63]+r[n<<2&63]+"=")),p.join("")}return v}var O=L();const b=`# Add your Python code here. E.g.
from microbit import *


while True:
    display.scroll('Hello, World!')
    display.show(Image.HEART)
    sleep(2000)
`,B=r=>{for(const e in r)Object.prototype.hasOwnProperty.call(r,e)&&(r[e]=O.fromByteArray(new TextEncoder().encode(r[e])));return r},H={files:B({"main.py":`# Add your Python code here. E.g.
from microbit import *
import foo


while True:
    display.scroll('Hello, World!')
    display.show(Image.HEART)
    foo.bar()
    sleep(2000)
`,"foo.py":`def bar():
    print('Hello from foo.py!')
`}),projectName:"Multiple files project"},S={fontFamily:"sans-serif",display:"flex",flexWrap:"wrap",gap:"5px",margin:"10px 0"},W=({driver:r})=>w.jsx("div",{style:{display:"flex",flexDirection:"column"},children:w.jsx("div",{style:S,children:w.jsx("button",{onClick:()=>r.current.importProject({project:b}),children:"Import project"})})});W.__docgenInfo={description:"",methods:[],displayName:"PythonEditorToolbar",props:{driver:{required:!0,tsType:{name:"RefObject",elements:[{name:"PythonEditorFrameDriver"}],raw:"RefObject<PythonEditorFrameDriver>"},description:""}}};const M=r=>w.jsx("div",{style:{display:"flex",flexDirection:"column",width:"100%",height:700},children:r.children});M.__docgenInfo={description:"",methods:[],displayName:"StoryWrapper",props:{children:{required:!0,tsType:{name:"ReactNode"},description:""}}};const U="MicrobitStorybook";export{W as P,M as S,N as a,U as b,_ as c,b as d,H as m};
