"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setSeaTableColumn = exports.isMenuDisabled = exports.insertSeaTableColumn = exports.getColumnType = exports.getColumnByKey = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _slate = require("slate");
var _slugid = _interopRequireDefault(require("slugid"));
var _elementTypes = require("../../constants/element-types");
var _core = require("../../core");
var _model = _interopRequireDefault(require("./model"));
const isMenuDisabled = (editor, readonly) => {
  if (readonly) return true;
  if (editor.selection == null) return true;
  const [nodeEntry] = _slate.Editor.nodes(editor, {
    match: n => {
      const type = (0, _core.getNodeType)(n);

      // Only available for p and blockquote
      if (type === _elementTypes.PARAGRAPH) return true;
      if (type === _elementTypes.BLOCKQUOTE) return true;
      if (type === _elementTypes.UNORDERED_LIST) return true;
      if (type === _elementTypes.ORDERED_LIST) return true;
      if (type === _elementTypes.CHECK_LIST_ITEM) return true;
      if (type === _elementTypes.IMAGE) return true;
      if (type === _elementTypes.TABLE_CELL) return true;
      return false;
    },
    universal: true,
    mode: 'highest' // Match top level
  });

  // Match to p blockquote, do not disable
  if (nodeEntry) {
    return false;
  }
  return true;
};
exports.isMenuDisabled = isMenuDisabled;
const getColumnType = editor => {
  const [match] = _slate.Editor.nodes(editor, {
    match: n => (0, _core.getNodeType)(n) === _elementTypes.COLUMN,
    universal: true
  });
  if (!match) return _elementTypes.PARAGRAPH;
  const [n] = match;
  return (0, _core.getNodeType)(n);
};
exports.getColumnType = getColumnType;
const insertSeaTableColumn = (editor, active) => {
  if (!active) {
    const column = new _model.default({});
    column.id = _slugid.default.nice();
    _slate.Transforms.insertNodes(editor, (0, _objectSpread2.default)({}, column));
  }
  (0, _core.focusEditor)(editor);
};
exports.insertSeaTableColumn = insertSeaTableColumn;
const setSeaTableColumn = (editor, data) => {
  _slate.Transforms.setNodes(editor, {
    data: data
  }, {
    match: node => node.type === _elementTypes.COLUMN,
    at: editor.selection
  });
};
exports.setSeaTableColumn = setSeaTableColumn;
const getColumnByKey = (columns, key) => {
  const column = columns.find(item => item.key === key);
  return column || null;
};
exports.getColumnByKey = getColumnByKey;