import { NodeSelection } from '@atlaskit/editor-prosemirror/state';

/**
 *
 * @param editorState current editor state
 * @returns selected media node (child of mediaSingle only) with position
 */
export var currentMediaNodeWithPos = function currentMediaNodeWithPos(editorState) {
  var doc = editorState.doc,
    selection = editorState.selection,
    schema = editorState.schema;
  if (!doc || !selection || !(selection instanceof NodeSelection) || selection.node.type !== schema.nodes.mediaSingle) {
    return;
  }
  var pos = selection.$anchor.pos + 1;
  var node = doc.nodeAt(pos);
  if (!node || node.type !== schema.nodes.media) {
    return;
  }
  return {
    node: node,
    pos: pos
  };
};