"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isSelectionTableNestedInTable = exports.isPanelNestingTableSupported = exports.isNestedTablesSupported = exports.getPositionAfterTopParentNodeOfType = exports.getParentOfTypeCount = void 0;
var _editorTables = require("@atlaskit/editor-tables");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
/*
 * Returns the level of nesting of a given `nodeType` in the current position.
 * eg. table > table is nested 1 level, table > table > table is nested 2 levels.
 *
 * ```typescript
 * const nestingLevel = getParentOfTypeCount(schema.nodes.table)(position);
 * ```
 */
var getParentOfTypeCount = exports.getParentOfTypeCount = function getParentOfTypeCount(nodeType) {
  return function ($pos) {
    var count = 0;
    // Loop through parent nodes from bottom to top
    for (var depth = $pos.depth; depth > 0; depth--) {
      var node = $pos.node(depth);
      // Count the table nodes
      if (node.type === nodeType) {
        count++;
      }
    }
    return count;
  };
};

/*
 * Returns the (absolute) position directly after the top parent node of a given `nodeType` in the current position.
 *
 * ```typescript
 * const nestingLevel = getEndTopParentNodeOfType(schema.nodes.table)(position);
 * ```
 */
var getPositionAfterTopParentNodeOfType = exports.getPositionAfterTopParentNodeOfType = function getPositionAfterTopParentNodeOfType(nodeType) {
  return function ($pos) {
    // Loop through parent nodes from top to bottom
    for (var depth = 1; depth <= $pos.depth; depth++) {
      var node = $pos.node(depth);
      // Count the table nodes
      if (node.type === nodeType) {
        return $pos.after(depth);
      }
    }
  };
};

/*
 * Returns true if the current selection is inside a table nested within a table.
 *
 * ```typescript
 * const isNestedTable = isSelectionTableNestedInTable(state);
 * ```
 */
var isSelectionTableNestedInTable = exports.isSelectionTableNestedInTable = function isSelectionTableNestedInTable(state) {
  var table = (0, _editorTables.findTable)(state.selection);
  if (!table) {
    return false;
  }
  var parent = state.doc.resolve(table.pos).parent;
  var nodeTypes = state.schema.nodes;
  return [nodeTypes.tableHeader, nodeTypes.tableCell].includes(parent.type);
};

/*
 * Returns true if the schema supports nesting tables inside table cells.
 * This is determined by checking if table cells can contain table nodes in their content model.
 *
 * ```typescript
 * const supportsNestedTables = isNestedTablesSupported(state.schema);
 * ```
 */
var isNestedTablesSupported = exports.isNestedTablesSupported = function isNestedTablesSupported(schema) {
  var _tableCell$contentMat, _tableHeader$contentM;
  var _schema$nodes = schema.nodes,
    table = _schema$nodes.table,
    tableCell = _schema$nodes.tableCell,
    tableHeader = _schema$nodes.tableHeader;
  if (!table || !tableCell || !tableHeader) {
    return false;
  }

  // Check if table cells can contain table nodes by testing their content match
  var tableCellCanContainTable = ((_tableCell$contentMat = tableCell.contentMatch.matchType(table)) === null || _tableCell$contentMat === void 0 ? void 0 : _tableCell$contentMat.validEnd) === true;
  var tableHeaderCanContainTable = ((_tableHeader$contentM = tableHeader.contentMatch.matchType(table)) === null || _tableHeader$contentM === void 0 ? void 0 : _tableHeader$contentM.validEnd) === true;
  return tableCellCanContainTable || tableHeaderCanContainTable;
};

/*
 * Returns true if the schema supports nesting a table inside a panel (panel_c1 variant),
 * AND the `platform_editor_nest_table_in_panel` experiment is enabled.
 *
 * ```typescript
 * const supportsTableInPanel = isPanelNestingTableSupported(state.schema);
 * ```
 */
var isPanelNestingTableSupported = exports.isPanelNestingTableSupported = function isPanelNestingTableSupported(schema) {
  var _panel_c1$contentMatc;
  var _schema$nodes2 = schema.nodes,
    table = _schema$nodes2.table,
    panel_c1 = _schema$nodes2.panel_c1;
  if (!table || !panel_c1) {
    return false;
  }

  // Confirm the PM schema actually allows table inside panel_c1
  var panelC1CanContainTable = ((_panel_c1$contentMatc = panel_c1.contentMatch.matchType(table)) === null || _panel_c1$contentMatc === void 0 ? void 0 : _panel_c1$contentMatc.validEnd) === true;
  return panelC1CanContainTable && (0, _expValEquals.expValEquals)('platform_editor_nest_table_in_panel', 'isEnabled', true);
};