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

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