"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _textEditorConstants = require("./text-editor-constants");
class TextEditorService {
  constructor(slate, slateReact) {
    this.slate = slate;
    this.slateReact = slateReact;
  }
  focus(editor) {
    this.slateReact.ReactEditor.focus(editor);
  }
  isMarkActive(editor, format) {
    const marks = this.slate.Editor.marks(editor);
    return marks ? marks[format] === true : false;
  }
  isBlockActive(editor, format) {
    const {
      Editor,
      Element
    } = this.slate;
    const [match] = Editor.nodes(editor, {
      match: n => !Editor.isEditor(n) && Element.isElement(n) && n.type === format
    });
    return !!match;
  }
  toggleMark(editor, format) {
    const {
      Editor
    } = this.slate;
    const isActive = this.isMarkActive(editor, format);
    if (isActive) {
      Editor.removeMark(editor, format);
    } else {
      Editor.addMark(editor, format, true);
    }
  }
  checkIsList(format) {
    return format === _textEditorConstants.BLOCK_TYPES.BULLETED_LIST || format === _textEditorConstants.BLOCK_TYPES.NUMBERED_LIST;
  }
  toggleBlock(editor, format) {
    const {
      Transforms,
      Element,
      Editor
    } = this.slate;
    const isActive = this.isBlockActive(editor, format);
    const isList = this.checkIsList(format);
    Transforms.unwrapNodes(editor, {
      match: n => {
        const valueType = !Editor.isEditor(n) && Element.isElement(n) && n.type;
        return this.checkIsList(valueType);
      },
      split: true
    });
    let nodeType = format;
    if (isActive) {
      nodeType = _textEditorConstants.BLOCK_TYPES.REGULAR_TEXT;
    } else if (isList) {
      nodeType = _textEditorConstants.BLOCK_TYPES.LIST_ITEM;
    }
    const newProperties = {
      type: nodeType
    };
    Transforms.setNodes(editor, newProperties);
    if (!isActive && isList) {
      const block = {
        type: format,
        children: []
      };
      Transforms.wrapNodes(editor, block);
    }
  }
  insertLink(editor, url) {
    const {
      Transforms,
      Range
    } = this.slate;
    if (editor.selection) {
      if (this.isBlockActive(editor, _textEditorConstants.BLOCK_TYPES.LINK)) {
        this.removeLink(editor);
      }
      const {
        selection
      } = editor;
      const isCollapsed = selection && Range.isCollapsed(selection);
      const link = {
        type: _textEditorConstants.BLOCK_TYPES.LINK,
        url,
        children: isCollapsed ? [{
          text: url
        }] : []
      };
      if (isCollapsed) {
        Transforms.insertNodes(editor, link);
      } else {
        Transforms.wrapNodes(editor, link, {
          split: true
        });
        Transforms.collapse(editor, {
          edge: 'end'
        });
      }
    }
  }
  clearFormatting(editor) {
    Object.values(_textEditorConstants.MARK_TYPES).forEach(mark => {
      editor.removeMark(mark);
    });
    this.removeLink(editor);
    this.toggleBlock(editor, _textEditorConstants.BLOCK_TYPES.REGULAR_TEXT);
  }
  removeLink(editor) {
    const {
      Transforms,
      Element,
      Editor
    } = this.slate;
    Transforms.unwrapNodes(editor, {
      match: n => !Editor.isEditor(n) && Element.isElement(n) && n.type === _textEditorConstants.BLOCK_TYPES.LINK
    });
  }
  deleteContent(editor) {
    const {
      Transforms,
      Editor
    } = this.slate;
    const selectAllRange = Editor.range(editor, Editor.start(editor, []), Editor.end(editor, [Math.max(editor.children.length - 1, 0), 0]));
    Transforms.removeNodes(editor, {
      at: selectAllRange
    });
    // Slate requires at least 1 text child node
    Transforms.insertNodes(editor, {
      type: _textEditorConstants.BLOCK_TYPES.REGULAR_TEXT,
      children: [{
        text: ''
      }]
    });
  }
}
exports.default = TextEditorService;