"use strict";

exports.__esModule = true;
exports.removeBlockFromMap = exports.isGreaterThan = exports.getSelectionInfo = exports.getLineNumber = exports.getEditorBounds = exports.clearInlineStyles = exports.atomicBlockExists = void 0;

var _draftJs = require("draft-js");

/**
 * Get the current selection details
 */
const getSelectionInfo = editorState => {
  const selection = editorState.getSelection();
  const startOffset = selection.getStartOffset();
  const currentContent = editorState.getCurrentContent();
  const contentBlock = currentContent.getBlockForKey(selection.getStartKey());
  const currentStyle = editorState.getCurrentInlineStyle();
  const linkKey = contentBlock.getEntityAt(startOffset);
  let entityType = '';

  if (linkKey) {
    const linkInstance = currentContent.getEntity(linkKey);
    entityType = linkInstance.getType();
  }

  return {
    inlineStyle: currentStyle,
    blockType: contentBlock.getType(),
    entityType: entityType,
    linkKey: linkKey,
    block: contentBlock
  };
};
/**
 * Remove a block from the ContentState
 */


exports.getSelectionInfo = getSelectionInfo;

const removeBlockFromMap = (editorState, block) => {
  const contentState = editorState.getCurrentContent();

  const removeBlockContentState = _draftJs.Modifier.removeRange(contentState, new _draftJs.SelectionState({
    anchorKey: block.getKey(),
    anchorOffset: 0,
    focusKey: block.getKey(),
    focusOffset: block.getLength()
  }), 'backward');

  const blockMap = removeBlockContentState.getBlockMap().delete(block.getKey());
  return removeBlockContentState.merge({
    blockMap,
    selectionAfter: contentState.getSelectionAfter()
  });
};

exports.removeBlockFromMap = removeBlockFromMap;

const atomicBlockExists = (name, controls) => {
  if (!controls) {
    return undefined;
  }

  return controls.find(control => control.type === 'atomic' && control.name === name && control.atomicComponent !== undefined);
};

exports.atomicBlockExists = atomicBlockExists;

const isGreaterThan = (value, maxValue) => {
  if (!maxValue) {
    return false;
  }

  return value > maxValue;
};

exports.isGreaterThan = isGreaterThan;

const clearInlineStyles = (editorState, customStyles) => {
  let styles = ['BOLD', 'ITALIC', 'UNDERLINE'];

  if (customStyles) {
    styles = styles.concat(Object.getOwnPropertyNames(customStyles));
  }

  return styles.reduce((newContentState, style) => _draftJs.Modifier.removeInlineStyle(newContentState, editorState.getSelection(), style), editorState.getCurrentContent());
};

exports.clearInlineStyles = clearInlineStyles;

const getEditorBounds = editor => {
  let fakeClientRect = (0, _draftJs.getVisibleSelectionRect)(window);
  return {
    selectionRect: fakeClientRect ? {
      top: fakeClientRect == null ? void 0 : fakeClientRect.top,
      left: fakeClientRect == null ? void 0 : fakeClientRect.left
    } : null,
    editorRect: editor.getBoundingClientRect()
  };
};

exports.getEditorBounds = getEditorBounds;

const getLineNumber = editorState => {
  const currentBlockKey = editorState.getSelection().getStartKey();
  return editorState.getCurrentContent().getBlockMap().keySeq().findIndex(k => k === currentBlockKey);
};

exports.getLineNumber = getLineNumber;