import Vue from 'vue'
import Clipboard from 'clipboard'

function clipboardSuccess() {
  console.log('success')
  Vue.prototype.$message({
    message: '复制成功',
    type: '系统提示',
    duration: 1500
  })
}

function clipboardError() {
  console.log('error')
  Vue.prototype.$message({
    message: '复制失败',
    type: '系统提示'
  })
}

export default function handleClipboard(text, event) {
  const clipboard = new Clipboard(event.target, {
    text: () => text
  })
  clipboard.on('success', () => {
    clipboardSuccess()
    clipboard.off('error')
    clipboard.off('success')
    clipboard.destroy()
  })
  clipboard.on('error', () => {
    clipboardError()
    clipboard.off('error')
    clipboard.off('success')
    clipboard.destroy()
  })
  clipboard.onClick(event)
}
