// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { useClipboardItems, whenever } from '@vueuse/core'

import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'

const isSafeHref = (href: string) => {
  try {
    return ['http:', 'https:'].includes(new URL(href, document.baseURI).protocol)
  } catch {
    return false
  }
}

export const createLinkClipboardItem = (href: string, label: string) => {
  if (!isSafeHref(href)) return new ClipboardItem({ 'text/plain': label })

  const anchor = document.createElement('a')
  anchor.href = href
  anchor.textContent = label

  return new ClipboardItem({
    'text/plain': label,
    'text/html': anchor.outerHTML,
  })
}

export const useCopyToClipboard = () => {
  const { copy: copyClipboardItems, copied: copiedToClipboard } = useClipboardItems()
  const { notify } = useNotifications()

  const copyToClipboard = (input?: string | ClipboardItem[] | null) => {
    if (typeof input === 'undefined' || input === null) return

    let source = input

    if (typeof input === 'string') {
      source = [new ClipboardItem({ 'text/plain': input })]
    }

    copyClipboardItems(source as ClipboardItem[]).catch(() => {
      notify({
        id: 'copy-to-clipboard-failed',
        type: NotificationTypes.Error,
        message: __('The content could not be copied to the clipboard.'),
      })
    })
  }

  whenever(copiedToClipboard, () => {
    notify({
      id: 'copied-to-clipboard',
      type: NotificationTypes.Success,
      message: __('Copied.'), // TODO should this not be something given to the composable for a more meaningful message?
    })
  })

  return {
    copiedToClipboard,
    copyToClipboard,
  }
}
