import {
  TIPTAP_CARD_FOOTER_TYPE,
  TIPTAP_CARD_TYPE,
  TIPTAP_FIGURE_TYPE,
  TIPTAP_LAYOUT_TYPE
} from "./../../ricos-content/index.js";
import { TOP_LEVEL_NODES } from "./../top-level-nodes.js";
import { findParentNodeClosestToPos } from "./../../tiptap/index.js";
import { NodeSelection } from "./../../tiptap/pm/state.js";
const canSetAsFooter = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  if (!editor.state.schema.nodes[TIPTAP_CARD_TYPE]) {
    return false;
  }
  const selection = editor.state.selection;
  const selectedNode = selection instanceof NodeSelection ? selection.node : void 0;
  const parentFigure = findParentNodeClosestToPos(
    selection.$from,
    (node) => node.type.name === TIPTAP_FIGURE_TYPE
  );
  if (parentFigure) {
    const parentOfFigure = selection.$from.node(parentFigure.depth - 1);
    const isTopLevel = TOP_LEVEL_NODES.includes(parentOfFigure.type.name);
    if (!isTopLevel) {
      return false;
    }
  }
  if (selectedNode?.type.name === TIPTAP_LAYOUT_TYPE && selectedNode.attrs?.banner?.position === "TOP") {
    return false;
  }
  const closestLayoutForExclusion = findParentNodeClosestToPos(
    selection.$from,
    (node) => node.type.name === TIPTAP_LAYOUT_TYPE
  );
  if (closestLayoutForExclusion?.node.attrs?.banner?.position === "TOP") {
    return false;
  }
  const parentCard = findParentNodeClosestToPos(
    selection.$from,
    (node) => node.type.name === TIPTAP_CARD_TYPE
  );
  let existingFooterId;
  if (parentCard) {
    parentCard.node.forEach((child) => {
      if (child.type.name === TIPTAP_CARD_FOOTER_TYPE && !existingFooterId) {
        existingFooterId = child.firstChild?.attrs.id;
      }
    });
  } else {
    const lastNode = editor.state.doc.content.lastChild;
    if (lastNode?.type.name === TIPTAP_LAYOUT_TYPE && lastNode.attrs?.banner?.position === "BOTTOM") {
      existingFooterId = lastNode.attrs.id;
    }
  }
  if (!existingFooterId) {
    return true;
  }
  if (selectedNode?.type.name === TIPTAP_LAYOUT_TYPE) {
    return selectedNode.attrs?.id === existingFooterId;
  }
  const closestLayout = findParentNodeClosestToPos(
    selection.$from,
    (node) => node.type.name === TIPTAP_LAYOUT_TYPE
  );
  if (closestLayout) {
    return closestLayout.node.attrs?.id === existingFooterId;
  }
  return false;
};
export {
  canSetAsFooter
};
//# sourceMappingURL=can-set-as-footer.js.map
