import moment from 'moment'

export const utilsMixin = {
  methods: {
    formatValue: function (value, formatType) {
      if (value === null || value === undefined)
        return value
      switch (formatType) {
      case 'integer': return value.toLocaleString()
      case 'numeric': return value.toLocaleString()
      case 'money': return value.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
      case 'date': return moment(value).format('YYYY-MM-DD')
      case 'datetime': return moment(value).format('YYYY-MM-DD HH:mm:ss')
      case 'bytes': return this.formatBytes(value)
      default: return value
      }
    },
    fV: function (value, formatType) {
      return this.formatValue(value, formatType)
    },
    formatBytes: function (bytes, decimals = 2) {
      if (bytes === 0) return '0 Bytes'

      const k = 1024
      const dm = decimals < 0 ? 0 : decimals
      const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

      const i = Math.floor(Math.log(bytes) / Math.log(k))

      return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
    }
  }
}