import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { ReplaceStep } from "./../../tiptap/pm/transform.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import { getLangDir } from "./../../wix-rich-content-common/Utils/rtl-utils.js";
import { isInlineTextOnly } from "../transaction-predicates/is-inline-text-only.js";
import { onlyChangesUnrelatedAttrs } from "../transaction-predicates/only-changes-unrelated-attrs.js";
const createDirectionAttributeDecorationPlugin = (targetNodeTypes, localeContent) => {
  const types = Array.isArray(targetNodeTypes) ? targetNodeTypes : [targetNodeTypes];
  const targetTypeNames = new Set(types);
  const emptyNodeDirection = getLangDir(localeContent);
  const key = new PluginKey("direction-attribute");
  return new Plugin({
    key,
    state: {
      init: (_config, state) => buildDirectionDecorations(state.doc, targetTypeNames, emptyNodeDirection),
      // The decoration value for a target node is
      //   dir = content.size > 0 ? 'auto' : emptyNodeDirection
      // — it only changes when the node crosses the empty ↔ non-empty
      // boundary. For inline-text-only transactions where no target node
      // crosses that boundary, the cached set can be safely mapped through
      // `tr.mapping` (positions shift, attribute values are unchanged).
      apply: (tr, prev) => {
        if (!tr.docChanged) {
          return prev;
        }
        if (canReuseDirectionDecorations(tr, targetTypeNames)) {
          return prev.map(tr.mapping, tr.doc);
        }
        if (onlyChangesUnrelatedAttrs(tr)) {
          return prev;
        }
        return buildDirectionDecorations(tr.doc, targetTypeNames, emptyNodeDirection);
      }
    },
    props: {
      decorations(state) {
        return key.getState(state);
      }
    }
  });
};
const canReuseDirectionDecorations = (tr, targetTypeNames) => {
  if (!isInlineTextOnly(tr)) {
    return false;
  }
  return tr.steps.every((step, stepIndex) => {
    if (!(step instanceof ReplaceStep)) {
      return false;
    }
    const docBeforeStep = tr.docs[stepIndex];
    const parent = docBeforeStep.resolve(step.from).parent;
    if (!targetTypeNames.has(parent.type.name)) {
      return true;
    }
    const removedSize = step.to - step.from;
    const insertedSize = step.slice.size;
    const newContentSize = parent.content.size - removedSize + insertedSize;
    const wasEmpty = parent.content.size === 0;
    const willBeEmpty = newContentSize === 0;
    return wasEmpty === willBeEmpty;
  });
};
const buildDirectionDecorations = (doc, targetTypeNames, emptyNodeDirection) => {
  const decorations = [];
  doc.descendants((node, pos) => {
    if (node.type.name === Node_Type.LIST_ITEM) {
      return false;
    }
    if (targetTypeNames.has(node.type.name)) {
      decorations.push(
        Decoration.node(pos, pos + node.nodeSize, {
          dir: node.content.size > 0 ? "auto" : emptyNodeDirection
        })
      );
      return false;
    }
    if (node.isTextblock) {
      return false;
    }
  });
  return DecorationSet.create(doc, decorations);
};
export {
  createDirectionAttributeDecorationPlugin
};
//# sourceMappingURL=direction-attribute.js.map
