import { LAYOUT_ROW_HEIGHT as g } from "./packages/sdk/dist/shared/layout-constants.js";
import a from "./packages/sdk/dist/shared/utils/invariant.js";
import { enableMapSet as m } from "./node_modules/.pnpm/immer@10.1.1/node_modules/immer/dist/immer.js";
import { isEqual as p, isNil as S } from "lodash-es";
import { createContext as h, useContext as P } from "react";
import { temporal as b } from "./node_modules/.pnpm/zundo@2.3.0_zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124_/node_modules/zundo/dist/index.js";
import { useStore as r } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/index.js";
import { subscribeWithSelector as x, persist as f } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/middleware.js";
import { immer as I } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/middleware/immer.js";
import { usePageContext as M } from "./use-page-data.js";
import { createStore as v } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/vanilla.js";
m();
const D = (t) => {
  const o = {
    previewMode: "desktop",
    themesLibrary: [],
    pages: [],
    panelPosition: "left",
    logoLink: "/dashboard",
    zoom: 1,
    chatVisible: !0,
    contextMenuVisible: !1,
    planIndex: 0,
    imagesSearchResults: void 0,
    sitePrompt: "",
    onPublish: () => {
      console.warn("onPublish is not implemented");
    },
    onDraftChange: async () => {
      console.warn("onDraftChange is not implemented");
    },
    debugMode: !1
  };
  return v()(
    x(
      f(
        b(
          I((n, c) => ({
            ...o,
            ...t,
            updateHistory: () => {
              setTimeout(() => {
                const e = c(), i = {
                  panel: e.panel,
                  selectedBrickId: e.selectedBrickId,
                  selectedSectionId: e.selectedBrickId ? void 0 : e.selectedSectionId,
                  panelPosition: e.panelPosition,
                  modal: e.modal,
                  debug: void 0
                }, d = new URL(window.location.href);
                Object.entries(i).forEach(([u, l]) => {
                  !S(l) && l.toString().trim() !== "" ? d.searchParams.set(u, l.toString()) : d.searchParams.delete(u);
                }), window.history.pushState(i, "", d.toString());
              }, 100);
            },
            setIsResizing: (e) => n((i) => {
              i.resizing = e;
            }),
            setMouseOverPanel: (e) => n((i) => {
              i.isMouseOverPanel = e;
            }),
            setContextMenuVisible: (e) => n((i) => {
              i.contextMenuVisible = e;
            }),
            setDraggingBrickType: (e) => n((i) => {
              i.draggingBrickType = e ?? void 0;
            }),
            setGridConfig: (e) => n((i) => {
              i.gridConfig = e;
            }),
            toggleEditorEnabled: () => n((e) => {
              e.disabled = !e.disabled;
            }),
            toggleChat: () => n((e) => {
              e.chatVisible = !e.chatVisible, e.chatVisible && (e.panel = void 0, e.panelPosition = "right"), e.updateHistory();
            }),
            toggleDebugMode: () => n((e) => {
              e.debugMode = !e.debugMode;
            }),
            setImagesSearchResults: (e) => n((i) => {
              i.imagesSearchResults = e;
            }),
            setPreviewMode: (e) => n((i) => {
              i.previewMode = e;
            }),
            setSettingsVisible: (e) => n((i) => {
              i.settingsVisible = e, i.updateHistory();
            }),
            toggleSettings: () => n((e) => {
              e.settingsVisible = !e.settingsVisible, e.updateHistory();
            }),
            setIsEditingText: (e) => n((i) => {
              i.isEditingTextForBrickId = e || void 0;
            }),
            setPanel: (e) => n((i) => {
              i.panel = e, i.updateHistory();
            }),
            togglePanel: (e) => n((i) => {
              i.panel = e && i.panel === e ? void 0 : e, i.updateHistory();
            }),
            hidePanel: (e) => n((i) => {
              (!e || i.panel === e) && (i.panel = void 0), i.updateHistory();
            }),
            toggleModal: (e) => n((i) => {
              i.modal = e && i.modal === e ? void 0 : e, i.updateHistory();
            }),
            zoomIn: () => n((e) => {
              e.zoom = Math.min(e.zoom + 0.1, 2);
            }),
            zoomOut: () => n((e) => {
              e.zoom = Math.max(e.zoom - 0.1, 0.5);
            }),
            resetZoom: () => n((e) => {
              e.zoom = 1;
            }),
            setSelectedGroup: (e) => n((i) => {
              i.selectedGroup = e;
            }),
            setSelectedBrickId: (e) => n((i) => {
              i.selectedBrickId = e, e && (i.selectedSectionId = void 0), i.updateHistory();
            }),
            setSelectedSectionId: (e) => n((i) => {
              i.selectedSectionId = e, e && (i.selectedBrickId = void 0, i.selectedGroup = void 0), i.updateHistory();
            }),
            deselectBrick: (e) => n((i) => {
              i.selectedBrickId && (!e || i.selectedBrickId === e) && (i.selectedBrickId = void 0), i.updateHistory();
            }),
            togglePanelPosition: () => n((e) => {
              e.panelPosition = e.panelPosition === "left" ? "right" : "left", e.updateHistory();
            }),
            showModal: (e) => n((i) => {
              i.modal = e, i.updateHistory();
            }),
            hideModal: () => n((e) => {
              e.modal = void 0, e.updateHistory();
            })
          })),
          // limit undo history to 100
          {
            limit: 100,
            equality: (n, c) => p(n, c)
          }
        ),
        {
          name: "editor-state",
          partialize: (n) => Object.fromEntries(
            Object.entries(n).filter(([c]) => ["chatVisible", "previewMode"].includes(c))
          )
        }
      )
    )
  );
}, E = h(null), s = () => {
  const t = P(E);
  return t || console.log("Problem with EditorStoreContext", t), a(t, "useEditorStoreContext must be used within a EditorStoreContext"), t;
}, G = () => {
  const t = s();
  return r(t);
}, L = () => {
  const t = s();
  return r(t, (o) => o.previewMode);
}, Z = () => {
  const t = s();
  return r(t, (o) => !o.disabled);
}, F = () => {
  const t = s();
  return r(t, (o) => o.logoLink);
}, U = () => {
  const t = s();
  return r(t, (o) => o.selectedGroup);
}, j = () => {
  const t = s();
  return r(t, (o) => o.selectedBrickId);
};
function W() {
  const t = s();
  return r(t, (o) => o.selectedSectionId);
}
function _() {
  const t = s(), o = M();
  return r(t, (n) => {
    const c = o.getState().sections.find((e) => e.id === n.selectedSectionId);
    return c || null;
  });
}
const q = () => {
  const t = s();
  return r(t, (o) => !!o.debugMode);
}, A = () => {
  const t = s();
  return r(t, (o) => ({
    panel: o.panel,
    panelPosition: o.panelPosition
  }));
}, N = () => {
  const t = s();
  return r(t, (o) => o.modal);
}, Y = () => {
  const t = s();
  return r(t, (o) => o.planIndex > 0);
};
function J(t) {
  const o = s();
  return r(o, (n) => n.planIndex >= t);
}
const K = () => {
  const t = s();
  return r(t, (o) => o.themesLibrary);
}, Q = () => {
  const t = s();
  return r(t, (o) => o.imagesSearchResults);
}, X = () => {
  const t = s();
  return r(t, (o) => ({
    zoom: o.zoom,
    zoomIn: o.zoomIn,
    zoomOut: o.zoomOut,
    canZoomIn: o.zoom < 2,
    canZoomOut: o.zoom > 0.5,
    resetZoom: o.resetZoom
  }));
}, $ = () => {
  const t = s();
  return r(t, (o) => !!o.chatVisible);
}, ee = () => {
  const t = s();
  return r(t, (o) => o.resizing);
};
function oe() {
  const t = s();
  return r(t, (o) => o.isEditingTextForBrickId);
}
const ie = () => {
  const t = s();
  return r(t, (o) => o.draggingBrickType);
}, te = () => {
  const t = s();
  return r(t, (o) => o.contextMenuVisible);
}, ne = () => {
  const t = s();
  return r(t, (o) => o.isMouseOverPanel);
}, re = () => {
  const t = s();
  return r(t, (o) => ({
    setIsResizing: o.setIsResizing,
    setMouseOverPanel: o.setMouseOverPanel,
    toggleDebugMode: o.toggleDebugMode,
    setContextMenuVisible: o.setContextMenuVisible,
    setDraggingBrickType: o.setDraggingBrickType,
    setPreviewMode: o.setPreviewMode,
    setSettingsVisible: o.setSettingsVisible,
    setGridConfig: o.setGridConfig,
    toggleSettings: o.toggleSettings,
    setIsEditingText: o.setIsEditingText,
    setImagesSearchResults: o.setImagesSearchResults,
    setPanel: o.setPanel,
    togglePanel: o.togglePanel,
    toggleModal: o.toggleModal,
    hidePanel: o.hidePanel,
    setSelectedGroup: o.setSelectedGroup,
    setSelectedBrickId: o.setSelectedBrickId,
    setSelectedSectionId: o.setSelectedSectionId,
    deselectBrick: o.deselectBrick,
    togglePanelPosition: o.togglePanelPosition,
    showModal: o.showModal,
    hideModal: o.hideModal,
    toggleEditorEnabled: o.toggleEditorEnabled,
    onPublish: o.onPublish,
    onSavePage: o.onSavePage,
    onSaveSite: o.onSaveSite,
    onDraftChange: o.onDraftChange,
    toggleChat: o.toggleChat,
    onShowPopup: o.onShowPopup
  }));
};
function se() {
  const t = s();
  return r(
    t,
    (o) => o.gridConfig ?? {
      colWidth: 20,
      rowHeight: g
    }
  );
}
export {
  E as EditorStoreContext,
  D as createEditorStore,
  $ as useChatVisible,
  te as useContextMenuVisible,
  q as useDebugMode,
  ie as useDraggingBrickType,
  oe as useEditingTextForBrickId,
  G as useEditor,
  Z as useEditorEnabled,
  re as useEditorHelpers,
  s as useEditorStoreContext,
  se as useGridConfig,
  J as useHasPlanOrHigher,
  Q as useImagesSearchResults,
  ne as useIsMouseOverPanel,
  Y as useIsPremiumPlan,
  ee as useIsResizing,
  F as useLogoLink,
  N as useModal,
  A as usePanel,
  L as usePreviewMode,
  j as useSelectedBrickId,
  U as useSelectedGroup,
  _ as useSelectedSection,
  W as useSelectedSectionId,
  K as useThemesLibrary,
  X as useZoom
};
//# sourceMappingURL=use-editor.js.map
