import {
  ACTION_BUTTON_TYPE,
  ANCHOR_TYPE,
  APP_EMBED_TYPE,
  AUDIO_TYPE,
  CODE_BLOCK_TYPE,
  COLLAPSIBLE_LIST_TYPE,
  DIVIDER_TYPE,
  EXTERNAL,
  FILE_UPLOAD_TYPE,
  GALLERY_TYPE,
  GIPHY_TYPE,
  HEADINGS_DROPDOWN_TYPE,
  HTML_TYPE,
  IMAGE_TYPE,
  INDENT_TYPE,
  LINE_SPACING_TYPE,
  LINK_BUTTON_TYPE,
  LINK_PREVIEW_TYPE,
  LINK_TYPE,
  MENTION_TYPE,
  POLL_TYPE,
  SPOILER_TYPE,
  TABLE_TYPE,
  TEXT_COLOR_TYPE,
  VIDEO_TYPE,
  Version
} from "./../ricos-content/index.js";
import { Decoration_Type, Node_Type } from "./../ricos-types/index.js";
import { TOOLBARS } from "./../editor-common/index.js";
const getBiCallbackToSubscribe = (events, helpersRef) => (topic, name, eventDataToBICallbackParams) => {
  return events.subscribe(
    topic,
    (_topic, data) => {
      const callback = helpersRef.current[name];
      if (!callback) {
        return;
      }
      const params = eventDataToBICallbackParams(data);
      callback.call(callback, ...params);
    },
    `${name} BI callback`
  );
};
const toPluginId = (id) => id.split(".")[0] ?? id;
const toButtonId = (id) => id.split(".")[1] ?? id;
const toDraftId = (id) => ({
  INDENT: INDENT_TYPE,
  LINE_SPACING: LINE_SPACING_TYPE,
  TextAlignment: "textAlignment",
  [Decoration_Type.ANCHOR]: ANCHOR_TYPE,
  [Decoration_Type.BOLD]: "Bold",
  [Decoration_Type.COLOR]: TEXT_COLOR_TYPE,
  [Decoration_Type.FONT_SIZE]: "FONT_SIZE",
  [Decoration_Type.ITALIC]: "Italic",
  [Decoration_Type.LINK]: LINK_TYPE,
  [Decoration_Type.MENTION]: MENTION_TYPE,
  [Decoration_Type.SPOILER]: SPOILER_TYPE,
  [Decoration_Type.UNDERLINE]: "Underline",
  [Decoration_Type.STRIKETHROUGH]: "Strikethrough",
  [Node_Type.APP_EMBED]: APP_EMBED_TYPE,
  [Node_Type.AUDIO]: AUDIO_TYPE,
  [Node_Type.BLOCKQUOTE]: "Blockquote",
  [Node_Type.BULLETED_LIST]: "unordered-list-item",
  [Node_Type.CHECKBOX_LIST]: "checkbox-list-item",
  [Node_Type.BUTTON]: LINK_BUTTON_TYPE,
  LINK_BUTTON: LINK_BUTTON_TYPE,
  ACTION_BUTTON: ACTION_BUTTON_TYPE,
  [Node_Type.CODE_BLOCK]: CODE_BLOCK_TYPE,
  [Node_Type.COLLAPSIBLE_LIST]: COLLAPSIBLE_LIST_TYPE,
  [Node_Type.DIVIDER]: DIVIDER_TYPE,
  [Node_Type.EMBED]: LINK_PREVIEW_TYPE,
  [Node_Type.EXTERNAL]: EXTERNAL,
  [Node_Type.FILE]: FILE_UPLOAD_TYPE,
  [Node_Type.GALLERY]: GALLERY_TYPE,
  [Node_Type.GIF]: GIPHY_TYPE,
  [Node_Type.HEADING]: HEADINGS_DROPDOWN_TYPE,
  [Node_Type.HTML]: HTML_TYPE,
  [Node_Type.IMAGE]: IMAGE_TYPE,
  [Node_Type.LINK_PREVIEW]: LINK_PREVIEW_TYPE,
  [Node_Type.ORDERED_LIST]: "ordered-list-item",
  [Node_Type.POLL]: POLL_TYPE,
  [Node_Type.TABLE]: TABLE_TYPE,
  [Node_Type.VIDEO]: VIDEO_TYPE
})[id] ?? id;
function mapBiCallbacksToSubscriptions(helpersRef, events, schema) {
  const version = Version.currentVersion;
  const contentId = schema.getID();
  const subscribeCallback = getBiCallbackToSubscribe(events, helpersRef);
  subscribeCallback("ricos.editor.functionality.firstEdit", "onContentEdited", () => [
    { version, contentId, editorEngine: "tiptap" }
  ]);
  subscribeCallback(
    "ricos.shortcuts.functionality.applied",
    "onKeyboardShortcutAction",
    ({ shortcutName }) => [
      {
        buttonName: toDraftId(toPluginId(shortcutName)),
        pluginId: toDraftId(toPluginId(shortcutName)),
        version,
        contentId
      }
    ]
  );
  subscribeCallback(
    "ricos.toolbars.functionality.buttonClick",
    "onToolbarButtonClick",
    // @ts-expect-error $TSFixMe
    ({ toolbarType, buttonId }) => [
      {
        version,
        contentId,
        buttonName: toDraftId(toPluginId(buttonId)),
        type: toolbarType,
        editorEngine: "tiptap"
      }
    ]
  );
  subscribeCallback(
    "ricos.modals.functionality.modalOpened",
    "onPluginModalOpened",
    ({ id, entryType, dropdownValue, modalSessionId }) => [
      {
        version,
        contentId,
        pluginId: toDraftId(toPluginId(id)),
        entryType: entryType ?? TOOLBARS.INLINE,
        dropdownValue: dropdownValue ?? "",
        modalSessionId,
        // TODO: rework this
        pluginDetails: { modalId: id },
        entryPoint: entryType ?? TOOLBARS.INLINE,
        editorEngine: "tiptap"
      }
    ]
  );
  subscribeCallback(
    "ricos.upload.functionality.uploadStarted",
    "onMediaUploadStart",
    ({ correlationId, pluginId, fileSize, mediaType }) => [
      correlationId,
      pluginId,
      fileSize,
      mediaType,
      version,
      contentId
    ]
  );
  subscribeCallback(
    "ricos.upload.functionality.uploadFinished",
    "onMediaUploadEnd",
    ({ correlationId, pluginId, duration, fileSize, mediaType, isSuccess, errorType }) => [
      correlationId,
      pluginId,
      duration,
      fileSize,
      mediaType,
      isSuccess,
      errorType,
      version,
      contentId
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginAddSuccess",
    "onPluginAddSuccess",
    ({ pluginId, params, entryPoint }) => [
      toDraftId(toPluginId(pluginId)),
      entryPoint,
      params,
      version,
      contentId,
      "tiptap"
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginAdd",
    "onPluginAdd",
    ({ pluginId, entryPoint }) => [
      toDraftId(toPluginId(pluginId)),
      entryPoint,
      version,
      contentId,
      "tiptap"
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginDelete",
    "onPluginDelete",
    ({ pluginId, pluginDetails }) => [
      {
        pluginId: toDraftId(toPluginId(pluginId)),
        pluginDetails,
        version,
        contentId,
        editorEngine: "tiptap"
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginToolbarButtonClick",
    "onToolbarButtonClick",
    ({ pluginId, buttonName, value, nodeId, type }) => [
      {
        pluginId: toDraftId(toPluginId(pluginId)),
        buttonName: toButtonId(buttonName),
        value,
        pluginUniqueId: nodeId,
        type,
        version,
        contentId,
        editorEngine: "tiptap"
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginPopoverClick",
    "onPluginsPopOverClick",
    ({ pluginId, buttonName }) => [{ pluginId, buttonName }]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginPopoverTabSwitch",
    "onPluginsPopOverTabSwitch",
    ({ pluginId, buttonName }) => [{ pluginId, buttonName }]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginPopoverSearch",
    "onPluginAction",
    ({ pluginId, searchTerm }) => ["searchInsideThePlugin", { plugin_id: pluginId, searchTerm }]
  );
  subscribeCallback(
    "ricos.plugins.functionality.modalAction",
    "onModalAction",
    ({ pluginId, modalId, actionName, value, modalSessionId, actionNumber }) => [
      { pluginId, modalId, value, actionName, modalSessionId, actionNumber }
    ]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginChangeSettings",
    "onChangePluginSettings",
    ({ pluginId, nodeId, actionName, value }) => [{ pluginId, nodeId, actionName, value }]
  );
  subscribeCallback(
    "ricos.plugins.functionality.pluginLinkable",
    "onPluginAction",
    ({ pluginId, nodeId, link, nofollow, noreferrer, newTab, anchor }) => [
      "addPluginLink",
      {
        plugin_id: toDraftId(toPluginId(pluginId)),
        nodeId,
        params: {
          category: link ? "web_address" : "section",
          link,
          nofollow,
          noreferrer,
          newTab,
          anchor
        }
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.table.addColumnRow",
    "onPluginAction",
    ({ pluginId, nodeId, category, location, source }) => [
      "tablePluginAddColumnRow",
      {
        plugin_id: toDraftId(toPluginId(pluginId)),
        nodeId,
        category,
        location,
        source
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.table.contextMenuClick",
    "onPluginAction",
    ({ pluginId, nodeId, category }) => [
      "tablePluginClickOnOptionMenu",
      {
        plugin_id: toDraftId(toPluginId(pluginId)),
        nodeId,
        category
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.table.deleteColumnRow",
    "onPluginAction",
    ({ pluginId, nodeId, category }) => [
      "tablePluginDeleteColumnRow",
      {
        plugin_id: toDraftId(toPluginId(pluginId)),
        nodeId,
        category
      }
    ]
  );
  subscribeCallback(
    "ricos.plugins.table.contextMenuOptionClick",
    "onPluginAction",
    ({ pluginId, nodeId, category, actionName }) => [
      "tablePluginClickActionFromOptionMenu",
      {
        plugin_id: toDraftId(toPluginId(pluginId)),
        nodeId,
        category,
        actionName
      }
    ]
  );
}
export {
  mapBiCallbacksToSubscriptions
};
//# sourceMappingURL=map-bi-callbacks-to-events.js.map
