"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 utils_exports = {};
__export(utils_exports, {
  findNodeById: () => findNodeById,
  getCollapsibleListByItemId: () => getCollapsibleListByItemId,
  getCollapsibleListItems: () => getCollapsibleListItems,
  isInCollapsibleList: () => isInCollapsibleList,
  updateCollapsibleListAttrs: () => updateCollapsibleListAttrs,
  updateCollapsibleListItemsAttrs: () => updateCollapsibleListItemsAttrs,
  updateListItemAttributes: () => updateListItemAttributes
});
module.exports = __toCommonJS(utils_exports);
var import_ricos_content = require("./../../ricos-content/index.js");
var import_ricos_plugins = require("./../../ricos-plugins/index.js");
var import_tiptap = require("./../../tiptap/index.js");
function updateCollapsibleListAttrs(commands, nodeId, attrs) {
  commands.updateNodeAttrsById(nodeId, attrs);
}
function updateCollapsibleListItemsAttrs(state, commands, nodeId, { initialExpandedItems }) {
  const collapsibleListNode = findNodeById(state, nodeId)[0].node;
  const nodes = (0, import_ricos_plugins.getFragmentContentNodes)(collapsibleListNode.content);
  nodes.forEach((node, index) => {
    const isExpanded = initialExpandedItems === "ALL" || index === 0 && initialExpandedItems === "FIRST";
    updateListItemAttributes(commands, node, {
      isExpanded
    });
  });
}
function updateListItemAttributes(commands, node, attrs) {
  commands.updateNodeAttrsById(node.attrs.id, attrs);
  const nodeContent = node.content;
  if (!!nodeContent && "content" in nodeContent && Array.isArray(nodeContent.content) && nodeContent.content[1]) {
    commands.updateNodeAttrsById(nodeContent.content[1].attrs.id, attrs);
  }
}
function isInCollapsibleList($pos, nodeId) {
  const collapsibleListNode = (0, import_tiptap.findParentNodeClosestToPos)(
    $pos,
    (node) => node.type?.name === import_ricos_content.TIPTAP_COLLAPSIBLE_LIST_TYPE
  )?.node;
  const collapsibleListNodeContent = collapsibleListNode?.content;
  if (!collapsibleListNodeContent) {
    return false;
  }
  const fragmentNodes = (0, import_ricos_plugins.getFragmentContentNodes)(collapsibleListNodeContent);
  return fragmentNodes.some((node) => node.attrs.id === nodeId);
}
function findCollapsibleListById(state, nodeId) {
  let collapsibleList;
  state.doc.descendants((node) => {
    if (node.attrs.id === nodeId) {
      collapsibleList = node;
    }
  });
  return collapsibleList;
}
function getCollapsibleListItems(state, nodeId) {
  const collapsibleListNode = findCollapsibleListById(state, nodeId);
  if (collapsibleListNode?.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_LIST_TYPE) {
    return (0, import_ricos_plugins.getFragmentContentNodes)(collapsibleListNode.content);
  }
  return [];
}
function getCollapsibleListByItemId(state, nodeId) {
  const collapsibleLists = findNodesByType(state, import_ricos_content.TIPTAP_COLLAPSIBLE_LIST_TYPE);
  let collapsibleList;
  collapsibleLists.forEach((listNode) => {
    listNode.node.descendants((node) => {
      if (node.attrs.id === nodeId) {
        collapsibleList = listNode.node;
        return;
      }
    });
  });
  return collapsibleList;
}
const findNodesByType = (state, type) => {
  return (0, import_tiptap.findChildren)(state.doc, (node) => node.type.name === type);
};
const findNodeById = (state, id) => {
  return (0, import_tiptap.findChildren)(state.doc, (node) => node.attrs.id === id);
};
//# sourceMappingURL=utils.js.map
