const {
  getWxApi
} = require('../../../utils/platform-utils.js')

const handlers = {
  copyToolResult(event) {
    const value = event && event.currentTarget && event.currentTarget.dataset
      ? event.currentTarget.dataset.value
      : ''
    const text = String(value === undefined || value === null ? '' : value).trim()

    if (!text || text === '--') return

    const wxApi = getWxApi()
    if (typeof wxApi.setClipboardData !== 'function') {
      if (this.pageToast) this.pageToast.show('当前环境不支持复制', 'error')
      return
    }

    wxApi.setClipboardData({
      data: text,
      fail: () => {
        if (this.pageToast) this.pageToast.show('复制失败', 'error')
      },
      success: () => {
        if (this.pageToast) this.pageToast.show('已复制')
      }
    })
  }
}

module.exports = {
  handlers
}
