import { Message } from '@uino/kiss-ui'

const _toString = Object.prototype.toString

export function isObject(o) {
  return _toString.call(o) === '[object Object]'
}

export function debounce(func, wait, immediate) {
  var timeout, result

  var debounced = function () {
    var context = this
    var args = arguments

    if (timeout) clearTimeout(timeout)
    if (immediate) {
      // 如果已经执行过，不再执行
      var callNow = !timeout
      timeout = setTimeout(function () {
        timeout = null
      }, wait)
      if (callNow) result = func.apply(context, args)
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args)
      }, wait)
    }
    return result
  }

  debounced.cancel = function () {
    clearTimeout(timeout)
    timeout = null
  }

  return debounced
}

Message.config({
  top: 50,
  duration: 3
})
export const message = Message

export function copy(text) {
  const input = document.createElement('input')
  document.body.appendChild(input)
  input.setAttribute('value', JSON.stringify(text))
  input.select()
  if (document.execCommand('copy')) {
    document.execCommand('copy')
  }
  document.body.removeChild(input)
  message.success('信息已经复制在您的剪贴板中')
}

