import { ref } from 'vue'
import { Message } from '@arco-design/web-vue'

/**
 * 拷贝路径 composable（3-tier fallback: Wails native → clipboard API → execCommand）
 */
export function useClipboardCopy() {
  const copied = ref(false)
  let copyTimer: ReturnType<typeof setTimeout> | null = null

  const copy = async (path: string) => {
    if (!path || copied.value) return

    try {
      await navigator.clipboard.writeText(path)
      copied.value = true
    } catch {
      try {
        const input = document.createElement('input')
        input.style.position = 'fixed'
        input.style.opacity = '0'
        input.value = path
        document.body.appendChild(input)
        input.select()
        document.execCommand('copy')
        document.body.removeChild(input)
        copied.value = true
      } catch {
        Message.error('复制失败')
      }
    }

    if (copyTimer) clearTimeout(copyTimer)
    copyTimer = setTimeout(() => { copied.value = false }, 2000)
  }

  const cleanup = () => {
    if (copyTimer) { clearTimeout(copyTimer); copyTimer = null }
  }

  return { copied, copy, cleanup }
}
