import { bf as enableMapSet, bg as useStore, bh as usePageContext, a6 as invariant, bi as createStore, bj as subscribeWithSelector, bk as temporal, bl as immer } from "./use-page-data.CMUQyNAA.js";
import { isEqual, isNil } from "lodash-es";
import { createContext, useContext } from "react";
/*! Upstart.gg - Copyright (C) 2024 Flippable - https://github.com/upstart-gg/upstart/blob/main/LICENSE */
var LAYOUT_COLS = 24;
var LAYOUT_ROW_HEIGHT = 24;
enableMapSet();
const createEditorStore = (initProps) => {
  const DEFAULT_PROPS = {
    previewMode: "desktop",
    themesLibrary: [],
    panelPosition: "left",
    logoLink: "/dashboard",
    zoom: 1,
    chatVisible: true,
    contextMenuVisible: false,
    planIndex: 0,
    attributesTab: "page",
    debugMode: false
  };
  return createStore()(
    subscribeWithSelector(
      temporal(
        immer((set, _get) => ({
          ...DEFAULT_PROPS,
          ...initProps,
          updateHistory: () => {
            setTimeout(() => {
              const currentState = _get();
              const state = {
                panel: currentState.panel,
                selectedBrickId: currentState.selectedBrickId,
                selectedSectionId: currentState.selectedBrickId ? void 0 : currentState.selectedSectionId,
                panelPosition: currentState.panelPosition,
                attributesTab: currentState.attributesTab,
                modal: currentState.modal,
                debug: void 0
              };
              const newUrl = new URL(window.location.href);
              Object.entries(state).forEach(([key, value]) => {
                if (!isNil(value) && value.toString().trim() !== "") {
                  newUrl.searchParams.set(key, value.toString());
                } else {
                  newUrl.searchParams.delete(key);
                }
              });
              window.history.pushState(state, "", newUrl.toString());
            }, 100);
          },
          setAttributesGroup: (group) => set((state) => {
            state.attributesGroup = group;
          }),
          setAttributesTab: (tab) => set((state) => {
            state.attributesTab = tab;
          }),
          setIsResizing: (resizing) => set((state) => {
            state.resizing = resizing;
          }),
          setMouseOverPanel: (over) => set((state) => {
            state.isMouseOverPanel = over;
          }),
          setContextMenuVisible: (open) => set((state) => {
            state.contextMenuVisible = open;
          }),
          setDraggingBrickType: (type) => set((state) => {
            state.draggingBrickType = type ?? void 0;
          }),
          setGridConfig: (config) => set((state) => {
            state.gridConfig = config;
          }),
          toggleEditorEnabled: () => set((state) => {
            state.disabled = !state.disabled;
          }),
          toggleChat: () => set((state) => {
            state.chatVisible = !state.chatVisible;
            if (state.chatVisible) {
              state.panel = void 0;
              state.panelPosition = "right";
            }
            state.updateHistory();
          }),
          toggleDebugMode: () => set((state) => {
            state.debugMode = !state.debugMode;
          }),
          setPreviewMode: (mode) => set((state) => {
            state.previewMode = mode;
          }),
          setSettingsVisible: (visible) => set((state) => {
            state.settingsVisible = visible;
            state.updateHistory();
          }),
          toggleSettings: () => set((state) => {
            state.settingsVisible = !state.settingsVisible;
            state.updateHistory();
          }),
          setIsEditingText: (forBrickId) => set((state) => {
            state.isEditingTextForBrickId = forBrickId || void 0;
          }),
          setPanel: (panel) => set((state) => {
            state.panel = panel;
            state.updateHistory();
          }),
          togglePanel: (panel) => set((state) => {
            state.panel = panel && state.panel === panel ? void 0 : panel;
            state.updateHistory();
          }),
          hidePanel: (panel) => set((state) => {
            if (!panel || state.panel === panel) {
              state.panel = void 0;
            }
            state.updateHistory();
          }),
          toggleModal: (modal) => set((state) => {
            state.modal = modal && state.modal === modal ? void 0 : modal;
            state.updateHistory();
          }),
          zoomIn: () => set((state) => {
            state.zoom = Math.min(state.zoom + 0.1, 2);
          }),
          zoomOut: () => set((state) => {
            state.zoom = Math.max(state.zoom - 0.1, 0.5);
          }),
          resetZoom: () => set((state) => {
            state.zoom = 1;
          }),
          setSelectedGroup: (group) => set((state) => {
            state.selectedGroup = group;
          }),
          setSelectedBrickId: (brickId) => set((state) => {
            state.selectedBrickId = brickId;
            if (brickId) {
              state.selectedSectionId = void 0;
            }
            state.updateHistory();
          }),
          setSelectedSectionId: (sectionId) => set((state) => {
            state.selectedSectionId = sectionId;
            if (sectionId) {
              state.selectedBrickId = void 0;
              state.selectedGroup = void 0;
            }
            state.updateHistory();
          }),
          deselectBrick: (brickId) => set((state) => {
            if (state.selectedBrickId && (!brickId || state.selectedBrickId === brickId)) {
              state.selectedBrickId = void 0;
            }
            state.updateHistory();
          }),
          togglePanelPosition: () => set((state) => {
            state.panelPosition = state.panelPosition === "left" ? "right" : "left";
            state.updateHistory();
          }),
          showModal: (modal) => set((state) => {
            state.modal = modal;
            state.updateHistory();
          }),
          hideModal: () => set((state) => {
            state.modal = void 0;
            state.updateHistory();
          })
        })),
        // limit undo history to 100
        {
          limit: 100,
          equality: (pastState, currentState) => isEqual(pastState, currentState)
        }
      )
    )
  );
};
const EditorStoreContext = createContext(null);
const useEditorStoreContext = () => {
  const store = useContext(EditorStoreContext);
  if (!store) {
    console.log("Problem with EditorStoreContext", store);
  }
  invariant(store, "useEditorStoreContext must be used within a EditorStoreContext");
  return store;
};
const useEditor = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx);
};
const usePreviewMode = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.previewMode);
};
const useEditorEnabled = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => !state.disabled);
};
const useLogoLink = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.logoLink);
};
const useSelectedGroup = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.selectedGroup);
};
const useSelectedBrickId = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.selectedBrickId);
};
const useAttributesTab = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.attributesTab ?? "page");
};
const useAttributesGroup = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.attributesGroup);
};
function useSelectedSectionId() {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.selectedSectionId);
}
function useSelectedSection() {
  const ctx = useEditorStoreContext();
  const draft = usePageContext();
  return useStore(ctx, (state) => {
    const section = draft.getState().page.sections.find((s) => s.id === state.selectedSectionId);
    if (!section) {
      return null;
    }
    return section;
  });
}
const useDebugMode = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => !!state.debugMode);
};
const usePanel = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => ({
    panel: state.panel,
    panelPosition: state.panelPosition
  }));
};
const useModal = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.modal);
};
const useIsPremiumPlan = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.planIndex > 0);
};
function useHasPlanOrHigher(plan) {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.planIndex >= plan);
}
const useThemesLibrary = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.themesLibrary);
};
const useZoom = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => ({
    zoom: state.zoom,
    zoomIn: state.zoomIn,
    zoomOut: state.zoomOut,
    canZoomIn: state.zoom < 2,
    canZoomOut: state.zoom > 0.5,
    resetZoom: state.resetZoom
  }));
};
const useChatSession = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.chatSession);
};
const useChatVisible = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => !!state.chatVisible);
};
const useIsResizing = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.resizing);
};
function useEditingTextForBrickId() {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.isEditingTextForBrickId);
}
const useDraggingBrickType = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.draggingBrickType);
};
const useContextMenuVisible = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => state.contextMenuVisible);
};
const useEditorHelpers = () => {
  const ctx = useEditorStoreContext();
  return useStore(ctx, (state) => ({
    setAttributesGroup: state.setAttributesGroup,
    setAttributesTab: state.setAttributesTab,
    setIsResizing: state.setIsResizing,
    setMouseOverPanel: state.setMouseOverPanel,
    toggleDebugMode: state.toggleDebugMode,
    setContextMenuVisible: state.setContextMenuVisible,
    setDraggingBrickType: state.setDraggingBrickType,
    setPreviewMode: state.setPreviewMode,
    setSettingsVisible: state.setSettingsVisible,
    setGridConfig: state.setGridConfig,
    toggleSettings: state.toggleSettings,
    setIsEditingText: state.setIsEditingText,
    setPanel: state.setPanel,
    togglePanel: state.togglePanel,
    toggleModal: state.toggleModal,
    hidePanel: state.hidePanel,
    setSelectedGroup: state.setSelectedGroup,
    setSelectedBrickId: state.setSelectedBrickId,
    setSelectedSectionId: state.setSelectedSectionId,
    deselectBrick: state.deselectBrick,
    togglePanelPosition: state.togglePanelPosition,
    showModal: state.showModal,
    hideModal: state.hideModal,
    toggleEditorEnabled: state.toggleEditorEnabled,
    onPublish: state.onPublish,
    onSavePage: state.onSavePage,
    onSaveSite: state.onSaveSite,
    onPageCreated: state.onPageCreated,
    toggleChat: state.toggleChat,
    onShowPopup: state.onShowPopup
  }));
};
function useGridConfig() {
  const ctx = useEditorStoreContext();
  return useStore(
    ctx,
    (state) => state.gridConfig ?? {
      colWidth: 20,
      rowHeight: LAYOUT_ROW_HEIGHT
    }
  );
}
export {
  useThemesLibrary as A,
  useIsResizing as B,
  EditorStoreContext as E,
  LAYOUT_COLS as L,
  usePreviewMode as a,
  useSelectedBrickId as b,
  useSelectedSectionId as c,
  useChatVisible as d,
  usePanel as e,
  useDebugMode as f,
  createEditorStore as g,
  useLogoLink as h,
  useModal as i,
  useZoom as j,
  useChatSession as k,
  LAYOUT_ROW_HEIGHT as l,
  useEditingTextForBrickId as m,
  useDraggingBrickType as n,
  useGridConfig as o,
  useAttributesTab as p,
  useAttributesGroup as q,
  useSelectedSection as r,
  useEditor as s,
  useIsPremiumPlan as t,
  useEditorHelpers as u,
  useContextMenuVisible as v,
  useEditorStoreContext as w,
  useEditorEnabled as x,
  useSelectedGroup as y,
  useHasPlanOrHigher as z
};
//# sourceMappingURL=use-editor.BcGWPSSE.js.map
