"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 map_bi_callbacks_to_events_exports = {};
__export(map_bi_callbacks_to_events_exports, {
  mapBiCallbacksToSubscriptions: () => mapBiCallbacksToSubscriptions
});
module.exports = __toCommonJS(map_bi_callbacks_to_events_exports);
var import_ricos_content = require("./../ricos-content/index.js");
var import_ricos_types = require("./../ricos-types/index.js");
var import_editor_common = require("./../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: import_ricos_content.INDENT_TYPE,
  LINE_SPACING: import_ricos_content.LINE_SPACING_TYPE,
  TextAlignment: "textAlignment",
  [import_ricos_types.Decoration_Type.ANCHOR]: import_ricos_content.ANCHOR_TYPE,
  [import_ricos_types.Decoration_Type.BOLD]: "Bold",
  [import_ricos_types.Decoration_Type.COLOR]: import_ricos_content.TEXT_COLOR_TYPE,
  [import_ricos_types.Decoration_Type.FONT_SIZE]: "FONT_SIZE",
  [import_ricos_types.Decoration_Type.ITALIC]: "Italic",
  [import_ricos_types.Decoration_Type.LINK]: import_ricos_content.LINK_TYPE,
  [import_ricos_types.Decoration_Type.MENTION]: import_ricos_content.MENTION_TYPE,
  [import_ricos_types.Decoration_Type.SPOILER]: import_ricos_content.SPOILER_TYPE,
  [import_ricos_types.Decoration_Type.UNDERLINE]: "Underline",
  [import_ricos_types.Decoration_Type.STRIKETHROUGH]: "Strikethrough",
  [import_ricos_types.Node_Type.APP_EMBED]: import_ricos_content.APP_EMBED_TYPE,
  [import_ricos_types.Node_Type.AUDIO]: import_ricos_content.AUDIO_TYPE,
  [import_ricos_types.Node_Type.BLOCKQUOTE]: "Blockquote",
  [import_ricos_types.Node_Type.BULLETED_LIST]: "unordered-list-item",
  [import_ricos_types.Node_Type.CHECKBOX_LIST]: "checkbox-list-item",
  [import_ricos_types.Node_Type.BUTTON]: import_ricos_content.LINK_BUTTON_TYPE,
  LINK_BUTTON: import_ricos_content.LINK_BUTTON_TYPE,
  ACTION_BUTTON: import_ricos_content.ACTION_BUTTON_TYPE,
  [import_ricos_types.Node_Type.CODE_BLOCK]: import_ricos_content.CODE_BLOCK_TYPE,
  [import_ricos_types.Node_Type.COLLAPSIBLE_LIST]: import_ricos_content.COLLAPSIBLE_LIST_TYPE,
  [import_ricos_types.Node_Type.DIVIDER]: import_ricos_content.DIVIDER_TYPE,
  [import_ricos_types.Node_Type.EMBED]: import_ricos_content.LINK_PREVIEW_TYPE,
  [import_ricos_types.Node_Type.EXTERNAL]: import_ricos_content.EXTERNAL,
  [import_ricos_types.Node_Type.FILE]: import_ricos_content.FILE_UPLOAD_TYPE,
  [import_ricos_types.Node_Type.GALLERY]: import_ricos_content.GALLERY_TYPE,
  [import_ricos_types.Node_Type.GIF]: import_ricos_content.GIPHY_TYPE,
  [import_ricos_types.Node_Type.HEADING]: import_ricos_content.HEADINGS_DROPDOWN_TYPE,
  [import_ricos_types.Node_Type.HTML]: import_ricos_content.HTML_TYPE,
  [import_ricos_types.Node_Type.IMAGE]: import_ricos_content.IMAGE_TYPE,
  [import_ricos_types.Node_Type.LINK_PREVIEW]: import_ricos_content.LINK_PREVIEW_TYPE,
  [import_ricos_types.Node_Type.ORDERED_LIST]: "ordered-list-item",
  [import_ricos_types.Node_Type.POLL]: import_ricos_content.POLL_TYPE,
  [import_ricos_types.Node_Type.TABLE]: import_ricos_content.TABLE_TYPE,
  [import_ricos_types.Node_Type.VIDEO]: import_ricos_content.VIDEO_TYPE
})[id] ?? id;
function mapBiCallbacksToSubscriptions(helpersRef, events, schema) {
  const version = import_ricos_content.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 ?? import_editor_common.TOOLBARS.INLINE,
        dropdownValue: dropdownValue ?? "",
        modalSessionId,
        // TODO: rework this
        pluginDetails: { modalId: id },
        entryPoint: entryType ?? import_editor_common.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
      }
    ]
  );
}
//# sourceMappingURL=map-bi-callbacks-to-events.js.map
