{"version":3,"file":"walk-mentions.cjs","names":[],"sources":["../../../../src/components/Mentions/utils/walk-mentions.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 * - walkMentions replaces the iterateMentionsMarkup callback: one pass over the value builds the\n *   plain text and a span per mention (markup and plain-text ranges), getPlainText,\n *   findMentionOccurrences, mapPlainTextIndex and mapMarkupIndex derive from these spans instead\n *   of running their own iterateMentionsMarkup callbacks\n * - Each source parses its mentions with its own serializer (template or custom), the matches\n *   of all sources are merged by index and overlapping matches are dropped, instead of one\n *   combined regex over all markup templates\n * - The trigger prefix is part of the plain text only when withTrigger is set\n * - TypeScript types adjusted for the Mantine build, formatted with oxfmt\n */\n\nimport type { MentionConfig } from './types';\n\nexport interface MentionSpan {\n  markupStart: number;\n  markupEnd: number;\n  sourceIndex: number;\n  id: string;\n  display: string;\n  trigger: string;\n  plainStart: number;\n  plainEnd: number;\n  rendered: string;\n}\n\nexport interface MentionWalk {\n  value: string;\n  plainText: string;\n  spans: MentionSpan[];\n}\n\ninterface FoundMention {\n  configIndex: number;\n  markup: string;\n  index: number;\n  id: string;\n  display: string;\n}\n\nfunction getTriggerText(config: MentionConfig): string {\n  return typeof config.trigger === 'string' ? config.trigger : '';\n}\n\nexport function getMentionPlainText(config: MentionConfig, id: string, display: string): string {\n  return (config.withTrigger ? getTriggerText(config) : '') + config.displayTransform(id, display);\n}\n\nfunction findAllMentions(value: string, configs: MentionConfig[]): FoundMention[] {\n  const all: FoundMention[] = [];\n\n  configs.forEach((config, configIndex) => {\n    for (const match of config.serializer.findAll(value)) {\n      if (match.markup.length === 0) {\n        continue;\n      }\n\n      all.push({\n        configIndex,\n        markup: match.markup,\n        index: match.index,\n        id: String(match.id),\n        display: match.display == null ? String(match.id) : String(match.display),\n      });\n    }\n  });\n\n  all.sort((a, b) => a.index - b.index);\n\n  const result: FoundMention[] = [];\n  let cursor = 0;\n\n  for (const mention of all) {\n    if (mention.index >= cursor) {\n      result.push(mention);\n      cursor = mention.index + mention.markup.length;\n    }\n  }\n\n  return result;\n}\n\nexport function walkMentions(value: string, configs: MentionConfig[]): MentionWalk {\n  const spans: MentionSpan[] = [];\n  let plainText = '';\n  let markupCursor = 0;\n\n  for (const mention of findAllMentions(value, configs)) {\n    const config = configs[mention.configIndex];\n    plainText += value.slice(markupCursor, mention.index);\n\n    const rendered = getMentionPlainText(config, mention.id, mention.display);\n    const markupEnd = mention.index + mention.markup.length;\n\n    spans.push({\n      markupStart: mention.index,\n      markupEnd,\n      sourceIndex: mention.configIndex,\n      id: mention.id,\n      display: mention.display,\n      trigger: getTriggerText(config),\n      plainStart: plainText.length,\n      plainEnd: plainText.length + rendered.length,\n      rendered,\n    });\n\n    plainText += rendered;\n    markupCursor = markupEnd;\n  }\n\n  plainText += value.slice(markupCursor);\n  return { value, plainText, spans };\n}\n"],"mappings":";;AA4CA,SAAS,eAAe,QAA+B;CACrD,OAAO,OAAO,OAAO,YAAY,WAAW,OAAO,UAAU;AAC/D;AAEA,SAAgB,oBAAoB,QAAuB,IAAY,SAAyB;CAC9F,QAAQ,OAAO,cAAc,eAAe,MAAM,IAAI,MAAM,OAAO,iBAAiB,IAAI,OAAO;AACjG;AAEA,SAAS,gBAAgB,OAAe,SAA0C;CAChF,MAAM,MAAsB,CAAC;CAE7B,QAAQ,SAAS,QAAQ,gBAAgB;EACvC,KAAK,MAAM,SAAS,OAAO,WAAW,QAAQ,KAAK,GAAG;GACpD,IAAI,MAAM,OAAO,WAAW,GAC1B;GAGF,IAAI,KAAK;IACP;IACA,QAAQ,MAAM;IACd,OAAO,MAAM;IACb,IAAI,OAAO,MAAM,EAAE;IACnB,SAAS,MAAM,WAAW,OAAO,OAAO,MAAM,EAAE,IAAI,OAAO,MAAM,OAAO;GAC1E,CAAC;EACH;CACF,CAAC;CAED,IAAI,MAAM,GAAG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