/**
 * Copyright 2024
 * @license Apache-2.0, see License.md for full text.
 */
import{html as e,css as t}from"../../lit/index.js";import{ESGlobalBridgeStore as i}from"../es-global-bridge/es-global-bridge.js";import{DDD as r}from"../d-d-d/d-d-d.js";class RunkitEmbed extends r{static get tag(){return"runkit-embed"}static get styles(){return[t`
        :host {
          display: block;
          min-height: 100px;
        }
      `]}constructor(){super(),this.__runkitloaded=!1,this.loading=!1,i.load("runkit","https://embed.runkit.com").then((()=>{globalThis.RunKit&&(this.__runkitloaded=!0,this.buildRunKit())})),this.querySelector("template")?this.source=this.querySelector("template").content.textContent:this.source="",this.mode="endpoint",this.nodeVersion="18.x.x",this.__observer=new MutationObserver((e=>{this.querySelector("template")&&!this.loading&&(this.source=this.querySelector("template").content.textContent)})),this.__observer.observe(this,{attributes:!0,childList:!0,subtree:!0,characterData:!1})}updated(e){super.updated&&super.updated(e),!this._haxstate&&!this.dataHaxActive&&this.__runkitloaded&&(e.has("source")||e.has("nodeVersion")||e.has("mode"))&&this.nodeVersion&&this.mode&&this.source&&this.buildRunKit()}buildRunKit(){this.__runkitloaded&&(this.__rkContainer&&this.__rkContainer.remove(),this.loading=!0,globalThis.RunKit.createNotebook({mode:this.mode,nodeVersion:this.nodeVersion,element:this,source:this.source,onLoad:e=>{this.__rkContainer=this.querySelector(`[name="${e.name}"]`).parentNode,this.loading=!1}}))}render(){return e`<slot></slot>`}static get properties(){return{nodeVersion:{type:String,attribute:"node-version"},mode:{type:String},loading:{type:Boolean},source:{type:String},dataHaxActive:{type:String,reflect:!0,attribute:"data-hax-active"}}}haxHooks(){return{preProcessNodeToContent:"haxpreProcessNodeToContent",editModeChanged:"haxeditModeChanged"}}haxeditModeChanged(e){this._haxstate=e,this._haxstate&&this.querySelector("div")&&this.querySelector("div").remove()}async haxpreProcessNodeToContent(e){return this.__runkitloaded=!1,this.loading=!1,this.__rkContainer&&this.__rkContainer.remove(),this._haxstate&&this.querySelector("div")&&this.querySelector("div").remove(),e}static get haxProperties(){return new URL(`./lib/${this.tag}.haxProperties.json`,import.meta.url).href}}globalThis.customElements.define(RunkitEmbed.tag,RunkitEmbed);export{RunkitEmbed};