<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 { CloudVariable } from '@/pages/admin/cloud-variables/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<CloudVariable>
}

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

<template>
  <div class="flex items-center flex-1 space-x-2">
    <Input
      :placeholder="t('admin.cloudVariables.searchPlaceholder')"
      :model-value="(table.getColumn('key')?.getFilterValue() as string) ?? ''"
      class="h-8 w-[150px] lg:w-[250px]"
      @input="table.getColumn('key')?.setFilterValue($event.target.value)"
    />

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