<?php

namespace Daylight\Core\Livewire\Tables\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Livewire\Attributes\Url;

trait WithFiltering
{
    #[Url]
    public array $filters = [];

    public function updatedFilters(): void
    {
        $this->resetPage();
    }

    protected function applyFilters(Builder $query): Builder
    {
        foreach ($this->filters as $name => $value) {
            if ($value === '' || $value === null) {
                continue;
            }

            $query = $this->applyFilter($query, $name, $value);
        }

        return $query;
    }

    abstract protected function applyFilter(Builder $query, string $name, mixed $value): Builder;

    abstract protected function availableFilters(): array;
}
