"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toggleTextStyle = exports.removeMark = exports.isMenuDisabled = exports.isMarkActive = exports.addMark = void 0;
var _slate = require("slate");
var _helpers = require("../code-block/helpers");
var _constants = require("../../constants");
var _core = require("../../core");
const isMenuDisabled = (editor, isReadonly) => {
  if (isReadonly) return true;
  const {
    selection
  } = editor;
  if (!selection) return true;
  if ((0, _helpers.isInCodeBlock)(editor)) return true;
  return false;
};
exports.isMenuDisabled = isMenuDisabled;
const isMarkActive = (editor, mark) => {
  const {
    selection
  } = editor;
  if (!selection) return false;
  const marks = _slate.Editor.marks(editor);

  // If curMarks exists, you need to set this parameter manually. curMarks prevails
  if (marks && Object.keys(marks).length > 0) {
    return !!marks[mark];
  } else {
    const [match] = _slate.Editor.nodes(editor, {
      match: n => n[mark] === true
    });
    return !!match;
  }
};
exports.isMarkActive = isMarkActive;
const addMark = (editor, type) => {
  _slate.Editor.addMark(editor, type, true);
};
exports.addMark = addMark;
const removeMark = (editor, type) => {
  _slate.Editor.removeMark(editor, type);
};
exports.removeMark = removeMark;
const toggleTextStyle = (editor, type) => {
  const isActive = isMarkActive(editor, type);
  isActive ? removeMark(editor, type) : addMark(editor, type);
  if (type === _constants.TEXT_STYLE_MAP.CODE) {
    if (_slate.Range.isCollapsed(editor.selection)) {
      _slate.Editor.insertText(editor, '');
    }
    (0, _core.focusEditor)(editor);
  }
};
exports.toggleTextStyle = toggleTextStyle;