import { defineStore } from 'pinia'

export const useDateStore = defineStore('dateStore', {
  state: () => ({
    dayNames: ['Pazar', 'Pazartesi', 'Salı', 'Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi'],
    dayShortNames: ['Paz', 'Pzt', 'Sal', 'Çar', 'Per', 'Cum', 'Cts'],
    monthNames: [
      { name: 'Ocak', val: 0 },
      { name: 'Şubat', val: 1 },
      { name: 'Mart', val: 2 },
      { name: 'Nisan', val: 3 },
      { name: 'Mayıs', val: 4 },
      { name: 'Haziran', val: 5 },
      { name: 'Temmuz', val: 6 },
      { name: 'Ağustos', val: 7 },
      { name: 'Eylül', val: 8 },
      { name: 'Ekim', val: 9 },
      { name: 'Kasım', val: 10 },
      { name: 'Aralık', val: 11 }
    ]
  }),
  actions: {
    dateFormat({
      pattern = 'dd-mm-yy-ddd-tt',
      date,
      splitDate = '/',
      splitTime = ' '
    }: {
      pattern?: string
      date?: Date | string | null
      splitDate?: string
      splitTime?: string
    }): string {
      let formattedDate = ''
      let inDate = date !== null && date !== undefined ? new Date(date) : new Date()
      let patternA = pattern.split('-')

      for (let i = 0; i < patternA.length; i++) {
        if (patternA[i] == 'y') formattedDate += inDate.getFullYear().toString().slice(-2)
        if (patternA[i] == 'yy') formattedDate += inDate.getFullYear()
        if (patternA[i] == 'm') formattedDate += inDate.getMonth() + 1
        if (patternA[i] == 'mm')
          formattedDate += (inDate.getMonth() + 1).toString().padStart(2, '0')
        if (patternA[i] == 'mmm')
          formattedDate += this.monthNames[inDate.getMonth()].name.slice(0, 3)
        if (patternA[i] == 'mmmm')
          formattedDate += this.monthNames[inDate.getMonth()].name
        if (patternA[i] == 'd') formattedDate += inDate.getDate()
        if (patternA[i] == 'dd')
          formattedDate += inDate.getDate().toString().padStart(2, '0')
        if (patternA[i] == 'ddd' || patternA[i] == 'dddd') {
          if (i > 0) {
            let lc = formattedDate.slice(-1)
            if (lc == splitDate)
              formattedDate = formattedDate.slice(0, formattedDate.length - 1)
            formattedDate += ' '
          }
          if (patternA[i] == 'ddd') formattedDate += this.dayShortNames[inDate.getDay()]
          if (patternA[i] == 'dddd') formattedDate += this.dayNames[inDate.getDay()]
          if (i == 0) formattedDate += ' '
        }
        if (patternA[i] == 't' || patternA[i] == 'tt' || patternA[i] == 'ttt') {
          if (i > 0) {
            let lc = formattedDate.slice(-1)
            if (lc == splitDate)
              formattedDate = formattedDate.slice(0, formattedDate.length - 1)
            formattedDate += splitTime
          }
          if (patternA[i] == 't')
            formattedDate +=
              inDate.getHours().toString().padStart(2, '0') +
              ':' +
              inDate.getMinutes().toString().padStart(2, '0')
          if (patternA[i] == 'tt')
            formattedDate +=
              inDate.getHours().toString().padStart(2, '0') +
              ':' +
              inDate.getMinutes().toString().padStart(2, '0') +
              ':' +
              inDate.getSeconds().toString().padStart(2, '0')
          if (patternA[i] == 'ttt')
            formattedDate +=
              inDate.getHours().toString().padStart(2, '0') +
              ':' +
              inDate.getMinutes().toString().padStart(2, '0') +
              ':' +
              inDate.getSeconds().toString().padStart(2, '0') +
              '.' +
              inDate.getMilliseconds()
          if (i == 0) formattedDate += splitTime
        }
        if (
          i != patternA.length - 1 &&
          patternA[i] != 'ddd' &&
          patternA[i] != 'dddd' &&
          patternA[i] != 't' &&
          patternA[i] != 'tt' &&
          patternA[i] != 'ttt'
        )
          formattedDate += splitDate
      }
      return formattedDate
    }
  }
})
