import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
import { fg } from '@atlaskit/platform-feature-flags';
import { getSliceFromSelection } from './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.
 */
export const getLocalIdsFromSelection = selection => {
  if (!selection) {
    return null;
  }
  if (selection instanceof NodeSelection) {
    if (fg('platform_editor_ai_selection_local_ids')) {
      var _selection$node$attrs;
      const ids = [];
      const 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(node => {
        var _node$attrs;
        if (node.isInline) {
          return false;
        }
        const 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) {
    if (fg('platform_editor_ai_selection_local_ids')) {
      const ids = [];
      const ancestorIds = new Set();
      // Collect localIds from each selected cell and its descendants
      selection.forEachCell((cellNode, cellPos) => {
        var _cellNode$attrs;
        const cellLocalId = (_cellNode$attrs = cellNode.attrs) === null || _cellNode$attrs === void 0 ? void 0 : _cellNode$attrs.localId;
        if (cellLocalId) {
          ids.push(cellLocalId);
        }
        cellNode.descendants(node => {
          var _node$attrs2;
          if (node.isInline) {
            return false;
          }
          const localId = (_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.localId;
          if (localId) {
            ids.push(localId);
          }
        });
        // Collect ancestor localIds (row, table) separately
        const $cell = selection.$anchorCell.doc.resolve(cellPos + 1);
        for (let depth = $cell.depth - 1; depth >= 1; depth--) {
          var _ancestor$attrs;
          const ancestor = $cell.node(depth);
          const localId = (_ancestor$attrs = ancestor.attrs) === null || _ancestor$attrs === void 0 ? void 0 : _ancestor$attrs.localId;
          if (localId) {
            ancestorIds.add(localId);
          }
        }
      });
      const uniqueIds = new Set(ids);
      const uniqueAncestors = [...ancestorIds].filter(id => !uniqueIds.has(id));
      return [...uniqueIds, ...uniqueAncestors];
    }
    // Fall through to default slice-based behavior when gate is off
  } else if (selection.empty) {
    return [selection.$from.parent.attrs.localId];
  }
  const slice = getSliceFromSelection(selection);
  const content = slice.content;
  const ids = [];
  content.forEach(node => {
    var _node$attrs3;
    const localId = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.localId;
    if (localId) {
      ids.push(localId);
    }
  });
  return ids;
};