import { CellSelection } from '@atlaskit/editor-tables/cell-selection';

/**
 * Determine if a mark of a specific type exists anywhere in the selection.
 */
export var anyMarkActive = function anyMarkActive(state, markType) {
  var _state$selection = state.selection,
    $from = _state$selection.$from,
    from = _state$selection.from,
    to = _state$selection.to,
    empty = _state$selection.empty;
  if (empty) {
    return !!markType.isInSet(state.storedMarks || $from.marks());
  }
  var rangeHasMark = false;
  if (state.selection instanceof CellSelection) {
    state.selection.forEachCell(function (cell, cellPos) {
      var from = cellPos;
      var to = cellPos + cell.nodeSize;
      if (!rangeHasMark) {
        rangeHasMark = state.doc.rangeHasMark(from, to, markType);
      }
    });
  } else {
    rangeHasMark = state.doc.rangeHasMark(from, to, markType);
  }
  return rangeHasMark;
};