"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getLocalIdsFromSelection = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _state = require("@atlaskit/editor-prosemirror/state");
var _cellSelection = require("@atlaskit/editor-tables/cell-selection");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _contextHelpers = require("./context-helpers");
/**
 * Get the local IDs from the selection.
 * @param selection The selection to get the local IDs from.
 * @returns The local IDs as an array of strings.
 */
var getLocalIdsFromSelection = exports.getLocalIdsFromSelection = function getLocalIdsFromSelection(selection) {
  if (!selection) {
    return null;
  }
  if (selection instanceof _state.NodeSelection) {
    if ((0, _platformFeatureFlags.fg)('platform_editor_ai_selection_local_ids')) {
      var _selection$node$attrs;
      var _ids = [];
      var rootLocalId = (_selection$node$attrs = selection.node.attrs) === null || _selection$node$attrs === void 0 ? void 0 : _selection$node$attrs.localId;
      if (rootLocalId) {
        _ids.push(rootLocalId);
      }
      selection.node.descendants(function (node) {
        var _node$attrs;
        if (node.isInline) {
          return false;
        }
        var localId = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.localId;
        if (localId) {
          _ids.push(localId);
        }
      });
      return _ids;
    }
    return [selection.node.attrs.localId];
  } else if (selection instanceof _cellSelection.CellSelection) {
    if ((0, _platformFeatureFlags.fg)('platform_editor_ai_selection_local_ids')) {
      var _ids2 = [];
      var ancestorIds = new Set();
      // Collect localIds from each selected cell and its descendants
      selection.forEachCell(function (cellNode, cellPos) {
        var _cellNode$attrs;
        var cellLocalId = (_cellNode$attrs = cellNode.attrs) === null || _cellNode$attrs === void 0 ? void 0 : _cellNode$attrs.localId;
        if (cellLocalId) {
          _ids2.push(cellLocalId);
        }
        cellNode.descendants(function (node) {
          var _node$attrs2;
          if (node.isInline) {
            return false;
          }
          var localId = (_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.localId;
          if (localId) {
            _ids2.push(localId);
          }
        });
        // Collect ancestor localIds (row, table) separately
        var $cell = selection.$anchorCell.doc.resolve(cellPos + 1);
        for (var depth = $cell.depth - 1; depth >= 1; depth--) {
          var _ancestor$attrs;
          var ancestor = $cell.node(depth);
          var localId = (_ancestor$attrs = ancestor.attrs) === null || _ancestor$attrs === void 0 ? void 0 : _ancestor$attrs.localId;
          if (localId) {
            ancestorIds.add(localId);
          }
        }
      });
      var uniqueIds = new Set(_ids2);
      var uniqueAncestors = (0, _toConsumableArray2.default)(ancestorIds).filter(function (id) {
        return !uniqueIds.has(id);
      });
      return [].concat((0, _toConsumableArray2.default)(uniqueIds), (0, _toConsumableArray2.default)(uniqueAncestors));
    }
    // Fall through to default slice-based behavior when gate is off
  } else if (selection.empty) {
    return [selection.$from.parent.attrs.localId];
  }
  var slice = (0, _contextHelpers.getSliceFromSelection)(selection);
  var content = slice.content;
  var ids = [];
  content.forEach(function (node) {
    var _node$attrs3;
    var localId = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.localId;
    if (localId) {
      ids.push(localId);
    }
  });
  return ids;
};