import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import { getListSymbol, normalizeOffset } from "./../../wix-rich-content-common/index.js";
import { getListItemsWithVisibleIndentation } from "../list-item/commands/commons.js";
import { isInlineTextOnly } from "../transaction-predicates/is-inline-text-only.js";
import { onlyChangesUnrelatedAttrs } from "../transaction-predicates/only-changes-unrelated-attrs.js";
const RELEVANT_ATTRS = /* @__PURE__ */ new Set(["offset"]);
const createIndentationStylingDecorationPlugin = () => {
  const key = new PluginKey("indentation-styling");
  return new Plugin({
    key,
    state: {
      init: (_config, state) => buildIndentationStylingDecorations(state.doc),
      apply: (tr, prev) => {
        if (!tr.docChanged) {
          return prev;
        }
        if (isInlineTextOnly(tr)) {
          return prev.map(tr.mapping, tr.doc);
        }
        if (onlyChangesUnrelatedAttrs(tr, RELEVANT_ATTRS)) {
          return prev;
        }
        return buildIndentationStylingDecorations(tr.doc);
      }
    },
    props: {
      decorations(state) {
        return key.getState(state);
      }
    }
  });
};
const buildIndentationStylingDecorations = (doc) => {
  const decorations = [];
  const listItemsWithVisualIndent = getListItemsWithVisibleIndentation(doc);
  listItemsWithVisualIndent.forEach(({ node, pos, visibleIndentation }) => {
    const normalizedVisualIndent = normalizeOffset(visibleIndentation);
    const marker = getListSymbol(normalizedVisualIndent, node.type.name);
    const isCheckboxList = node.type.name === Node_Type.CHECKBOX_LIST;
    const marginStart = `margin-inline-start: ${normalizeOffset(node.attrs.offset || 0) * 2 + 1.5}em`;
    const style = isCheckboxList ? `${marginStart};` : `list-style-type: ${marker}; ${marginStart};`;
    node.forEach((childNode, childPos) => {
      const childPosInDoc = childPos + pos + 1;
      decorations.push(
        Decoration.node(childPosInDoc, childPosInDoc + childNode.nodeSize, {
          style,
          "aria-level": `${normalizedVisualIndent + 1}`
        })
      );
    });
  });
  return DecorationSet.create(doc, decorations);
};
export {
  createIndentationStylingDecorationPlugin
};
//# sourceMappingURL=indentation-styling.js.map
