"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var toolbar_context_exports = {};
__export(toolbar_context_exports, {
  ToolbarContextProvider: () => ToolbarContextProvider,
  getIsStaticTextToolbarEnabled: () => getIsStaticTextToolbarEnabled,
  getToolbarSettings: () => getToolbarSettings,
  useToolbarSettings: () => useToolbarSettings
});
module.exports = __toCommonJS(toolbar_context_exports);
var import_react = __toESM(require("react"));
var import_default_text_formatting_buttons = require("./../ricos-plugins/toolbar-utils/default-text-formatting-buttons.js");
var import_merge_toolbar_settings = require("./../ricos-plugins/toolbar-utils/merge-toolbar-settings.js");
var import_get_selected_nodes = require("./../_shared/get-selected-nodes.js");
var import_use_context_selector = require("./../_shared/use-context-selector.js");
var import_editor_common = require("./../editor-common/index.js");
var import_default_toolbar_settings = require("./../toolbars-v3/default-toolbar-settings.js");
var import_editor_context = require("./editor-context.js");
var import_plugins_context = require("./plugins-context.js");
var import_ricos_context = require("./ricos-context.js");
const ToolbarStateContext = (0, import_use_context_selector.createContext)({});
const pluginButtonNames = Object.values(import_editor_common.INSERT_PLUGIN_BUTTONS);
const textButtons = {
  mobile: import_default_text_formatting_buttons.mobileTextButtonList,
  desktop: import_default_text_formatting_buttons.desktopTextButtonList
};
const ToolbarContextProvider = ({
  toolbarSettings = {},
  onSelectorContextValue,
  children
}) => {
  const {
    adapter: { tiptapEditor }
  } = (0, import_react.useContext)(import_editor_context.EditorContext);
  const { content } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const [selectedNodes, setSelectedNodes] = import_react.default.useState([]);
  (0, import_react.useEffect)(() => {
    const updateContent = () => {
      const newSelectedNodes = (0, import_get_selected_nodes.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__ */ import_react.default.createElement(
    ToolbarStateContext.Provider,
    {
      value: import_react.default.useMemo(
        () => ({
          resolvedToolbarSettings,
          ...restToolbarSettings
        }),
        [
          resolvedToolbarSettings,
          restToolbarSettings.staticToolbarAlignment,
          restToolbarSettings.textToolbarContainer,
          restToolbarSettings.useStaticTextToolbar,
          selectedNodes
        ]
      ),
      onSelectorContextValue
    },
    children
  );
};
const useToolbarSettings = (selector, trackingString) => {
  return (0, import_use_context_selector.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 = (0, import_react.useContext)(import_plugins_context.PluginsContext);
  return import_react.default.useMemo(() => {
    const pluginButtons = plugins.getAddButtons().map((b) => ({
      buttonSettings: { name: b.id, toolbars: b.toolbars },
      component: b.icon,
      blockType: "node"
    }));
    const defaultSettings = (0, import_default_toolbar_settings.getDefaultToolbarSettings)({
      textButtons,
      pluginButtons,
      pluginButtonNames
    });
    const customSettings = toolbarSettings?.getToolbarSettings?.({
      textButtons,
      pluginButtons,
      pluginButtonNames
    }) || [];
    return (0, import_merge_toolbar_settings.mergeToolbarSettings)({ defaultSettings, customSettings });
  }, [plugins, toolbarSettings]);
};
//# sourceMappingURL=toolbar-context.js.map
