"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateImage = exports.lazyLoadImage = exports.isMenuDisabled = exports.insertSeafileImage = exports.insertImage = exports.handleUpdateFile = exports.getImagesUrlList = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _slate = require("slate");
var _slugid = _interopRequireDefault(require("slugid"));
var _helper = require("../header/helper");
var _elementTypes = require("../../constants/element-types");
var _core = require("../../core");
var _helpers = require("../code-block/helpers");
var _helper2 = require("../link/helper");
const isMenuDisabled = (editor, readonly) => {
  if (readonly) return true;
  const isHeader = (0, _helper.getHeaderType)(editor);
  if (isHeader) return true;
  if ((0, _helpers.isInCodeBlock)(editor)) return true;
  return false;
};
exports.isMenuDisabled = isMenuDisabled;
const insertImage = (editor, url, title) => {
  const imageNode = {
    type: _elementTypes.IMAGE,
    id: _slugid.default.nice(),
    data: (0, _objectSpread2.default)({
      init: true,
      src: url
    }, title && {
      title
    }),
    children: [(0, _core.generateDefaultText)()]
  };
  _slate.Transforms.insertNodes(editor, imageNode, {
    at: editor.selection,
    select: true
  });
  (0, _core.focusEditor)(editor);
};
exports.insertImage = insertImage;
const insertSeafileImage = _ref => {
  let {
    editor,
    url,
    title,
    selection
  } = _ref;
  const imageNode = {
    type: _elementTypes.IMAGE,
    id: _slugid.default.nice(),
    data: (0, _objectSpread2.default)({
      src: url
    }, title && {
      title
    }),
    children: [(0, _core.generateDefaultText)()]
  };
  _slate.Transforms.insertNodes(editor, imageNode, {
    at: selection,
    select: true
  });
};
exports.insertSeafileImage = insertSeafileImage;
const updateImage = (editor, data) => {
  _slate.Transforms.setNodes(editor, {
    data
  }, {
    match: n => (0, _core.getNodeType)(n) === _elementTypes.IMAGE,
    at: editor.selection,
    voids: true
  });
};
exports.updateImage = updateImage;
const getImagesUrlList = nodes => {
  let nodeIndex = 0;
  const list = [];
  while (nodes && nodeIndex <= nodes.length - 1) {
    const currentNode = nodes[nodeIndex];
    if (currentNode.type === _elementTypes.IMAGE) {
      const {
        data = {}
      } = currentNode;
      data.src && list.push(data.src);
    } else {
      list.push(...getImagesUrlList(currentNode.children));
    }
    nodeIndex++;
  }
  return list;
};
exports.getImagesUrlList = getImagesUrlList;
const handleUpdateFile = async (editor, file, insertPosition) => {
  if (editor.api.uploadLocalImage) {
    try {
      const url = await editor.api.uploadLocalImage(file);
      const title = file.name;
      if (file.type.includes(_elementTypes.IMAGE)) {
        insertImage(editor, url);
      } else {
        (0, _helper2.insertLink)({
          editor,
          url,
          title,
          insertPosition
        });
      }
    } catch (error) {
      console.log('error', error);
    }
  }
};
exports.handleUpdateFile = handleUpdateFile;
const lazyLoadImage = (url, resolve, reject) => {
  if (!url) {
    reject('img path is require');
    return;
  }
  const image = new Image();
  image.onload = () => {
    resolve(image);
  };
  image.onerror = e => {
    reject(e);
  };
  image.src = url;
};
exports.lazyLoadImage = lazyLoadImage;