50
  6   iˆl–Ãa¾”ªi?u8 ¸Û71hß�þct+8àYŒd(\p¬ÿ?åƒâäÇÀâ q    i// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { type MaybeRef, nextTick, toValue } from 'vue'

import type { FormFieldValue } from '#shared/components/Form/types.ts'

import { waitForAnimationFrame } from './helpers.ts'

export const domFrom = (html: string, document_ = document) => {
  const dom = document_.createElement('div')
  dom.innerHTML = html
  return dom
}

export const removeSignatureFromBody = (input: FormFieldValue, placeholder: boolean = false) => {
  if (!input || typeof input !== 'string') {
    return input
  }

  const dom = domFrom(input)

  dom.querySelectorAll('div[data-signature-placeholder]').forEach((elem) => elem.remove())

  dom.querySelectorAll('div[data-signature]').forEach((elem) => {
    if (placeholder) {
      elem.replaceWith(domFrom('<div data-signature-placeholder="true"></div>'))
    } else {
      elem.remove()
    }
  })

  return dom.innerHTML
}

export const setAutoDirectionOnChildElements = (container: HTMLElement) => {
  container.querySelectorAll('*').forEach((child) => {
    if (child.hasAttribute('dir')) return

    child.setAttribute('dir', 'auto')
  })
}

/**
 * Queries all images in the container and waits for them to load.
 * */
export const waitForImagesToLoad = async (container: MaybeRef) => {
  const inlineImages: HTMLImageElement[] = toValue(container).querySelectorAll('img')

  if (inlineImages.length > 0) {
    return Promise.allSettled<null>(
      Array.from(inlineImages).map((image) => {
        return new Promise((resolve, reject) => {
          const cleanup = () => {
            image.onload = null
            image.onerror = null
          }

          const handleLoad = () => {
            cleanup()
            resolve(null)
          }

          const handleError = () => {
            cleanup()
            reject()
          }

          image.onload = handleLoad
          image.onerror = handleError

          // An image that is already complete fires no further event: it is either one the browser
          // already has (cache, or a body swap re-rendering the same image), or one without a usable
          // source, which stays complete forever.
          if (image.complete)
            if (image.naturalWidth > 0) handleLoad()
            else handleError()
        })
      }),
    )
  }

  return Promise.allSettled<null>([])
}

export const scrollIntoView = async (
  scrollElement: MaybeRef<HTMLElement | null | undefined>,
  block: 'start' | 'end',
  options?: { behavior: ScrollOptions['behavior']; postFlush?: boolean },
) => {
  const { behavior = 'smooth', postFlush } = options || {}

  if (postFlush) {
    await nextTick()
    await waitForAnimationFrame()
  }

  const container = toValue(scrollElement)
  if (!container || !container?.scrollTo) return

  const top = block === 'start' ? 0 : container.scrollHeight

  container?.scrollTo({
    behavior,
    top,
  })
}
     kˆÜÛä448ÚÀâ À    k‹      �“_OÛ0Åßùw~à­¹ýLÇˆ%•V©´UÉ4MhšÛI,’8r<
|ú9MÝD·ÁSœøwOÎ¹×¦âe”|[Másr=‡Õ—OóYd€øu!ÆIÜoœÃâtA€Î5çˆ›Í&ØLcsLÖX¸ª<ÁÖY-\ �$ìˆvßØÐBqéŸ@�v¥b³Zª0 ùéZ-ÕpŒO¼ª¸Ä¶QK�"Nß«V	c%Å¾ÐKáN‹¦F>v¿½GÎWmíðt«Ú--£®€{^ê¼‰3aTW9´V„µ0u‹iÉë» ×^º�ÜÎ¢åwÏ¡+ºjF9Ve!¹ŒÂÅÅ2¼"lÁ+E‘¿Î\÷Ìœ·*#u¦•<ßôð�~:,{&&,V­°ºqÚÔ/(úˆûQ…)Û†û¬§>Aa÷©‘ÿê¿5cuµž.’x¶î[";‰wa+nUí Ö~@ÎØÇ�ë^â¸NÛæâùvž¬ÎGÀ ^%ß$3¥Tv?Ê¡¥wºÎ½ãçÕ¾Ó?¬�‡ã³Áh4„áÙùéà]þsd¸;Ì·7ƒbù~ÓÖŽß       mˆÜÛä528ÚÀâ     m‹      µ”QoÚ0Çßû)n~Ø[r	:¨“©#HC£€(Ó4M{0±C¬†8rÜ±îÓÏ!LÍ&hÙª=å’üïòÿÝ]L_ÅóÑêóbïW7SX||7�Œ€8ˆŸº#Äx7/×óÇ3$3¦"îv;w×u•Þàj‰™ÙæVFËÄ¸Üp]ÐúYt4ŒÛ+P#M.¢IÁÅwP) º7•ä"@a,³/Ý¦å–b£´¹xH¦kÅêšþYùV¶ÿ [ïËÔ¡Ž¨ÉàËå¦‰Q%‰¨Ün ÒIHP&ª¨p�³âÎÝÈ” ËMH¾LFó¯V‡&«³#Ê Ó"ÉÛQ8»š‡×$š±­ ÈŽknÍ”U¶ŠËT
~R|Ûˆoå�Óc«‰I‹*Ñ²4R�R´ˆmÔDåUÉ,kÏdº­j$ü¹n°ä·f,®—ãÙ*ž,›–ðºÄ£»ãÓ Ñ‚iQˆ¥‰Qúá`¸É~]¬«òê×-ìh¹É‰ 8ªü†Tå\è6Å)ÿVžºÜ>mŸëx�¾ãûNg ^è÷ þ#A3é6��ñQÛsÇn¿[�¨}÷‹×s|¼7Cß?É¸Á‡—²Üw…ÚgÂ¸²�{Ž:x¡ëxÇë€7x
.½#œ¿P¥Rù~¡Á9å×Cúÿ¸Pgþüx8)îUŠÍAý‚'HL    5   oˆl–ßi~”*hŠ�À¹q–ÔÅ£�þJàj8ÂYuñ”€þæ