import React, { useContext, useEffect } from "react";
import {
  desktopTextButtonList,
  mobileTextButtonList
} from "./../ricos-plugins/toolbar-utils/default-text-formatting-buttons.js";
import { mergeToolbarSettings } from "./../ricos-plugins/toolbar-utils/merge-toolbar-settings.js";
import { getSelectedNodes } from "./../_shared/get-selected-nodes.js";
import { createContext, useContextSelector } from "./../_shared/use-context-selector.js";
import { INSERT_PLUGIN_BUTTONS } from "./../editor-common/index.js";
import { getDefaultToolbarSettings } from "./../toolbars-v3/default-toolbar-settings.js";
import { EditorContext } from "./editor-context.js";
import { PluginsContext } from "./plugins-context.js";
import { RicosContext } from "./ricos-context.js";
const ToolbarStateContext = createContext({});
const pluginButtonNames = Object.values(INSERT_PLUGIN_BUTTONS);
const textButtons = {
  mobile: mobileTextButtonList,
  desktop: desktopTextButtonList
};
const ToolbarContextProvider = ({
  toolbarSettings = {},
  onSelectorContextValue,
  children
}) => {
  const {
    adapter: { tiptapEditor }
  } = useContext(EditorContext);
  const { content } = useContext(RicosContext);
  const [selectedNodes, setSelectedNodes] = React.useState([]);
  useEffect(() => {
    const updateContent = () => {
      const newSelectedNodes = getSelectedNodes(tiptapEditor);
      setSelectedNodes(newSelectedNodes);
      content.update(newSelectedNodes, tiptapEditor);
    };
    const onTransaction = ({ transaction }) => {
      if (transaction.storedMarksSet || transaction.getMeta("forceToolbarUpdate")) {
        updateContent();
      }
    };
    tiptapEditor.on("transaction", onTransaction);
    tiptapEditor.on("update", updateContent);
    tiptapEditor.on("focus", updateContent);
    tiptapEditor.on("selectionUpdate", updateContent);
    return () => {
      tiptapEditor.off("transaction", onTransaction);
      tiptapEditor.off("update", updateContent);
      tiptapEditor.off("focus", updateContent);
      tiptapEditor.off("selectionUpdate", updateContent);
    };
  }, []);
  const resolvedToolbarSettings = useResolveToolbarSettings(toolbarSettings);
  const { getToolbarSettings: getToolbarSettings2, ...restToolbarSettings } = toolbarSettings;
  return /* @__PURE__ */ React.createElement(
    ToolbarStateContext.Provider,
    {
      value: React.useMemo(
        () => ({
          resolvedToolbarSettings,
          ...restToolbarSettings
        }),
        [
          resolvedToolbarSettings,
          restToolbarSettings.staticToolbarAlignment,
          restToolbarSettings.textToolbarContainer,
          restToolbarSettings.useStaticTextToolbar,
          selectedNodes
        ]
      ),
      onSelectorContextValue
    },
    children
  );
};
const useToolbarSettings = (selector, trackingString) => {
  return useContextSelector(ToolbarStateContext, selector, Object.is, trackingString);
};
const getToolbarSettings = (toolbarType) => (state) => {
  const settings = state.resolvedToolbarSettings.find((setting) => setting.name === toolbarType);
  return settings;
};
const getIsStaticTextToolbarEnabled = (state) => typeof state.useStaticTextToolbar === "boolean" ? state.useStaticTextToolbar : typeof state.useStaticTextToolbar === "object" ? !state.useStaticTextToolbar.disabled : false;
const useResolveToolbarSettings = (toolbarSettings) => {
  const plugins = useContext(PluginsContext);
  return React.useMemo(() => {
    const pluginButtons = plugins.getAddButtons().map((b) => ({
      buttonSettings: { name: b.id, toolbars: b.toolbars },
      component: b.icon,
      blockType: "node"
    }));
    const defaultSettings = getDefaultToolbarSettings({
      textButtons,
      pluginButtons,
      pluginButtonNames
    });
    const customSettings = toolbarSettings?.getToolbarSettings?.({
      textButtons,
      pluginButtons,
      pluginButtonNames
    }) || [];
    return mergeToolbarSettings({ defaultSettings, customSettings });
  }, [plugins, toolbarSettings]);
};
export {
  ToolbarContextProvider,
  getIsStaticTextToolbarEnabled,
  getToolbarSettings,
  useToolbarSettings
};
//# sourceMappingURL=toolbar-context.js.map
