"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 resolvers_exports = {};
__export(resolvers_exports, {
  canAddCell: () => canAddCell,
  getParentVariant: () => getParentVariant,
  getSelectedSmartNode: () => getSelectedSmartNode,
  getSmartBlockNode: () => getSmartBlockNode,
  isBorderSupported: () => isBorderSupported,
  isCellInLabelVariantWithImage: () => isCellInLabelVariantWithImage,
  isCellInLabelVariantWithoutImage: () => isCellInLabelVariantWithoutImage,
  isCellOrLayoutSelected: () => isCellOrLayoutSelected,
  isCellSelected: () => isCellSelected,
  isLayoutInLabelVariantWithImage: () => isLayoutInLabelVariantWithImage,
  isLayoutSelected: () => isLayoutSelected,
  isLayoutSelectedAndColumnSizeVariant: () => isLayoutSelectedAndColumnSizeVariant,
  isLayoutSelectedAndNumberedVariants: () => isLayoutSelectedAndNumberedVariants,
  isLayoutSelectedAndOrientationVariant: () => isLayoutSelectedAndOrientationVariant
});
module.exports = __toCommonJS(resolvers_exports);
var import_pm_node_types = require("./../../_shared/pm-node-types.js");
var import_core = require("./../../tiptap/core/index.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_consts = require("../consts.js");
var import_variant_features = require("../utils/variant-features.js");
const getSelectedSmartNode = (_, __, editor) => {
  if (!editor?.state) {
    return void 0;
  }
  const selection = editor.state.selection;
  if (selection instanceof import_state.NodeSelection && selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
    return selection.node;
  }
  return getSmartBlockNode(_, __, editor);
};
const getSmartBlockNode = (_, __, editor) => {
  if (!editor?.state) {
    return void 0;
  }
  const { selection } = editor.state;
  if (selection instanceof import_state.NodeSelection && selection.node) {
    if (selection.node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK) {
      return selection.node;
    }
  }
  const result = (0, import_core.findParentNodeClosestToPos)(
    editor.state.doc.resolve(selection.from),
    (node) => node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK
  );
  return result?.node;
};
const isLayoutSelected = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const selection = editor.state.selection;
  if (selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
    return false;
  }
  if (selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK) {
    return true;
  }
  if (!selection.node && selection.$from) {
    const closestParent = (0, import_core.findParentNodeClosestToPos)(
      selection.$from,
      (node) => node.type.name === import_pm_node_types.PmNodeType.SMART_BLOCK
    );
    return !!closestParent;
  }
  return false;
};
const isLayoutSelectedAndColumnSizeVariant = (_, __, editor) => {
  const node = getSmartBlockNode(_, __, editor);
  const variant = node?.attrs?.type;
  if (!isLayoutSelected(_, __, editor) || !variant) {
    return false;
  }
  return (0, import_variant_features.hasColumnSizeSupport)(variant);
};
const isLayoutSelectedAndOrientationVariant = (_, __, editor) => {
  const node = getSmartBlockNode(_, __, editor);
  const variant = node?.attrs?.type;
  if (!isLayoutSelected(_, __, editor) || !variant) {
    return false;
  }
  return (0, import_variant_features.hasOrientation)(variant);
};
const isLayoutSelectedAndNumberedVariants = (_, __, editor) => {
  const node = getSmartBlockNode(_, __, editor);
  const variant = node?.attrs?.type;
  if (!isLayoutSelected(_, __, editor) || !variant) {
    return false;
  }
  return (0, import_variant_features.hasShapeSupport)(variant);
};
const isBorderSupported = (_, __, editor) => {
  if (isCellSelected(_, __, editor)) {
    const variant2 = getParentVariant(editor);
    return !!variant2 && (0, import_variant_features.hasCellBorderSupport)(variant2);
  }
  const node = getSmartBlockNode(_, __, editor);
  const variant = node?.attrs?.type;
  if (!isLayoutSelected(_, __, editor) || !variant) {
    return false;
  }
  return (0, import_variant_features.hasCellBorderSupport)(variant) || (0, import_variant_features.hasBlockBorderSupport)(variant);
};
const isCellSelected = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const selection = editor.state.selection;
  return selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL;
};
const isCellOrLayoutSelected = (_, __, editor) => {
  return isLayoutSelected(_, __, editor) || isCellSelected(_, __, editor);
};
const canAddCell = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const { selection } = editor.state;
  if (!(selection instanceof import_state.NodeSelection)) {
    return false;
  }
  if (selection.node?.type?.name !== import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
    return false;
  }
  const parent = (0, import_core.findParentNodeClosestToPos)(
    editor.state.doc.resolve(selection.from),
    (node) => node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK
  );
  return !!parent && parent.node.childCount < import_consts.MAX_ITEMS;
};
const getParentVariant = (editor) => {
  if (!editor?.state) {
    return void 0;
  }
  const { selection } = editor.state;
  const nodeSelection = selection;
  if (nodeSelection.node?.type?.name !== import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
    return void 0;
  }
  const result = (0, import_core.findParentNodeClosestToPos)(
    editor.state.doc.resolve(selection.from),
    (node) => node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK
  );
  return result?.node?.attrs?.type;
};
const isCellInLabelVariantWithoutImage = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const variant = getParentVariant(editor);
  if (!variant || !(0, import_variant_features.hasShapeSupport)(variant)) {
    return false;
  }
  const selection = editor.state.selection;
  return !selection.node?.attrs?.shape;
};
const isCellInLabelVariantWithImage = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const variant = getParentVariant(editor);
  if (!variant || !(0, import_variant_features.hasShapeSupport)(variant)) {
    return false;
  }
  const selection = editor.state.selection;
  return !!selection.node?.attrs?.shape;
};
const isLayoutInLabelVariantWithImage = (_, __, editor) => {
  if (!editor?.state) {
    return false;
  }
  const node = getSmartBlockNode(_, __, editor);
  const variant = node?.attrs?.type;
  if (!isLayoutSelected(_, __, editor) || !variant || !(0, import_variant_features.hasShapeSupport)(variant)) {
    return false;
  }
  let hasShape = false;
  node?.forEach((child) => {
    if (child.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL && child.attrs?.shape) {
      hasShape = true;
    }
  });
  return hasShape;
};
//# sourceMappingURL=resolvers.js.map
