import { JSONTransformer } from '@atlaskit/editor-json-transformer';
import { SelectedState } from './SelectedState';
import { validNode } from './validNode';

/** Validates prosemirror nodes, and returns true only if all nodes are valid */
export var validateNodes = function validateNodes(nodes) {
  return nodes.every(validNode);
};
export var isType = function isType(node, type) {
  return type && node && node.type === type;
};
export var isParagraph = function isParagraph(node, schema) {
  return isType(node, schema.nodes.paragraph);
};
export var isText = function isText(node, schema) {
  return isType(node, schema.nodes.text);
};
export var isLinkMark = function isLinkMark(node, schema) {
  return isType(node, schema.marks.link);
};

/**
 * Returns if the current selection from achor-head is selecting the node.
 * If the node is not selected then null is returned.
 * If the node is selected then an enum is returned that describes weather the node
 * is fully selected by a range or if the "inside" of the node has been selected or clicked.
 */
export var isNodeSelectedOrInRange = function isNodeSelectedOrInRange(anchorPosition, headPosition, nodePosition, nodeSize) {
  if (typeof nodePosition !== 'number') {
    return null;
  }
  var rangeStart = Math.min(anchorPosition, headPosition);
  var rangeEnd = Math.max(anchorPosition, headPosition);
  var nodeStart = nodePosition;
  var nodeEnd = nodePosition + nodeSize;
  if (anchorPosition === headPosition) {
    return null;
  }
  if (rangeStart <= nodeStart && nodeEnd < rangeEnd || rangeStart < nodeStart && nodeEnd <= rangeEnd) {
    return SelectedState.selectedInRange;
  }
  if (nodeStart <= anchorPosition && headPosition <= nodeEnd) {
    return SelectedState.selectedInside;
  }
  return null;
};
var transformer = new JSONTransformer();
export function toJSON(node) {
  return transformer.encode(node);
}
export function nodeToJSON(node) {
  return transformer.encodeNode(node);
}
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { findChangedNodesFromTransaction } from './findChangedNodesFromTransaction';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { validNode } from './validNode';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { SelectedState } from './SelectedState';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { isSupportedInParent } from './isSupportedInParent';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { isMediaNode } from './isMediaNode';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { isNodeBeforeMediaNode } from './isNodeBeforeMediaNode';