import { j as jsxRuntimeExports } from "./chunks/jsx-runtime.CXYVdt5g.js";
import { forwardRef, useRef, useImperativeHandle, useEffect } from "react";
import "./chunks/index.bXWE_wTx.js";
import { a as tx } from "./chunks/twind.B1qQmF_s.js";
import { a as useDarkMode } from "./chunks/index.Cwl5DAaT.js";
import { z as createEditorStore, A as createDraftStore, E as EditorStoreContext, D as DraftStoreContext } from "./chunks/use-editor.CurXdZnZ.js";
import { U as UploaderProvider } from "./chunks/UploaderContext.B3E_5A-R.js";
import { R } from "./chunks/theme.0GtWn8C6.js";
const EditorWrapper = forwardRef(
  ({
    config,
    pageVersion,
    pageId,
    onImageUpload,
    children,
    onSaveSite,
    onSavePage,
    onPublish,
    onShowPopup,
    onReady = () => {
    }
  }, ref) => {
    const { site, pages } = config;
    const urlParams = new URL(self.location.href).searchParams;
    const setup = urlParams.has("setup");
    const debugMode = urlParams.has("debug") && urlParams.get("debug") !== "false";
    const editorStore = useRef(
      createEditorStore({
        onPublish,
        onSaveSite,
        onSavePage,
        onShowPopup,
        logoLink: `/dashboard/sites/${site.id}`,
        debugMode,
        panel: urlParams.get("panel") ?? void 0,
        selectedBrickId: urlParams.get("selectedBrickId") ?? void 0,
        selectedSectionId: urlParams.get("selectedBrickId") ? void 0 : urlParams.get("selectedSectionId") ?? void 0,
        modal: urlParams.get("modal") ?? void 0,
        attributesTab: urlParams.get("attributesTab") ?? void 0,
        panelPosition: urlParams.get("panelPosition") ?? void 0
      })
    ).current;
    const page = pages.find((p) => p.id === pageId) ?? pages[0];
    const draftStore = useRef(
      createDraftStore({
        site: {
          ...site,
          ...setup ? {
            attributes: {},
            datarecords: [],
            datasources: [],
            sitemap: [],
            themes: []
          } : {}
        },
        page: {
          ...page,
          version: pageVersion,
          ...setup ? {
            attributes: {
              path: "/"
            },
            sections: [],
            label: "Home"
          } : {}
        }
      })
    ).current;
    useImperativeHandle(
      ref,
      () => ({
        editorStore,
        draftStore
      }),
      [editorStore, draftStore]
    );
    const { isDarkMode } = useDarkMode();
    useEffect(onReady, []);
    return /* @__PURE__ */ jsxRuntimeExports.jsx(UploaderProvider, { onImageUpload, children: /* @__PURE__ */ jsxRuntimeExports.jsx(EditorStoreContext.Provider, { value: editorStore, children: /* @__PURE__ */ jsxRuntimeExports.jsx(DraftStoreContext.Provider, { value: draftStore, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
      R,
      {
        accentColor: "violet",
        className: tx("w-full flex flex-col"),
        appearance: isDarkMode ? "dark" : "light",
        children
      }
    ) }, "DraftStoreContext") }, "EditorStoreContext") });
  }
);
export {
  EditorWrapper
};
//# sourceMappingURL=EditorWrapper.js.map
