{"version":3,"file":"detect-query.cjs","names":[],"sources":["../../../src/components/Mentions/detect-query.ts"],"sourcesContent":["import type { ResolvedMentionSource } from './resolve-sources';\nimport type { MentionSpan } from './utils/walk-mentions';\n\nexport interface ActiveMentionQuery {\n  sourceIndex: number;\n  query: string;\n  queryStart: number;\n  queryEnd: number;\n}\n\nexport function detectQuery(\n  plainText: string,\n  caret: number,\n  sources: ResolvedMentionSource[],\n  spans: MentionSpan[] = []\n): ActiveMentionQuery | null {\n  const before = plainText.slice(0, caret);\n  let active: ActiveMentionQuery | null = null;\n\n  for (const source of sources) {\n    const match = before.match(source.regex);\n    const full = match?.groups?.mentionsFull ?? match?.[1];\n    if (!match || full === undefined) {\n      continue;\n    }\n\n    const query = match.groups?.mentionsQuery ?? match[2] ?? '';\n    const queryStart = before.length - full.length;\n\n    if (!active || queryStart > active.queryStart) {\n      active = { sourceIndex: source.sourceIndex, query, queryStart, queryEnd: caret };\n    }\n  }\n\n  if (!active) {\n    return null;\n  }\n\n  const queryStart = active.queryStart;\n  const overlapsMention = spans.some(\n    (span) => span.plainStart < caret && span.plainEnd > queryStart\n  );\n\n  return overlapsMention ? null : active;\n}\n\nexport function isQueryInText(\n  query: ActiveMentionQuery,\n  plainText: string,\n  sources: ResolvedMentionSource[],\n  spans: MentionSpan[] = []\n): boolean {\n  if (query.queryEnd > plainText.length) {\n    return false;\n  }\n\n  const detected = detectQuery(plainText, query.queryEnd, sources, spans);\n  return (\n    detected !== null &&\n    detected.sourceIndex === query.sourceIndex &&\n    detected.queryStart === query.queryStart &&\n    detected.query === query.query\n  );\n}\n"],"mappings":";;AAUA,SAAgB,YACd,WACA,OACA,SACA,QAAuB,CAAC,GACG;CAC3B,MAAM,SAAS,UAAU,MAAM,GAAG,KAAK;CACvC,IAAI,SAAoC;CAExC,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,QAAQ,OAAO,MAAM,OAAO,KAAK;EACvC,MAAM,OAAO,OAAO,QAAQ,gBAAgB,QAAQ;EACpD,IAAI,CAAC,SAAS,SAAS,KAAA,GACrB;EAGF,MAAM,QAAQ,MAAM,QAAQ,iBAAiB,MAAM,MAAM;EACzD,MAAM,aAAa,OAAO,SAAS,KAAK;EAExC,IAAI,CAAC,UAAU,aAAa,OAAO,YACjC,SAAS;GAAE,aAAa,OAAO;GAAa;GAAO;GAAY,UAAU;EAAM;CAEnF;CAEA,IAAI,CAAC,QACH,OAAO;CAGT,MAAM,aAAa,OAAO;CAK1B,OAJwB,MAAM,MAC3B,SAAS,KAAK,aAAa,SAAS,KAAK,WAAW,UAGlC,IAAI,OAAO;AAClC;AAEA,SAAgB,cACd,OACA,WACA,SACA,QAAuB,CAAC,GACf;CACT,IAAI,MAAM,WAAW,UAAU,QAC7B,OAAO;CAGT,MAAM,WAAW,YAAY,WAAW,MAAM,UAAU,SAAS,KAAK;CACtE,OACE,aAAa,QACb,SAAS,gBAAgB,MAAM,eAC/B,SAAS,eAAe,MAAM,cAC9B,SAAS,UAAU,MAAM;AAE7B"}