// 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,
  })
}
