import type { SortItem } from '@/@core/types'

export const useDataTableStore = defineStore({
  id: 'dataTable',
  state: () => ({
    filters: {} as Record<string, string>,
    currentPage: 1,
    itemsPerPage: 10,
    searchText: '',
    sortBy: '',
    sortOrder: false,
  }),
  actions: {
    clearState() {
      this.filters = {} as Record<string, string>
      this.currentPage = 1
      this.itemsPerPage = 10
      this.searchText = ''
      this.sortBy = ''
      this.sortOrder = false
    },
    setFilters(filterName: string, value: any) {
      this.filters[filterName] = value
    },
    setCurrentPage(page: number) {
      this.currentPage = page
    },
    setItemsPerPage(value: number) {
      this.itemsPerPage = value
    },
    setSearchText(value: string) {
      this.searchText = value
    },
    setSortBy(value: string) {
      this.sortBy = value
    },
    setSortOrder(value: string) {
      if (value === 'asc')
        this.sortOrder = true
      else
        this.sortOrder = false
    },
    getSortBy(): SortItem[] {
      return [{
        key: this.sortBy,
        order: this.sortOrder ? 'asc' : 'desc',
      }]
    },
  },
},
)
