"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 remove_text_wrapping_plugin_exports = {};
__export(remove_text_wrapping_plugin_exports, {
  createHandleTextWrapPlugin: () => createHandleTextWrapPlugin
});
module.exports = __toCommonJS(remove_text_wrapping_plugin_exports);
var import_consts = require("./../../ricos-content/consts.js");
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_state = require("./../../tiptap/pm/state.js");
const createHandleTextWrapPlugin = () => {
  return new import_state.Plugin({
    key: new import_state.PluginKey("handle-text-wrap"),
    appendTransaction: (_, oldState, newState) => {
      if (newState.doc === oldState.doc) {
        return;
      }
      const tr = newState.tr;
      tr.doc.descendants((node, pos) => {
        if (allowsTextWrap(node.type.name)) {
          return;
        }
        node.forEach((child, childPos) => {
          if (!isTextWrappable(child.type.name)) {
            return false;
          }
          const [tempNode, additionalPos] = isFigure(child.type.name) ? [child.firstChild, 1] : [child, 0];
          tr.setNodeAttribute(childPos + pos + additionalPos + 1, "containerData", {
            ...tempNode?.attrs.containerData ?? {},
            textWrap: false
          });
        });
      });
      const wrappableNodeIdsWithinNotAllowedNodes = [];
      oldState.doc.descendants((node, _pos, parent) => {
        if (isTextWrappable(node.type.name) && !allowsTextWrap(parent?.type.name)) {
          wrappableNodeIdsWithinNotAllowedNodes.push(node.attrs.id);
        }
      });
      tr.doc.descendants((node, pos, parent) => {
        if (wrappableNodeIdsWithinNotAllowedNodes.includes(node.attrs.id) && allowsTextWrap(parent?.type.name)) {
          const [tempNode, additionalPos] = isFigure(node.type.name) ? [node.firstChild, 1] : [node, 0];
          tr.setNodeAttribute(pos + additionalPos, "containerData", {
            ...tempNode?.attrs.containerData ?? {},
            textWrap: true
          });
        }
      });
      return tr;
    }
  });
};
const isTextWrappable = (nodeType) => {
  return [
    import_consts.TIPTAP_FIGURE_TYPE,
    import_consts.TIPTAP_GALLERY_TYPE,
    import_consts.TIPTAP_GIF_TYPE,
    import_consts.TIPTAP_VIDEO_TYPE,
    import_consts.TIPTAP_FILE_TYPE,
    import_consts.TIPTAP_HTML_TYPE,
    import_consts.TIPTAP_LINK_PREVIEW_TYPE,
    import_consts.TIPTAP_LINK_BUTTON_TYPE,
    import_consts.TIPTAP_ACTION_BUTTON_TYPE,
    import_consts.TIPTAP_AUDIO_TYPE
  ].includes(nodeType);
};
const isFigure = (nodeType) => {
  return [import_consts.TIPTAP_FIGURE_TYPE].includes(nodeType);
};
const allowsTextWrap = (nodeType) => {
  return ![import_v1.Node_Type.LIST_ITEM, import_v1.Node_Type.LAYOUT_CELL].includes(nodeType);
};
//# sourceMappingURL=remove-text-wrapping-plugin.js.map
