"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBaseNodeTypeName = getBaseNodeTypeName;
exports.isNodeOfSameBaseType = isNodeOfSameBaseType;
exports.isNodeTypeValidChildOf = isNodeTypeValidChildOf;
exports.pickPanelTypeForInsertion = pickPanelTypeForInsertion;
var _model = require("@atlaskit/editor-prosemirror/model");
/**
 * Returns the base name of a node type, stripping known variant suffixes.
 */
var variantToBaseNameMap = {
  panel_c1: 'panel'
};
function getBaseNodeTypeName(nodeType) {
  var _variantToBaseNameMap;
  return (_variantToBaseNameMap = variantToBaseNameMap[nodeType.name]) !== null && _variantToBaseNameMap !== void 0 ? _variantToBaseNameMap : nodeType.name;
}

/**
 * Returns true if two node types share the same base type name.
 *
 * Useful for comparing nodes that may exist as schema variants
 * (e.g. `panel` and `panel_c1` are considered the same base type).
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function isNodeOfSameBaseType(a, b) {
  return getBaseNodeTypeName(a) === getBaseNodeTypeName(b);
}

/**
 * Checks if a node type is a valid child of a parent node by creating a minimal valid
 * instance and validating it against the parent's content expression. Unlike ProseMirror's
 * canReplaceWith, this checks general type compatibility without requiring a specific insertion index.
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function isNodeTypeValidChildOf(childTypeName, parentNode, schema) {
  var childType = schema.nodes[childTypeName];
  if (!childType) {
    return false;
  }
  var candidate = childType.createAndFill();
  if (!candidate) {
    return false;
  }
  return parentNode.type.validContent(_model.Fragment.from(candidate));
}

/**
 * Picks the appropriate panel NodeType for insertion at the current selection
 * or resolved position.
 *
 * Prefers `panel_c1` when the parent can accept it (e.g. when table-in-panel
 * is supported), falls back to `panel` otherwise or at isolating boundaries.
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function pickPanelTypeForInsertion($from) {
  var schema = $from.doc.type.schema;
  var _schema$nodes = schema.nodes,
    panel = _schema$nodes.panel,
    panel_c1 = _schema$nodes.panel_c1;
  if (!panel_c1) {
    return panel;
  }
  for (var depth = $from.depth; depth >= 0; depth--) {
    var parent = $from.node(depth);
    var index = $from.index(depth);
    if (parent.canReplaceWith(index, index, panel_c1)) {
      return panel_c1;
    }
    var spec = parent.type.spec;
    // Stop at isolating containers (e.g. expand, tableCell) — hard walls where
    // the panel stays inside and should use the regular panel type.
    if (spec.isolating) {
      return panel;
    }
  }
  return panel;
}