/**
 * https://www.npmjs.com/package/escape-string-regexp
 */
export const sanitizeForRegex = (unsafeText: string): string =>
  unsafeText.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');

export const HTTP_URL_PATTERN = `https?:\\/\\/(?:www\\.)?(?:[^\\s)]*)(?<![.,:;!/?()[\\]\\s]+)`;

export const URL_REG = new RegExp(HTTP_URL_PATTERN, 'g');

export const EMAIL_REGEX =
  /^(([^<>()[\]\\.,;:\s@\\"]+(\.[^<>()[\]\\.,;:\s@\\"]+)*)|(\\".+\\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export const URL_NEG_LB = '(?<!(https?|ftp|mailto|magnet):\\/\\/\\S*)';

// https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block)
export const VARIATION_SELECTOR_PATTERN = String.raw`[\uFE00-\uFE0F]`;

/**
 * One emoji grapheme via Unicode property escapes.
 * Avoids surrogate-pair character classes — Rolldown/minifiers corrupt those and
 * break jumbo emoji + scaleSystemEmoji in production builds.
 *
 * Covers: pictographs (+ skin tone / VS16 / ZWJ sequences), flag pairs, keycaps.
 */
export const EMOJI_SEQUENCE = String.raw`(?:\p{RI}\p{RI}|\p{Extended_Pictographic}(?:\p{Emoji_Modifier}|\uFE0F)?(?:\u200D(?:\p{Extended_Pictographic}|\p{Emoji_Component})(?:\p{Emoji_Modifier}|\uFE0F)?)*|[#*0-9]\uFE0F?\u20E3)`;

/** @deprecated Prefer EMOJI_SEQUENCE — kept as alias for older imports. */
export const EMOJI_PATTERN = EMOJI_SEQUENCE;

/** Match emoji in free text (skip when already part of a URL). */
export const EMOJI_REG_G = new RegExp(`${URL_NEG_LB}(${EMOJI_SEQUENCE})`, 'gu');

/** Plain-text messages that are only emoji / :shortcodes: (up to 10). */
export const JUMBO_EMOJI_REG = new RegExp(
  `^(?:(?:${EMOJI_SEQUENCE}|:[^\\s:]+:)(?:${VARIATION_SELECTOR_PATTERN}|\\s)*){1,10}$`,
  'u'
);
