import { jsx } from "react/jsx-runtime";
import { forwardRef, useRef, useImperativeHandle, useEffect } from "react";
import "./chunks/index.NovFngrl.js";
import { a as tx } from "./chunks/twind.DdSZmZu6.js";
import { u as useDarkMode } from "./chunks/index.rDt1W65w.js";
import { g as createEditorStore, E as EditorStoreContext } from "./chunks/use-editor.BcGWPSSE.js";
import { U as UploaderProvider } from "./chunks/UploaderContext.DwS2olmD.js";
import { v as createDraftStore, D as DraftStoreContext } from "./chunks/use-page-data.CMUQyNAA.js";
import { R } from "./chunks/theme.aQt__Z8L.js";
const EditorWrapper = forwardRef(
  ({
    chatSession,
    site,
    pages,
    pageVersion,
    pageId,
    onImageUpload,
    children,
    onSaveSite,
    onSavePage,
    onPublish,
    onShowPopup,
    onPageCreated,
    onReady = () => {
    }
  }, ref) => {
    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,
        onPageCreated,
        logoLink: `/dashboard/sites/${site.id}`,
        debugMode,
        chatSession,
        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: "/",
              title: "Untitled",
              description: "",
              keywords: "",
              queries: [],
              tags: []
            },
            sections: [],
            label: "Untitled"
          } : {}
        }
      })
    ).current;
    useImperativeHandle(
      ref,
      () => ({
        editorStore,
        draftStore
      }),
      [editorStore, draftStore]
    );
    const { isDarkMode } = useDarkMode();
    useEffect(onReady, []);
    return /* @__PURE__ */ jsx(UploaderProvider, { onImageUpload, children: /* @__PURE__ */ jsx(EditorStoreContext.Provider, { value: editorStore, children: /* @__PURE__ */ jsx(DraftStoreContext.Provider, { value: draftStore, children: /* @__PURE__ */ jsx(
      R,
      {
        accentColor: "violet",
        className: tx("w-full flex flex-col"),
        appearance: isDarkMode ? "dark" : "light",
        children
      }
    ) }, "DraftStoreContext") }, "EditorStoreContext") });
  }
);
export {
  EditorWrapper
};
//# sourceMappingURL=EditorWrapper.js.map
