"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DONTLINKIFY_REGEXP = void 0;
Object.defineProperty(exports, "FILEPATH_REGEXP", {
  enumerable: true,
  get: function get() {
    return _FILEPATH_REGEXP.FILEPATH_REGEXP;
  }
});
Object.defineProperty(exports, "LinkMatcher", {
  enumerable: true,
  get: function get() {
    return _LinkMatcher.LinkMatcher;
  }
});
Object.defineProperty(exports, "canLinkBeCreatedInRange", {
  enumerable: true,
  get: function get() {
    return _canLinkBeCreatedInRange.canLinkBeCreatedInRange;
  }
});
exports.findFilepaths = void 0;
exports.getLinkCreationAnalyticsEvent = getLinkCreationAnalyticsEvent;
Object.defineProperty(exports, "getLinkDomain", {
  enumerable: true,
  get: function get() {
    return _getLinkDomain.getLinkDomain;
  }
});
exports.isFromCurrentDomain = isFromCurrentDomain;
exports.isLinkInMatches = void 0;
exports.linkifyContent = linkifyContent;
Object.defineProperty(exports, "normalizeUrl", {
  enumerable: true,
  get: function get() {
    return _normalizeUrl.normalizeUrl;
  }
});
var _adfSchema = require("@atlaskit/adf-schema");
var _enums = require("../analytics/types/enums");
var _FILEPATH_REGEXP = require("./FILEPATH_REGEXP");
var _getLinkDomain = require("./getLinkDomain");
var _normalizeUrl = require("./normalizeUrl");
var _shouldAutoLinkifyTld = require("./should-auto-linkify-tld");
var _slice = require("./slice");
var _LinkMatcher = require("./LinkMatcher");
var _canLinkBeCreatedInRange = require("./canLinkBeCreatedInRange");
// File has been copied to packages/editor/editor-plugin-ai/src/provider/markdown-transformer/utils/hyperlink.ts
// If changes are made to this file, please make the same update in the linked file.

// Don't linkify if starts with $ or {
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var DONTLINKIFY_REGEXP = exports.DONTLINKIFY_REGEXP = /^(\$|\{)/;

/**
 * Linkify content in a slice (eg. after a rich text paste)
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function linkifyContent(schema) {
  return function (slice) {
    return (0, _slice.mapSlice)(slice, function (node, parent) {
      var isAllowedInParent = !parent || parent.type !== schema.nodes.codeBlock;
      var link = node.type.schema.marks.link;
      if (link === undefined) {
        throw new Error('Link not in schema - unable to linkify content');
      }
      if (isAllowedInParent && node.isText && !link.isInSet(node.marks)) {
        var linkified = [];
        // Ignored via go/ees005
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        var text = node.text;
        var matches = findLinkMatches(text).filter(_shouldAutoLinkifyTld.shouldAutoLinkifyMatch);
        var pos = 0;
        var filepaths = findFilepaths(text);
        matches.forEach(function (match) {
          if (isLinkInMatches(match.index, filepaths)) {
            return;
          }
          if (match.index > 0) {
            linkified.push(node.cut(pos, match.index));
          }
          linkified.push(node.cut(match.index, match.lastIndex).mark(link.create({
            href: (0, _normalizeUrl.normalizeUrl)(match.url)
          }).addToSet(node.marks)));
          pos = match.lastIndex;
        });
        if (pos < text.length) {
          linkified.push(node.cut(pos));
        }
        return linkified;
      }
      return node;
    });
  };
}

// eslint-disable-next-line jsdoc/require-jsdoc, @atlaskit/volt-strict-mode/no-multiple-exports
function isFromCurrentDomain(url) {
  if (!window || !window.location) {
    return false;
  }
  var currentDomain = window.location.hostname;
  var linkDomain = (0, _getLinkDomain.getLinkDomain)(url);
  return currentDomain === linkDomain;
}

/**
 * Fetch linkify matches from text
 * @param text Input text from a node
 * @returns Array of linkify matches. Returns empty array if text is empty or no matches found;
 */
function findLinkMatches(text) {
  if (text === '') {
    return [];
  }
  return _adfSchema.linkify.match(text) || [];
}
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var findFilepaths = exports.findFilepaths = function findFilepaths(text) {
  var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  // Creation of a copy of the RegExp is necessary as lastIndex is stored on it when we run .exec()
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp -- Ignored via go/ees005
  var localRegExp = new RegExp(_FILEPATH_REGEXP.FILEPATH_REGEXP);
  var match;
  var matchesList = [];
  var maxFilepathSize = 260;
  // Ignored via go/ees005
  // eslint-disable-next-line no-cond-assign
  while ((match = localRegExp.exec(text)) !== null) {
    var start = match.index + offset;
    var end = localRegExp.lastIndex + offset;
    if (end - start > maxFilepathSize) {
      end = start + maxFilepathSize;
    } // We don't care about big strings of text that are pretending to be filepaths!!
    matchesList.push({
      startIndex: start,
      endIndex: end
    });
  }
  return matchesList;
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var isLinkInMatches = exports.isLinkInMatches = function isLinkInMatches(linkStart, matchesList) {
  for (var i = 0; i < matchesList.length; i++) {
    if (linkStart >= matchesList[i].startIndex && linkStart < matchesList[i].endIndex) {
      return true;
    }
  }
  return false;
};

// eslint-disable-next-line jsdoc/require-jsdoc, @atlaskit/volt-strict-mode/no-multiple-exports
function getLinkCreationAnalyticsEvent(inputMethod, url) {
  return {
    action: _enums.ACTION.INSERTED,
    actionSubject: _enums.ACTION_SUBJECT.DOCUMENT,
    actionSubjectId: _enums.ACTION_SUBJECT_ID.LINK,
    attributes: {
      inputMethod: inputMethod,
      fromCurrentDomain: isFromCurrentDomain(url)
    },
    eventType: _enums.EVENT_TYPE.TRACK,
    nonPrivacySafeAttributes: {
      linkDomain: (0, _getLinkDomain.getLinkDomain)(url)
    }
  };
}
// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export