"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 list_item_sizing_exports = {};
__export(list_item_sizing_exports, {
  createListItemSizingDecorationPlugin: () => createListItemSizingDecorationPlugin
});
module.exports = __toCommonJS(list_item_sizing_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_ricos_types = require("./../../ricos-types/index.js");
var import_get_block_default_font_size = require("./../../_shared/list-checkbox/get-block-default-font-size.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_view = require("./../../tiptap/pm/view.js");
var import_extension = require("../title/extension.js");
var import_only_changes_unrelated_attrs = require("../transaction-predicates/only-changes-unrelated-attrs.js");
const RELEVANT_ATTRS = /* @__PURE__ */ new Set(["level"]);
const createListItemSizingDecorationPlugin = () => {
  const key = new import_state.PluginKey("list-item-sizing");
  return new import_state.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 ((0, import_only_changes_unrelated_attrs.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 !== import_v1.Node_Type.LIST_ITEM) {
      return;
    }
    const firstChildOfListItem = node.firstChild;
    const headingLevel = firstChildOfListItem?.type.name === import_v1.Node_Type.HEADING ? firstChildOfListItem?.attrs.level || 1 : void 0;
    const childFontSize = Number(
      firstChildOfListItem?.firstChild?.marks.find((mark) => mark.type.name === import_ricos_types.Mark_Type.FONT_SIZE)?.attrs.value
    );
    if (childFontSize) {
      decorations.push(
        import_view.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 < (0, import_get_block_default_font_size.getBlockDefaultFontSize)(headingLevel) ? { "data-child-font-fit": "true" } : {}
        })
      );
    }
    if (headingLevel !== void 0) {
      decorations.push(
        import_view.Decoration.node(pos, pos + node.nodeSize, {
          "data-heading-level": `header${import_extension.HEADER_NAMES[headingLevel - 1]}`
        })
      );
    }
  });
  return import_view.DecorationSet.create(doc, decorations);
};
//# sourceMappingURL=list-item-sizing.js.map
