import {
  AppThemeProvider,
  HTML_ID_APP_ROOT
} from "./chunk-KYQU52U4.js";
import "./chunk-Z67LV52D.js";

// runtime/codeComponentEditor/renderCodeComponent.tsx
import * as React2 from "react";
import { createRoot } from "react-dom/client";

// runtime/codeComponentEditor/CodeComponentSandbox.tsx
import { styled } from "@mui/material";
import * as React from "react";
var ErrorBoundary = class extends React.Component {
  constructor() {
    super(...arguments);
    this.state = {
      error: null
    };
  }
  static getDerivedStateFromError(error) {
    return { error };
  }
  render() {
    if (this.state.error) {
      return /* @__PURE__ */ React.createElement(React.Fragment, null, this.state.error.message);
    }
    return this.props.children;
  }
};
function Noop() {
  return null;
}
var CodeComponentSandboxRoot = styled("div")({});
function CodeComponentSandbox() {
  const [codeComponentSrc, setCodeComponentSrc] = React.useState(null);
  const [Component2, setComponent] = React.useState(() => Noop);
  const [errorBoundaryKey, seterrorBoundaryKey] = React.useState(1);
  React.useEffect(() => {
    window.__CODE_COMPONENT_SANDBOX_BRIDGE__ = {
      updateCodeCompoent: (component) => setCodeComponentSrc(component)
    };
    if (typeof window.__CODE_COMPONENT_SANDBOX_READY__ === "function") {
      window.__CODE_COMPONENT_SANDBOX_READY__();
    } else {
      window.__CODE_COMPONENT_SANDBOX_READY__ = true;
    }
    return () => {
      delete window.__CODE_COMPONENT_SANDBOX_BRIDGE__;
    };
  }, []);
  React.useEffect(() => {
    if (!codeComponentSrc) {
      return () => {
      };
    }
    const importUrl = URL.createObjectURL(new Blob([codeComponentSrc], {
      type: "application/javascript"
    }));
    import(importUrl).then((mod) => {
      setComponent(() => mod.default);
      seterrorBoundaryKey((key) => key + 1);
    });
    return () => URL.revokeObjectURL(importUrl);
  }, [codeComponentSrc]);
  return /* @__PURE__ */ React.createElement(ErrorBoundary, {
    key: errorBoundaryKey
  }, /* @__PURE__ */ React.createElement(CodeComponentSandboxRoot, null, /* @__PURE__ */ React.createElement(AppThemeProvider, {
    node: null
  }, /* @__PURE__ */ React.createElement(Component2, null))));
}

// runtime/codeComponentEditor/renderCodeComponent.tsx
function renderToolpadApp() {
  const container = document.getElementById(HTML_ID_APP_ROOT);
  if (!container) {
    throw new Error(`Can't locate app container #${HTML_ID_APP_ROOT}`);
  }
  const root = createRoot(container);
  root.render(/* @__PURE__ */ React2.createElement(CodeComponentSandbox, null));
}

// runtime/codeComponentEditor/index.tsx
renderToolpadApp();
