{"version":3,"file":"make-trigger-regex.cjs","names":["escapeRegex"],"sources":["../../../../src/components/Mentions/utils/make-trigger-regex.ts"],"sourcesContent":["/*\n * Derived from react-mentions 4.4.10 (https://github.com/signavio/react-mentions)\n * — BSD-3-Clause, Copyright (c) 2018 Signavio GmbH. See ../LICENSE for the full license text.\n *\n * Local changes compared to react-mentions 4.4.10:\n * - Standalone module instead of a helper inside MentionsInput\n * - A query with allowSpaceInQuery never crosses a newline\n * - A RegExp trigger with fewer than two capture groups is wrapped into the anchored\n *   trigger + query shape instead of being used as is, the g and y flags are dropped\n * - The built regexes name their groups mentionsFull and mentionsQuery\n * - TypeScript types adjusted for the Mantine build, formatted with oxfmt\n */\n\nimport { escapeRegex } from './escape-regex';\n\nexport interface MakeTriggerRegexOptions {\n  /** If set, the query is allowed to contain whitespace, it never crosses a newline @default false */\n  allowSpaceInQuery?: boolean;\n}\n\nfunction countCaptureGroups(regex: RegExp) {\n  return new RegExp(`${regex.source}|`).exec('')!.length - 1;\n}\n\nfunction withoutStatefulFlags(regex: RegExp) {\n  const flags = regex.flags.replace(/[gy]/g, '');\n  return flags === regex.flags ? regex : new RegExp(regex.source, flags);\n}\n\n/**\n * Builds an anchored trigger regex used to detect the active query in the text before the caret.\n * The resulting regex has two capture groups: the full match to replace (trigger + query) and the query.\n * A `RegExp` trigger with fewer than two capture groups matches the trigger itself and is wrapped\n * into that shape. A `RegExp` with two or more capture groups is used as is: it must be anchored\n * to the end of the text (`$`), group 1 must capture the trigger and the query and group 2 the query.\n */\nexport function makeTriggerRegex(\n  trigger: string | RegExp = '@',\n  options: MakeTriggerRegexOptions = {}\n): RegExp {\n  const { allowSpaceInQuery } = options;\n\n  if (trigger instanceof RegExp) {\n    if (countCaptureGroups(trigger) >= 2) {\n      return withoutStatefulFlags(trigger);\n    }\n\n    const notTrigger = `(?!${trigger.source})`;\n    const queryPattern = allowSpaceInQuery ? `(?:${notTrigger}[^\\\\n])*` : `(?:${notTrigger}\\\\S)*`;\n    return new RegExp(\n      `(?:^|\\\\s)(?<mentionsFull>(?:${trigger.source})(?<mentionsQuery>${queryPattern}))$`,\n      withoutStatefulFlags(trigger).flags\n    );\n  }\n\n  const escapedTrigger = escapeRegex(trigger);\n  const queryPattern = allowSpaceInQuery ? `[^\\\\n${escapedTrigger}]*` : `[^\\\\s${escapedTrigger}]*`;\n\n  return new RegExp(\n    `(?:^|\\\\s)(?<mentionsFull>${escapedTrigger}(?<mentionsQuery>${queryPattern}))$`\n  );\n}\n"],"mappings":";;;AAoBA,SAAS,mBAAmB,OAAe;CACzC,OAAO,IAAI,OAAO,GAAG,MAAM,OAAO,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAE,SAAS;AAC3D;AAEA,SAAS,qBAAqB,OAAe;CAC3C,MAAM,QAAQ,MAAM,MAAM,QAAQ,SAAS,EAAE;CAC7C,OAAO,UAAU,MAAM,QAAQ,QAAQ,IAAI,OAAO,MAAM,QAAQ,KAAK;AACvE;;;;;;;;AASA,SAAgB,iBACd,UAA2B,KAC3B,UAAmC,CAAC,GAC5B;CACR,MAAM,EAAE,sBAAsB;CAE9B,IAAI,mBAAmB,QAAQ;EAC7B,IAAI,mBAAmB,OAAO,KAAK,GACjC,OAAO,qBAAqB,OAAO;EAGrC,MAAM,aAAa,MAAM,QAAQ,OAAO;EACxC,MAAM,eAAe,oBAAoB,MAAM,WAAW,YAAY,MAAM,WAAW;EACvF,OAAO,IAAI,OACT,+BAA+B,QAAQ,OAAO,oBAAoB,aAAa,MAC/E,qBAAqB,OAAO,CAAC,CAAC,KAChC;CACF;CAEA,MAAM,iBAAiBA,qBAAAA,YAAY,OAAO;CAC1C,MAAM,eAAe,oBAAoB,QAAQ,eAAe,MAAM,QAAQ,eAAe;CAE7F,OAAO,IAAI,OACT,4BAA4B,eAAe,mBAAmB,aAAa,IAC7E;AACF"}