import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { Mark_Type } from "./../../ricos-types/index.js";
import { getBlockDefaultFontSize } from "./../../_shared/list-checkbox/get-block-default-font-size.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import { HEADER_NAMES } from "../title/extension.js";
import { onlyChangesUnrelatedAttrs } from "../transaction-predicates/only-changes-unrelated-attrs.js";
const RELEVANT_ATTRS = /* @__PURE__ */ new Set(["level"]);
const createListItemSizingDecorationPlugin = () => {
  const key = new PluginKey("list-item-sizing");
  return new Plugin({
    key,
    state: {
      init: (_config, state) => buildListItemSizingDecorations(state.doc),
      // No `reuseWhen` predicate — see plugin docstring for why.
      apply: (tr, prev) => {
        if (!tr.docChanged) {
          return prev;
        }
        if (onlyChangesUnrelatedAttrs(tr, RELEVANT_ATTRS)) {
          return prev;
        }
        return buildListItemSizingDecorations(tr.doc);
      }
    },
    props: {
      decorations(state) {
        return key.getState(state);
      }
    }
  });
};
const buildListItemSizingDecorations = (doc) => {
  const decorations = [];
  doc.descendants((node, pos) => {
    if (node.isTextblock) {
      return false;
    }
    if (node.type.name !== Node_Type.LIST_ITEM) {
      return;
    }
    const firstChildOfListItem = node.firstChild;
    const headingLevel = firstChildOfListItem?.type.name === Node_Type.HEADING ? firstChildOfListItem?.attrs.level || 1 : void 0;
    const childFontSize = Number(
      firstChildOfListItem?.firstChild?.marks.find((mark) => mark.type.name === Mark_Type.FONT_SIZE)?.attrs.value
    );
    if (childFontSize) {
      decorations.push(
        Decoration.node(pos, pos + node.nodeSize, {
          style: `font-size: ${childFontSize}px;`,
          // When the chosen font is smaller than the block's own default, the
          // block's strut keeps the line (and marker) oversized; flag the item
          // so the child paragraph/heading shrinks its font to match — see
          // `list-checkbox.scss`.
          ...childFontSize < getBlockDefaultFontSize(headingLevel) ? { "data-child-font-fit": "true" } : {}
        })
      );
    }
    if (headingLevel !== void 0) {
      decorations.push(
        Decoration.node(pos, pos + node.nodeSize, {
          "data-heading-level": `header${HEADER_NAMES[headingLevel - 1]}`
        })
      );
    }
  });
  return DecorationSet.create(doc, decorations);
};
export {
  createListItemSizingDecorationPlugin
};
//# sourceMappingURL=list-item-sizing.js.map
