"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slate = require("slate");
var _seafileEditor = require("@seafile/seafile-editor");
const PREVIEW_TEXT_LENGTH = 150;
const getPreviewContent = function (content) {
  let isMarkdown = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  let previewTextNeedSlice = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  const slateNodes = isMarkdown ? (0, _seafileEditor.mdStringToSlate)(content) : content;
  let previewContent = {
    previewText: '',
    images: [],
    links: [],
    checklist: {
      total: 0,
      completed: 0
    }
  };
  getPreviewInfo(slateNodes, previewContent);
  getPreviewText(slateNodes, previewContent, previewTextNeedSlice);
  return previewContent;
};
const getPreviewInfo = (nodes, previewContent) => {
  let nodeIndex = 0;
  while (nodes && nodeIndex <= nodes.length - 1) {
    const currentNode = nodes[nodeIndex];
    if (currentNode.type === 'link') {
      previewContent.links.push(currentNode.url);
    } else if (currentNode.type === 'image') {
      previewContent.images.push(currentNode.data.src);
    } else if (currentNode.type === 'check_list_item') {
      previewContent.checklist.total += 1;
      if (currentNode.checked) {
        previewContent.checklist.completed++;
      }
      // recalculate child elements
      getPreviewInfo(currentNode.children, previewContent);
    } else {
      getPreviewInfo(currentNode.children, previewContent);
    }
    nodeIndex++;
  }
};
const getPreviewText = (content, previewContent, previewTextNeedSlice) => {
  let previewText = '';
  for (let index = 0; index < content.length; index++) {
    previewText += getTextOfNode(content[index]) + ' ';
    let textLength = previewText.length;
    if (previewTextNeedSlice && textLength >= PREVIEW_TEXT_LENGTH) {
      previewText = textLength > PREVIEW_TEXT_LENGTH ? previewText.slice(0, PREVIEW_TEXT_LENGTH) : previewText;
      break;
    }
  }
  previewContent.previewText = previewText;
};
const getTextOfNode = node => {
  let text = '';
  if (node.type === 'check_list_item') {
    text += '';
    return text;
  }
  for (let index = 0; index < node.children.length; index++) {
    const currentNode = node.children[index];
    const {
      type
    } = currentNode;
    if (type === 'link') {
      text += '';
    } else {
      text += _slate.Node.string(currentNode) + ' ';
    }
  }
  return text;
};
var _default = exports.default = getPreviewContent;