import { debounce } from 'lodash-es'
import notif from '@/utils/notif'
import IColor from '@/models/IColor'
import colors from '@/data/colors'

export const confirmation = debounce(
  (message: string) => {
    notif.success(message)
  },
  3000,
  {
    leading: true
  }
)

export const alertMessage = debounce(
  (message: string) => {
    notif.error(message)
  },
  3000,
  {
    leading: true
  }
)

export const primary: string = '#3f4fa6'
export const main: string = '#2c3e50'

export const slug = (text: string): string => {
  if (!text) {
    return ''
  }
  return text
    .toString()
    .toLowerCase()
    .replace(/\s+/g, '-') // Replace spaces with -
    .replace(/[^\w\-]+/g, '') // Remove all non-word chars
    .replace(/\-\-+/g, '-') // Replace multiple - with single -
    .replace(/^-+/, '') // Trim - from start of text
    .replace(/-+$/, '') // Trim - from end of text
}

export const findDarkValue = (value: string): string | null => {
  if (!value) {
    return null
  }
  const find: IColor | undefined = colors.find(
    (color: IColor) => color.value === value
  )
  return find && find.darkValue ? find.darkValue : null
}

export const findLightValue = (value: string): string | null => {
  if (!value) {
    return null
  }
  const find: IColor | undefined = colors.find(
    (color: IColor) => color.value === value
  )
  return find && find.lightValue ? find.lightValue : null
}

export const toHex = (text: string): string => {
  return (
    [...text]
      // tslint:disable-next-line: variable-name
      .map((_c: string, i: number) => Number(text.charCodeAt(i)).toString(16))
      .join('')
  )
}

export const getCurrentPosition = (options = {}): Promise<Position> => {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition(resolve, reject, options)
  })
}
