"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _linkifyIt = _interopRequireDefault(require("linkify-it"));
var _adfSchema = require("@atlaskit/adf-schema");
var _utils = require("../../utils");
// File has been copied to packages/editor/editor-plugin-ai/src/provider/markdown-transformer/md/linkify-md-plugin.ts
// If changes are made to this file, please make the same update in the linked file.

// See https://product-fabric.atlassian.net/browse/ED-3097 for why this file exists.

// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var LINK_OPEN_REGEX = /^<a[>\s]/i;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var LINK_CLOSE_REGEX = /^<\/a\s*>/i;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var HTTP_PREFIX_REGEX = /^http:\/\//;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var MAILTO_CASE_INSENSITIVE_REGEX = /^mailto:/i;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var MAILTO_REGEX = /^mailto:/;

// modified version of the original markdown-it Linkify plugin
// https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/linkify.js
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var arrayReplaceAt = function arrayReplaceAt(src, pos, newElements) {
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1));
};
var isLinkOpen = function isLinkOpen(str) {
  return LINK_OPEN_REGEX.test(str);
};
var isLinkClose = function isLinkClose(str) {
  return LINK_CLOSE_REGEX.test(str);
};

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var linkify = function linkify(state) {
  var blockTokens = state.tokens;
  var linkify = new _linkifyIt.default();
  for (var j = 0, l = blockTokens.length; j < l; j++) {
    if (blockTokens[j].type !== 'inline' || !linkify.pretest(blockTokens[j].content)) {
      continue;
    }
    var tokens = blockTokens[j].children;
    var htmlLinkLevel = 0;

    // We scan from the end, to keep position when new tags added.
    // Use reversed logic in links start/end match
    for (var i = tokens.length - 1; i >= 0; i--) {
      var currentToken = tokens[i];

      // Skip content of markdown links
      if (currentToken.type === 'link_close') {
        i--;
        while (tokens[i].level !== currentToken.level && tokens[i].type !== 'link_open') {
          i--;
        }
        continue;
      }

      // Skip content of html tag links
      if (currentToken.type === 'html_inline') {
        if (isLinkOpen(currentToken.content) && htmlLinkLevel > 0) {
          htmlLinkLevel--;
        }
        if (isLinkClose(currentToken.content)) {
          htmlLinkLevel++;
        }
      }
      if (htmlLinkLevel > 0) {
        continue;
      }
      if (currentToken.type === 'text' && linkify.test(currentToken.content)) {
        var text = currentToken.content;
        var links = (0, _adfSchema.linkifyMatch)(text);
        if (!links.length) {
          links = linkify.match(text) || [];
        }
        links = links.filter(function (link) {
          return (0, _utils.shouldAutoLinkifyMatch)(link);
        });

        // Now split string to nodes
        var nodes = [];
        var level = currentToken.level;
        var lastPos = 0;
        var filepaths = (0, _utils.findFilepaths)(text);
        for (var ln = 0; ln < links.length; ln++) {
          if ((0, _utils.isLinkInMatches)(links[ln].index, filepaths)) {
            continue;
          }
          var url = links[ln].url;
          var fullUrl = state.md.normalizeLink(url);
          if (!state.md.validateLink(fullUrl)) {
            continue;
          }
          var urlText = links[ln].text;

          // Linkifier might send raw hostnames like "example.com", where url
          // starts with domain name. So we prepend http:// in those cases,
          // and remove it afterwards.
          //
          if (!links[ln].schema) {
            // eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed)
            urlText = state.md.normalizeLinkText('http://' + urlText).replace(HTTP_PREFIX_REGEX, '');
          } else if (links[ln].schema === 'mailto:' && !MAILTO_CASE_INSENSITIVE_REGEX.test(urlText)) {
            // eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed)
            urlText = state.md.normalizeLinkText('mailto:' + urlText).replace(MAILTO_REGEX, '');
          } else {
            urlText = state.md.normalizeLinkText(urlText);
          }
          var pos = links[ln].index;
          if (pos > lastPos) {
            var _token = new state.Token('text', '', 0);
            _token.content = text.slice(lastPos, pos);
            _token.level = level;
            nodes.push(_token);
          }
          var token = new state.Token('link_open', 'a', 1);
          token.attrs = [['href', fullUrl]];
          token.level = level++;
          token.markup = 'linkify';
          token.info = 'auto';
          nodes.push(token);
          token = new state.Token('text', '', 0);
          token.content = urlText;
          token.level = level;
          nodes.push(token);
          token = new state.Token('link_close', 'a', -1);
          token.level = --level;
          token.markup = 'linkify';
          token.info = 'auto';
          nodes.push(token);
          lastPos = links[ln].lastIndex;
        }
        if (lastPos < text.length) {
          var _token2 = new state.Token('text', '', 0);
          _token2.content = text.slice(lastPos);
          _token2.level = level;
          nodes.push(_token2);
        }

        // replace current node
        blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes);
      }
    }
  }
};

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var _default = exports.default = function _default(md) {
  return md.core.ruler.push('custom-linkify', linkify);
};