// 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.

import { linkify } from '@atlaskit/adf-schema';
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '../analytics/types/enums';
import { FILEPATH_REGEXP } from './FILEPATH_REGEXP';
import { getLinkDomain } from './getLinkDomain';
import { normalizeUrl } from './normalizeUrl';
import { shouldAutoLinkifyMatch } from './should-auto-linkify-tld';
import { mapSlice } from './slice';

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

/**
 * Linkify content in a slice (eg. after a rich text paste)
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export function linkifyContent(schema) {
  return function (slice) {
    return 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(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: 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
export function isFromCurrentDomain(url) {
  if (!window || !window.location) {
    return false;
  }
  var currentDomain = window.location.hostname;
  var linkDomain = 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 linkify.match(text) || [];
}
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var 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);
  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
export var 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
export function getLinkCreationAnalyticsEvent(inputMethod, url) {
  return {
    action: ACTION.INSERTED,
    actionSubject: ACTION_SUBJECT.DOCUMENT,
    actionSubjectId: ACTION_SUBJECT_ID.LINK,
    attributes: {
      inputMethod: inputMethod,
      fromCurrentDomain: isFromCurrentDomain(url)
    },
    eventType: EVENT_TYPE.TRACK,
    nonPrivacySafeAttributes: {
      linkDomain: getLinkDomain(url)
    }
  };
}
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { FILEPATH_REGEXP } from './FILEPATH_REGEXP';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { LinkMatcher } from './LinkMatcher';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { normalizeUrl } from './normalizeUrl';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { getLinkDomain } from './getLinkDomain';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { canLinkBeCreatedInRange } from './canLinkBeCreatedInRange';