<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 { Webhook } from '@/pages/admin/extension/data/schema'

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

const props = defineProps<DataTableToolbarProps>()

const emit = defineEmits<{
  'update:searchFilter': [value: string]
}>()

const { t } = useI18n()

interface DataTableToolbarProps {
  table: Table<Webhook>
  searchFilter?: string
}

const isFiltered = computed(() => !!props.searchFilter)

function resetFilters() {
  emit('update:searchFilter', '')
}
</script>

<template>
  <div class="flex items-center flex-1 space-x-2">
    <Input
      :placeholder="t('admin.extension.webhooks.searchWebhook')"
      :model-value="searchFilter"
      class="h-8 w-[150px] lg:w-[250px]"
      @update:model-value="emit('update:searchFilter', $event as string)"
    />

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