"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_toolbars_items_config_exports = {};
__export(use_toolbars_items_config_exports, {
  useToolbarsItemsConfig: () => useToolbarsItemsConfig
});
module.exports = __toCommonJS(use_toolbars_items_config_exports);
var import_function = require("fp-ts-esm/function");
var import_Predicate = require("fp-ts-esm/Predicate");
var import_react = require("react");
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_toolbar_context = require("./../../../ricos-context/toolbar-context.js");
var import_ricos_modals = require("./../../../ricos-modals/index.js");
var import_get_buttons_list_from_config = require("./../../../ricos-plugins/toolbar-utils/get-buttons-list-from-config.js");
var import_ricos_styles = require("./../../../ricos-styles/index.js");
var import_ricos_types = require("./../../../ricos-types/index.js");
var import_is_always_visible = require("./../../../_shared/resolvers/is-always-visible.js");
var import_default_toolbar_items_configs = require("../../default-toolbar-items-configs.js");
var import_map_buttons_from_config = require("../../map-buttons-from-config.js");
var import_to_resolved_attributes = require("../../to-resolved-attributes.js");
var import_to_schema_base_id = require("../../to-schema-base-id.js");
const useToolbarsItemsConfig = (toolbarType, options) => {
  const { isMobile, t, platform } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const plugins = (0, import_react.useContext)(import_ricos_context.PluginsContext);
  const pluginsEvents = (0, import_react.useContext)(import_ricos_context.PluginsEventsContext);
  const shortcuts = (0, import_react.useContext)(import_ricos_context.ShortcutsContext);
  const modalService = (0, import_ricos_modals.useModalService)();
  const toolbarSettingsFunctions = (0, import_toolbar_context.useToolbarSettings)((0, import_toolbar_context.getToolbarSettings)(toolbarType));
  const { styles } = (0, import_react.useContext)(import_ricos_styles.StylesContext);
  const {
    getPublicCommands,
    adapter: { tiptapEditor }
  } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  return (0, import_react.useMemo)(() => {
    return toToolbarItemsConfig({
      buttons: plugins.getTextButtons(),
      toolbarSettingsFunctions,
      toolbarType,
      isMobile,
      publicCommands: getPublicCommands(),
      editor: tiptapEditor,
      modals: modalService,
      pluginsEvents,
      t,
      shortcuts,
      platform,
      options,
      styles
    });
  }, [
    plugins,
    isMobile,
    getPublicCommands,
    tiptapEditor,
    options?.disabled,
    pluginsEvents,
    shortcuts,
    t,
    modalService,
    toolbarType,
    styles
  ]);
};
const toToolbarItemsConfig = ({
  buttons,
  toolbarSettingsFunctions,
  toolbarType,
  publicCommands,
  editor,
  modals,
  pluginsEvents,
  t,
  isMobile,
  shortcuts,
  platform,
  options,
  styles
}) => {
  const toolbarItemConfigs = [
    ...buttons.map(
      (button) => toToolbarItemConfig({
        button,
        publicCommands,
        editor,
        toolbarType,
        modals,
        pluginsEvents,
        t,
        isMobile,
        options,
        tooltipHint: shortcuts.getShortcutDisplayData(
          button.id,
          t,
          platform
        ).tooltipHint,
        styles
      })
    ),
    ...import_default_toolbar_items_configs.defaultToolbarItemsConfigs
  ];
  const toolbarItemsConfig = toTiptapToolbarItemsConfig(
    toolbarSettingsFunctions,
    toolbarItemConfigs,
    toolbarType,
    isMobile ? "mobile" : "desktop"
  );
  return cleanSeparators(toolbarItemsConfig);
};
const toToolbarItemConfig = ({
  button,
  publicCommands,
  editor,
  toolbarType,
  modals,
  pluginsEvents,
  t,
  isMobile,
  tooltipHint,
  options,
  styles
}) => {
  const attributes = (0, import_to_resolved_attributes.toResolvedAttributes)(button, options);
  if (button.type === "separator") {
    return {
      ...button,
      presentation: {
        ...button.presentation || {}
      },
      attributes,
      commands: {}
    };
  }
  return {
    ...button,
    presentation: {
      ...button.presentation || {},
      icon: "getIcon" in button.presentation ? button.presentation.getIcon({ publicCommands, t }) : button.presentation?.icon,
      tooltip: `${t(button.presentation.tooltip)} ${tooltipHint ? `(${tooltipHint})` : ""}`
    },
    attributes,
    commands: {
      click: ({ editorQuery }) => ({ referenceElement, ...rest } = {}) => {
        pluginsEvents.publishers.byTopic("ricos.toolbars.functionality.buttonClick").publish({
          toolbarType,
          buttonId: button.id
        });
        if ("command" in button && button.command) {
          return button.command?.({
            publicCommands,
            modalService: modals,
            editor,
            editorQuery,
            referenceElement,
            isMobile,
            componentProps: "getComponentProps" in button.presentation ? button.presentation.getComponentProps({
              publicCommands,
              t,
              modals,
              styles,
              editor
            }) : void 0
          })(rest);
        }
      }
    }
  };
};
const toTiptapToolbarItemsConfig = (toolbarConfig, tiptapToolbarConfig, toolbarType, buttonsType) => {
  if (!toolbarConfig) {
    console.error(`${toolbarType} doesn't exists`);
    return [];
  }
  const buttonsListFromConfig = (0, import_get_buttons_list_from_config.getButtonsListFromConfig)(toolbarConfig, buttonsType);
  const customButtons = toolbarConfig.getTextPluginButtons?.()?.[buttonsType];
  const buttonsList = buttonsListFromConfig.map(import_map_buttons_from_config.mapButtonsFromConfig);
  const tiptapToolbarItemsConfig = buttonsList.flatMap((button) => {
    const buttonConfig = tiptapToolbarConfig.find((config) => config.id === (0, import_to_schema_base_id.toSchemaBasedId)(button));
    if (buttonConfig) {
      return [buttonConfig];
    }
    if (customButtons?.[button]) {
      return [
        {
          id: button,
          type: "custom",
          attributes: {
            visible: import_is_always_visible.isAlwaysVisible
          },
          commands: {},
          presentation: {
            component: customButtons[button].component,
            dataHook: "customButton"
          }
        }
      ];
    }
    return [];
  });
  const finalTiptapToolbarItemsConfig = cleanTitleIfNeeded(tiptapToolbarItemsConfig);
  return finalTiptapToolbarItemsConfig;
};
const cleanTitleIfNeeded = (tiptapToolbarItemsConfig) => {
  if (tiptapToolbarItemsConfig.some((button) => button.id === `${import_ricos_types.Node_Type.HEADING}.dropdown`) && tiptapToolbarItemsConfig.some((button) => button.id === `${import_ricos_types.Node_Type.HEADING}.title`)) {
    return tiptapToolbarItemsConfig.filter((button) => button.id !== `${import_ricos_types.Node_Type.HEADING}.title`);
  } else {
    return tiptapToolbarItemsConfig;
  }
};
const isSeparator = (button) => button && button.id === "separator";
const notSeparator = (0, import_Predicate.not)(isSeparator);
const filterAdjacent = (buttons) => buttons.filter(
  (b, i) => notSeparator(buttons[i - 1]) && isSeparator(b) || isSeparator(buttons[i - 1]) && notSeparator(b) || notSeparator(buttons[i - 1]) && notSeparator(b)
);
const filterEdges = (buttons) => buttons.filter(
  (b, i) => !(isSeparator(b) && i === 0) && !(isSeparator(b) && i === buttons.length - 1)
);
const cleanSeparators = (0, import_function.flow)(filterAdjacent, filterEdges);
//# sourceMappingURL=use-toolbars-items-config.js.map
