"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _isUrl = _interopRequireDefault(require("is-url"));
var _slugid = _interopRequireDefault(require("slugid"));
var _slateConvert = require("../../../slate-convert");
var _elementTypes = require("../../constants/element-types");
var _helpers = require("../code-block/helpers");
const withHtml = editor => {
  const {
    insertData
  } = editor;
  const newEditor = editor;
  newEditor.insertData = data => {
    // If the text is a link and is not within the code_block block, it is processed as link
    const text = data.getData('text/plain') || '';
    if (text.trim() && (0, _isUrl.default)(text.trim()) && !(0, _helpers.isInCodeBlock)(newEditor)) {
      insertData(data);
      return;
    }

    // If the copied content contains files, proceed as shown in the image
    if (data.types.includes('Files')) {
      insertData(data);
      return;
    }

    // If code_block is selected, the copied content will be processed according to code_block
    if (!data.types.includes('text/code-block') && (0, _helpers.isInCodeBlock)(newEditor)) {
      const plaintext = data.getData('text/plain') || '';
      if (plaintext) {
        let fragmentData = [];
        plaintext.split('\n').forEach(item => {
          const codeLine = {
            id: _slugid.default.nice(),
            type: _elementTypes.CODE_LINE,
            children: [{
              text: item,
              id: _slugid.default.nice()
            }]
          };
          fragmentData.push(codeLine);
        });
        newEditor.insertFragment(fragmentData);
      }
      return;
    }

    // If it is in slate format, it will be processed in slate format.
    if (newEditor.insertFragmentData(data)) {
      return;
    }

    // If it is in html format, it will be processed in html format.
    const htmlContent = data.getData('text/html') || '';
    if (htmlContent) {
      const content = (0, _slateConvert.deserializeHtml)(htmlContent);
      editor.insertFragment(content);
      return;
    }
    insertData(data);
  };
  return newEditor;
};
var _default = exports.default = withHtml;