/*
 * @format
 */
import moment from 'moment'
import {KeyValue, TimePeriods} from '../types/types'
export const getPseudoRandomId = () => `VNTGPLS_${performance.now()}`
export const removeLetters = (value: string | number): string =>
  String(value).replace(/\D/gim, '')
export const removeLettersLeaveDots = (value: string | number): string =>
  String(value).replace(/[^\d.]/gim, '')
export const sqftToSqm = (sqft: number): string => (sqft * 0.092903).toFixed(2)
export const sqmToSqft = (sqm: number): string => (sqm * 10.76391).toFixed(2)
import { v4 as uuidv4 } from 'uuid'

export const nFormatter = (num: number, digits: number) => {
  const lookup = [
    {value: 1, symbol: ''},
    {value: 1e3, symbol: 'k'},
    {value: 1e6, symbol: 'M'},
    {value: 1e9, symbol: 'G'},
    {value: 1e12, symbol: 'T'},
    {value: 1e15, symbol: 'P'},
    {value: 1e18, symbol: 'E'}
  ]
  const rx = /\.0+$|(\.[0-9]*[1-9])0+$/

  const item = lookup
    .slice()
    .reverse()
    .find((obj: any) => num >= obj.value)

  return item
    ? (num / item.value).toFixed(digits).replace(rx, '$1') + item.symbol
    : '0'
}
export enum DateFormats {
  DD_MM = 'DD_MM',
  MM_YY = 'MM_YY',
  DD_MM_YY = 'DD_MM_YY',
  YY = 'YY',
  MM = 'MM'
}

export const dateFormatter = (input: string, format: DateFormats): string => {
  const date = new Date(input)
  const day = date.getDate()
  const month = date.toLocaleString('default', {month: 'short'})
  const year = date.getFullYear()

  switch (format) {
    case DateFormats.DD_MM:
      return `${day} ${month}`
    case DateFormats.MM_YY:
      return `${month} ${year}`
    case DateFormats.DD_MM_YY:
      return `${day} ${month} ${year}`
    case DateFormats.YY:
      return `${year}`
    case DateFormats.MM:
      return `${month}`
    default:
      return input
  }
}

// temporary solution as we dont have data format now
export const groupArrayBy = (prop: string | number, arr: Array<any>) =>
  Object.values(
    arr.reduce(
      (a: {[x: string]: any}, x: {[x: string]: string | number}) => ({
        ...a,
        [x[prop]]: [...(a[x[prop]] || []), x]
      }),
      {}
    )
  )
export const scatterDataFormatter = (data: any, timePeriod: TimePeriods) => {
  switch (timePeriod) {
    case TimePeriods.Montly:
      return data
        .sort(
          (a: any, b: any) =>
            new Date(a.X_AXIS_VALUE).getTime() -
            new Date(b.X_AXIS_VALUE).getTime()
        )
        .map((item: any) => ({
          date: item.X_AXIS_VALUE,
          date2: Math.floor(new Date(item.X_AXIS_VALUE).getTime()),
          price: item.Y_AXIS_VALUE,
          address: '9, Jalan Setia Villa',
          id: uuidv4()
        }))
    case TimePeriods.Quarterly:
      return data.map((item: any) => ({
        price: item.Y_AXIS_VALUE,
        date: item.X_AXIS_VALUE,
        address: '9, Jalan Setia Villa',
        id: uuidv4()
      }))
    case TimePeriods.Yearly:
      return data
        .map((item: any) => ({
          price: item.Y_AXIS_VALUE,
          date: item.X_AXIS_VALUE,
          address: '9, Jalan Setia Villa',
          id: uuidv4()
        }))
        .sort(
          (a: {date: any}, b: {date: any}) => Number(a.date) - Number(b.date)
        )
    default:
      return data
        .sort(
          (a: any, b: any) =>
            new Date(a.X_AXIS_VALUE).getTime() -
            new Date(b.X_AXIS_VALUE).getTime()
        )
        .map((item: any) => ({
          date: item.X_AXIS_VALUE,
          date2: Math.floor(new Date(item.X_AXIS_VALUE).getTime()),
          price: item.Y_AXIS_VALUE,
          address: '9, Jalan Setia Villa',
          id: uuidv4()
        }))
  }
}

export const setChartTicks = (data: any, keyName: string | number) => {
  const set = new Set('')
  data.forEach((item: any) => set.add(`${item[keyName]}`))

  return Array.from(set)
}

export const tickFormatter = (tick: any, currentTimePeriod: TimePeriods) => {
  if (currentTimePeriod === TimePeriods.Montly) {
    return moment(tick.trim()).format('MMM YY').toUpperCase()
  }

  return tick
}

export const formatLineChartData = (array: any) => {
  const dataKeys = new Set()
  const processedData =  array.map((array: any) => {
    const obj: KeyValue = {
      date: array[0].X_AXIS_VALUE,
      id: uuidv4()
    }

    array.forEach((item: any) => {
      const keyName = item.LINE_DESC
      dataKeys.add(keyName)
      obj[keyName] = {
        25: item['25'],
        75: item['75'],
        price: item.Y_AXIS_VALUE,
        address: '9, Jalan Setia Villa',
        numOfTransactions: item.numOfTransactions
      }
    })

    return obj
  })

  return {processedData, dataKeys}
}