import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import axios from 'axios'

export const useGlobalStore = defineStore('globalStore', () => {
  const menuLoaded = ref<boolean>(false)
  const animateTime = ref<number>(300)
  const sideMenu = ref<boolean>(true)
  const sideMenuWidth = ref<number>(0)
  const screenWidth = ref<number>(0)
  const mainPadding = ref<number>(24)
  const notificationPanel = ref<boolean>(false)
  const profileMenu = ref<boolean>(false)
  const breakPoints = reactive<Record<string, any>>({
    mobile: 576,
    tablet: 992,
    tabletp: 768
  })
  const perPage = ref<number>(10)
  const selUser = ref<number>(0)
  const selAuthUser = ref<number>(0)
  const selCustomer = ref<number>(0)
  const selCustomerType = ref<number>(0)
  const imageFormats = ref<string[]>(['png', 'jpg', 'jpeg', 'webp'])
  const docFormats = ref<string[]>(['doc', 'docx', 'xls', 'xlsx', 'pdf'])
  const allowFormats = ref<string[]>([
    'png',
    'jpg',
    'jpeg',
    'webp',
    'doc',
    'docx',
    'xls',
    'xlsx',
    'pdf'
  ])

  const trToLower = (s: string): string => {
    if (s === null || s == '' || s == undefined) {
      return ''
    } else {
      s = s.toString()
      var letters: { [key: string]: string } = {
        İ: 'i',
        I: 'ı',
        Ş: 'ş',
        Ğ: 'ğ',
        Ü: 'ü',
        Ö: 'ö',
        Ç: 'ç'
      }
      s = s.replace(/(([İIŞĞÜÇÖ]))/g, function (letter: string) {
        return letters[letter]
      })
      return s.toLowerCase()
    }
  }

  const trToEngLower = (s: string): string => {
    if (s === null || s == '' || s == undefined) {
      return ''
    } else {
      s = s.toString()
      var letters: { [key: string]: string } = {
        İ: 'i',
        I: 'i',
        Ş: 's',
        Ğ: 'g',
        Ü: 'u',
        Ö: 'o',
        Ç: 'c',
        ı: 'i',
        ş: 's',
        ğ: 'g',
        ü: 'u',
        ö: 'o',
        ç: 'c'
      }
      s = s.replace(/(([İIŞĞÜÇÖışğüöç]))/g, function (letter: string) {
        return letters[letter]
      })
      return s.toLowerCase()
    }
  }

  const trToUpper = (s: string): string => {
    if (s === null || s == '' || s == undefined) {
      return ''
    } else {
      s = s.toString()
      var letters: { [key: string]: string } = {
        i: 'İ',
        ş: 'Ş',
        ğ: 'Ğ',
        ü: 'Ü',
        ö: 'Ö',
        ç: 'Ç',
        ı: 'I'
      }
      if (s === null || s == '') {
        return ''
      } else {
        s = s.replace(/(([iışğüçö]))/g, function (letter: string) {
          return letters[letter]
        })
        return s.toUpperCase()
      }
    }
  }

  const toTrLocale = (
    num: number | string,
    prec?: number | string,
    curr?: string | number
  ): string => {
    var val: string
    var opt: Record<string, any> = {}
    if (prec) {
      if (prec) opt.minimumFractionDigits = prec
      if (curr) {
        opt.style = 'currency'
        opt.currency = curr
      }
      val = new Intl.NumberFormat('tr-TR', opt).format(Number(num))
    } else {
      let d = num.toString().split('.')[1] ? num.toString().split('.')[1].length : 0
      opt.minimumFractionDigits = d
      val = new Intl.NumberFormat('tr-TR', opt).format(Number(num))
    }
    return String(val)
  }

  const floatEnLocale = (num: number | string): string => {
    return num.toString().replace('.', '').replace(',', '.')
  }

  const CreateSlug = (s: string, dlm?: string): string => {
    let slug = trToEngLower(s)
    let inDlm = dlm !== undefined ? dlm : '-'
    slug = slug.replace(/\W+/g, inDlm).toLowerCase()
    return slug
  }

  const FileType = (s: string): string => {
    return s.slice(s.lastIndexOf('.') + 1)
  }

  const FileName = (s: string): string => {
    if (s.includes('/')) return s.slice(s.lastIndexOf('/') + 1)
    else if (s.includes('\\')) return s.slice(s.lastIndexOf('\\') + 1)
    else return s.slice(s.lastIndexOf('/') + 1)
  }

  const IsImage = (s: string): boolean => {
    return imageFormats.value.includes(FileType(s).toLocaleLowerCase())
  }

  const TableCellDocument = (v: string) => {
    if (IsImage(v)) {
      return `<a href="${
        axios.defaults.baseURL + v
      }" target="_blank" onclick="event.stopPropagation()"><img class="table-cell-image" src="${
        axios.defaults.baseURL + v
      }" /></a>`
    } else {
      return `<a class="back-grad back-grad-grey" target="_blank" onclick="event.stopPropagation()" href="${
        axios.defaults.baseURL + v
      }">${FileName(v)}</a>`
    }
  }

  const ResetObject = (target: Record<string, any>, base: Record<string, any>) => {
    Object.keys(target).forEach((tk) => {
      Object.keys(base).forEach((bk) => {
        if (base[tk] === undefined) delete target[tk]
      })
    })
  }

  return {
    menuLoaded,
    animateTime,
    sideMenu,
    sideMenuWidth,
    screenWidth,
    mainPadding,
    notificationPanel,
    profileMenu,
    breakPoints,
    perPage,
    selUser,
    selAuthUser,
    selCustomer,
    selCustomerType,
    imageFormats,
    docFormats,
    allowFormats,
    toTrLocale,
    floatEnLocale,
    trToLower,
    trToEngLower,
    trToUpper,
    CreateSlug,
    FileType,
    FileName,
    IsImage,
    TableCellDocument,
    ResetObject
  }
})
