1
  $   ˆÄÃÈ550Âa–äY>” ”¾R( 	ÅåÆCpAS�ÿÇ &    ‹      ÅUÁŽÚ0½ïW¸>ô–Zª]ÇÕ– •ÄRUUÕƒ‰±6‰#Û�Ò¯¯C@¤twUÚ’'/ž÷žGò"šôçŸ§ô~~7BÓ�ïFÃ>ÂÀ§N šGõ‹®ßjÆáÄÚâ ,K¿ìøJ¯`>ƒÄfiŒÕ2¶>·Ó+R=£Wˆ$‚q±Ò¦‚s.¾!µD ÖÖH.‚C\eŒÈùR¦Âç"S~büX©Âà*6ž1éíÊmp±‘±0°‘Ú®Y
Ön«+xf½p +²jÝ#PWw|`Oˆ,ßV<Ûÿ�“+½3†-vÔªTSb´a©\å!¶ªÀ”Èl…ŒŽC2V¹�EÊò%—±Ô†øË°?ùêp`“êkJJ´X†øm?ßLÂÓ1Ëvsç0·˜Ž˜±(S\.¥àgÁ÷5ø^~?¿aTc"ab-+U~„‚“Ø”«ÔÌi}å$º‰ª!ü17Xü“ÓÛÙ`<�†³Ú^mqd÷ÏNÓ)Ó"·(’ZÄVéíÞƒšÐË|aŠ›Ãíj¹J,¦yè$ò¶¬ó‡\•yÓçúIOœLÝ­IðFA+xíµ¯ vïºÓBèÐMŠJ}©R.t“ù¹Ó,T)´³ºŽ—à	ëŸÎ¿Ù.ÇüQmÏ)p:º½¿ªã-´×ë˜Öñ~ßHO °­vÃž@ýSúº3Æº÷       ˆÅÄÉ587Ã¾Ç K    ‹      ÕV]OÛ0}çWx~Ø[sia Aê‰5H«¥‚NÓ4M‘c;‰EbG¶ÛÒýúÝ6í(ÝÇÃ˜údûúäÞsŽ¿¿Jn“ÏãKò~r}EÆß]„v > ’IÒNG‡]€Ë%´¡9˜ÏçÑü(²®€É-”¡®ŽÁ§Eˆd�”ÄË; q©¸Ä–ÄA‡J±¡‘ê�Øœ€�¯¥ÂÌÍ4«´Ø@ÚÚƒWFæºR‘Tµ�J	k,$­ðï«Ë.<H5ƒ¬²âNÏ02Í0T^rÐ&¨Âé°ˆ¡¥€¤`Í*Î¬\,Évÿ/1¬¿²ˆg+~Ë®cq(ÉŒWº0}lCY¬ë‚x'ú´°Æcfnî£Bç”ð*ôé—áàæ+â ”Ë¯YÌIéTÞ§oýÑùM?¡lÄks�˜Ê®¸¤¶RçZÉ�à»|§¿íN˜´˜Dyát´5�P@‰ÛR…­|ÃQëTPºmT‘¿sƒ‹'fŒ/n/G“dxÛZ"—)Ù½Ø2S6æN™@í”Ö-Ö´„^›Ì7ç›!Y+tº(e„tÈ³È¿°ejî��›mgÐõg=AZ¨TûôÇÞLo–»’²_LnúIAï°wÒéö:‡=Ò==ë�²ˆwÌ‡”š[Ws,Û¶{( q6àvÂ#µZ‡‰Ól”§lçÔÊtxQ§3åt¾ lk°‡R/R�·%e›ÞŠ˜ã¹Wi¡Œr< ”§ã/èX?¥1¬^øÚß‘ïbÈ4%ñ    K   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þDð£YÇÌc!@á
çË„ó?Ê_“I|¥ŽõHºl=$¤�zTÖ™	ÀÉ (+    // Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { nextTick, onMounted, onUnmounted, watch, type ComputedRef, type Ref } from 'vue'

import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
import type { TicketArticleHighlightedText } from '#shared/graphql/types.ts'
import { i18n } from '#shared/i18n.ts'

import type { AnnouncerHandler } from '#desktop/composables/accessibility/types.ts'
import { useTicketArticleHighlightedTextUpsertMutation } from '#desktop/entities/ticket-article/graphql/mutations/highlightedTextUpsert.api.ts'

import { useHighlightMenuState } from '../../TicketDetailTopBar/composables/useHighlightMenuState.ts'

import { collectCharAnchors, extractText } from './utils.ts'

import type { CharAnchor, HighlightRange, SelectionContext } from './types.ts'

const sortByStart = (a: HighlightRange, b: HighlightRange) => a.startIndex - b.startIndex

const toRange = ({ startIndex, endIndex, colorClass }: HighlightRange): HighlightRange => ({
  startIndex,
  endIndex,
  colorClass,
})

const carveOutSelection = (
  highlight: HighlightRange,
  startIndex: number,
  endIndex: number,
): HighlightRange[] => {
  // No overlap with the selected interval.
  if (highlight.endIndex <= startIndex || highlight.startIndex >= endIndex)
    return [toRange(highlight)]

  const segments: HighlightRange[] = []

  // Keep left remainder.
  if (highlight.startIndex < startIndex)
    segments.push({
      startIndex: highlight.startIndex,
      endIndex: startIndex,
      colorClass: highlight.colorClass,
    })

  // Keep right remainder.
  if (highlight.endIndex > endIndex)
    segments.push({
      startIndex: endIndex,
      endIndex: highlight.endIndex,
      colorClass: highlight.colorClass,
    })

  return segments
}

const mergeAdjacentSameColor = (highlights: HighlightRange[]): HighlightRange[] => {
  const sorted = [...highlights].sort(sortByStart)
  const merged: HighlightRange[] = []

  sorted.forEach((highlight) => {
    const previous = merged.at(-1)

    if (
      previous?.colorClass === highlight?.colorClass &&
      previous.endIndex >= highlight.startIndex
    ) {
      previous.endIndex = Math.max(previous.endIndex, highlight.endIndex)
      return
    }

    merged.push({ ...highlight })
  })

  return merged
}

const findLastIndex = (
  anchors: Array<CharAnchor | null>,
  predicate: (anchor: CharAnchor) => boolean,
  fallback: number,
): number => {
  const lastIndex = anchors.findLastIndex((anchor) => anchor !== null && predicate(anchor))

  return lastIndex >= 0 ? lastIndex + 1 : fallback
}

const domStartToCharIndex = (
  anchors: Array<CharAnchor | null>,
