"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_input_rule_handler_exports = {};
__export(list_input_rule_handler_exports, {
  createListInputRuleHandler: () => createListInputRuleHandler
});
module.exports = __toCommonJS(list_input_rule_handler_exports);
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_tiptap = require("./../tiptap/index.js");
var import_transform = require("./../tiptap/pm/transform.js");
const createListInputRuleHandler = ({ type, getAttributes, getItemAttributes, joinPredicate }) => ({ state, range, match }) => {
  const attributes = (0, import_tiptap.callOrReturn)(getAttributes, void 0, match) || {};
  const itemAttributes = (0, import_tiptap.callOrReturn)(getItemAttributes, void 0, match) || {};
  const { from, to } = state.selection;
  let shouldApplyRule = true;
  state.doc.nodesBetween(from, to, (node, pos) => {
    const $pos = state.doc.resolve(pos);
    if (node.isTextblock && node.type.name !== import_v1.Node_Type.PARAGRAPH && node.type.name !== import_v1.Node_Type.HEADING || $pos.parent.type.name === import_v1.Node_Type.LIST_ITEM) {
      shouldApplyRule = false;
      return false;
    }
  });
  if (!shouldApplyRule) {
    return null;
  }
  const tr = state.tr.delete(range.from, range.to);
  const $start = tr.doc.resolve(range.from);
  const blockRange = $start.blockRange();
  const wrapping = blockRange && (0, import_transform.findWrapping)(blockRange, type, attributes);
  if (!wrapping) {
    return null;
  }
  tr.wrap(blockRange, wrapping);
  if (Object.keys(itemAttributes).length > 0) {
    const resolvedPos = tr.doc.resolve(Math.min(range.from + 1, tr.doc.content.size));
    const listItem = (0, import_tiptap.findParentNodeClosestToPos)(
      resolvedPos,
      (node) => node.type.name === import_v1.Node_Type.LIST_ITEM
    );
    if (listItem) {
      tr.setNodeMarkup(listItem.pos, listItem.node.type, {
        ...listItem.node.attrs,
        ...itemAttributes
      });
    }
  }
  const before = tr.doc.resolve(range.from - 1).nodeBefore;
  if (before && before.type === type && (0, import_transform.canJoin)(tr.doc, range.from - 1) && (!joinPredicate || joinPredicate(match, before))) {
    tr.join(range.from - 1);
  }
};
//# sourceMappingURL=list-input-rule-handler.js.map
