'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var index = require('./chunk-eyfbjfPH.cjs.js');
var neetoCist = require('@bigbinary/neeto-cist');
var constants$1 = require('./chunk-2HrSPdAV.cjs.js');
var constants = require('./chunk-BGy3NmZC.cjs.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);

var getEditorStyles = function getEditorStyles(_ref) {
  var rows = _ref.rows;
  var editorHeight = rows * constants.EDITOR_LINE_HEIGHT + 2 * (constants.EDITOR_PADDING_SIZE + constants.EDITOR_BORDER_SIZE);
  return "min-height: ".concat(editorHeight, "px;");
};
var clipboardTextParser = function clipboardTextParser(text, context) {
  var nodes = [];
  var blocks = text.split(/\n/);
  blocks.forEach(function (line) {
    var nodeJson = {
      type: "paragraph"
    };
    if (neetoCist.isNotEmpty(line)) {
      nodeJson.content = [{
        type: "text",
        text: line
      }];
    }
    var node = index.Node.fromJSON(context.doc.type.schema, nodeJson);
    nodes.push(node);
  });
  var fragment = index.Fragment.fromArray(nodes);
  return index.Slice.maxOpen(fragment);
};
var setInitialPosition = function setInitialPosition(editor) {
  var state = editor.state,
    view = editor.view;
  var endPosition = index.Selection.atEnd(state.doc);
  var transaction = state.tr;
  transaction.setSelection(endPosition);
  view.dispatch(transaction);
};
var isEditorOverlaysActive = function isEditorOverlaysActive() {
  return document.querySelector(".ne-media-uploader,.ne-embed-modal,.tippy-content,.ne-link-popover");
};
var validateAndFormatUrl = function validateAndFormatUrl(url) {
  if (!constants$1.ALL_PROTOCOL_URL_REGEXP.test(url)) {
    return null;
  }
  if (!/^[a-zA-Z]\w*:/i.test(url)) {
    return "https://".concat(url);
  }
  return url;
};
var transformEditorContent = function transformEditorContent(content) {
  var _content$replaceAll;
  return content === null || content === void 0 || (_content$replaceAll = content.replaceAll(constants.IMAGE_REGEX, constants.IMAGE_REPLACEMENT_PATTERN)) === null || _content$replaceAll === void 0 || (_content$replaceAll = _content$replaceAll.replaceAll(constants.EMPTY_DIV_REGEX, "")) === null || _content$replaceAll === void 0 ? void 0 : _content$replaceAll.replace(constants.TRAILING_BR_REGEX, "");
};
var isEmojiSuggestionsMenuActive = function isEmojiSuggestionsMenuActive() {
  return !!document.querySelector(".neeto-editor-emoji-suggestion");
};
var transformPastedHTML = function transformPastedHTML(content) {
  var contentWithoutBr = content.replaceAll("<br />", "<p></p>");
  var doc = new DOMParser().parseFromString(contentWithoutBr, "text/html");

  // ProseMirror tags clipboard output from any TipTap/ProseMirror editor with
  // `data-pm-slice`. When present, the paste is coming from another
  // neeto-editor instance and we preserve the author's color choices. When
  // absent, the source is external (Word, websites, customer messages) and we
  // strip inline colors so foreign theming does not bleed into the editor.
  var isInternalPaste = doc.querySelector("[data-pm-slice]") !== null;
  if (!isInternalPaste) {
    doc.querySelectorAll("[style]").forEach(function (el) {
      el.style.color = "";
      el.style.backgroundColor = "";
    });
  }
  return doc.body.innerHTML;
};
var buildLevelsFromOptions = function buildLevelsFromOptions(options) {
  var levels = _defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default({}, constants$1.EDITOR_OPTIONS.H1, 1), constants$1.EDITOR_OPTIONS.H2, 2), constants$1.EDITOR_OPTIONS.H3, 3), constants$1.EDITOR_OPTIONS.H4, 4), constants$1.EDITOR_OPTIONS.H5, 5), constants$1.EDITOR_OPTIONS.H6, 6);
  return Object.entries(levels).filter(function (heading) {
    return options.includes(heading[0]);
  }).map(function (heading) {
    return heading[1];
  });
};

exports.buildLevelsFromOptions = buildLevelsFromOptions;
exports.clipboardTextParser = clipboardTextParser;
exports.getEditorStyles = getEditorStyles;
exports.isEditorOverlaysActive = isEditorOverlaysActive;
exports.isEmojiSuggestionsMenuActive = isEmojiSuggestionsMenuActive;
exports.setInitialPosition = setInitialPosition;
exports.transformEditorContent = transformEditorContent;
exports.transformPastedHTML = transformPastedHTML;
exports.validateAndFormatUrl = validateAndFormatUrl;
//# sourceMappingURL=chunk-4ILtbNxB.cjs.js.map
