import moment from 'moment'

export const utilsMixin = {
  methods: {
    formatValue: function (value, formatType) {
      if (value === null || value === undefined)
        return value
      switch (formatType) {
      case 'integer': return new Intl.NumberFormat('pl-PL', { useGrouping: true, maximumFractionDigits: 0 }).format(value)
      case 'numeric': return new Intl.NumberFormat('pl-PL', { useGrouping: true }).format(value)
      case 'money': return new Intl.NumberFormat('pl-PL', { 
        useGrouping: true,
        minimumFractionDigits: 2,
        maximumFractionDigits: 16
      }).format(value)
      case 'date': return moment(value).format('YYYY-MM-DD')
      case 'datetime': return moment(value).format('YYYY-MM-DD HH:mm:ss')
      case 'time': return moment(value).format('HH:mm')
      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]
    }
  }
}
