{"version":3,"file":"create-markup-serializer.mjs","names":[],"sources":["../../../../src/components/Mentions/utils/create-markup-serializer.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 * - Combines markupToRegex, makeMentionsMarkup and the placeholder index helpers into one\n *   serializer object (insert + findAll) per markup template\n * - insert replaces both placeholders in one pass with a function replacer, so $ patterns\n *   and placeholder names in an id or display are inserted literally\n * - Placeholders match any character except a newline ([^\\n]+?), so an unclosed opening\n *   delimiter cannot capture a mention on a later line\n * - TypeScript types adjusted for the Mantine build, formatted with oxfmt\n */\n\nimport type { MentionSerializer } from '../Mentions.types';\nimport { escapeRegex } from './escape-regex';\n\nexport const DEFAULT_MENTION_MARKUP = '@[__display__](__id__)';\n\nconst DISPLAY_PLACEHOLDER = '__display__';\nconst ID_PLACEHOLDER = '__id__';\nconst PLACEHOLDERS_REGEX = /__id__|__display__/g;\n\ninterface CompiledMarkup {\n  source: string;\n  displayGroup: number;\n  idGroup: number;\n}\n\nfunction compileMarkup(markup: string): CompiledMarkup {\n  const displayIndex = markup.indexOf(DISPLAY_PLACEHOLDER);\n  const idIndex = markup.indexOf(ID_PLACEHOLDER);\n\n  const source = escapeRegex(markup)\n    .replace(DISPLAY_PLACEHOLDER, '([^\\\\n]+?)')\n    .replace(ID_PLACEHOLDER, '([^\\\\n]+?)');\n\n  const hasDisplay = displayIndex !== -1;\n  const hasId = idIndex !== -1;\n\n  let displayGroup = -1;\n  let idGroup = -1;\n\n  if (hasDisplay && hasId) {\n    if (displayIndex < idIndex) {\n      displayGroup = 1;\n      idGroup = 2;\n    } else {\n      idGroup = 1;\n      displayGroup = 2;\n    }\n  } else if (hasDisplay) {\n    displayGroup = 1;\n  } else if (hasId) {\n    idGroup = 1;\n  }\n\n  return { source, displayGroup, idGroup };\n}\n\n/** Builds a `MentionSerializer` from a template string, for example `@[__display__](__id__)` */\nexport function createMarkupSerializer(template: string): MentionSerializer {\n  const { source, displayGroup, idGroup } = compileMarkup(template);\n\n  return {\n    id: template,\n\n    insert: ({ id, display }) => {\n      const replaced = new Set<string>();\n      return template.replace(PLACEHOLDERS_REGEX, (placeholder) => {\n        if (replaced.has(placeholder)) {\n          return placeholder;\n        }\n        replaced.add(placeholder);\n        return placeholder === ID_PLACEHOLDER ? String(id) : display;\n      });\n    },\n\n    findAll: (value) => {\n      const regex = new RegExp(source, 'g');\n      const result: Array<{ markup: string; index: number; id: string; display?: string | null }> =\n        [];\n\n      for (const match of value.matchAll(regex)) {\n        const id = idGroup !== -1 ? match[idGroup] : displayGroup !== -1 ? match[displayGroup] : '';\n        const display = displayGroup !== -1 ? match[displayGroup] : id;\n        result.push({ markup: match[0], index: match.index ?? 0, id, display });\n      }\n\n      return result;\n    },\n  };\n}\n"],"mappings":";;;AAiBA,MAAa,yBAAyB;AAEtC,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB;AACvB,MAAM,qBAAqB;AAQ3B,SAAS,cAAc,QAAgC;CACrD,MAAM,eAAe,OAAO,QAAQ,mBAAmB;CACvD,MAAM,UAAU,OAAO,QAAQ,cAAc;CAE7C,MAAM,SAAS,YAAY,MAAM,CAAC,CAC/B,QAAQ,qBAAqB,YAAY,CAAC,CAC1C,QAAQ,gBAAgB,YAAY;CAEvC,MAAM,aAAa,iBAAiB;CACpC,MAAM,QAAQ,YAAY;CAE1B,IAAI,eAAe;CACnB,IAAI,UAAU;CAEd,IAAI,cAAc,OAAO;EACvB,IAAI,eAAe,SAAS;GAC1B,eAAe;GACf,UAAU;EACZ,OAAO;GACL,UAAU;GACV,eAAe;EACjB;CACF,OAAO,IAAI,YACT,eAAe;MACV,IAAI,OACT,UAAU;CAGZ,OAAO;EAAE;EAAQ;EAAc;CAAQ;AACzC;;AAGA,SAAgB,uBAAuB,UAAqC;CAC1E,MAAM,EAAE,QAAQ,cAAc,YAAY,cAAc,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