<script setup lang="ts">
import type { Table } from '@tanstack/vue-table'

import { X } from 'lucide-vue-next'
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'

import type { User } from '@/pages/admin/users/data/schema'

import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'

const props = defineProps<DataTableToolbarProps>()

const { t } = useI18n()

interface DataTableToolbarProps {
  table: Table<User>
}

const isFiltered = computed(() => props.table.getState().columnFilters.length > 0)

const globalFilter = computed({
  get: () => props.table.getState().globalFilter as string ?? '',
  set: (value: string) => props.table.setGlobalFilter(value),
})
</script>

<template>
  <div class="flex items-center flex-1 space-x-2">
    <Input
      :placeholder="t('admin.users.searchPlaceholder')"
      :model-value="globalFilter"
      class="h-8 w-[200px] lg:w-[300px]"
      @input="globalFilter = $event.target.value"
    />

    <Button
      v-if="isFiltered"
      variant="ghost"
      class="h-8 px-2 lg:px-3"
      @click="table.resetColumnFilters()"
    >
      {{ t('admin.users.reset') }}
      <X class="size-4 ml-2" />
    </Button>
  </div>
</template>
