import { signal, computed } from '@angular/core';
import { ListParams, SortDir } from './paging';

export class TableDataSource {
  readonly page = signal(1);
  readonly perPage = signal(10);
  readonly search = signal('');
  readonly sortKey = signal<string | undefined>(undefined);
  readonly sortDir = signal<SortDir>(''); // '', 'asc', 'desc'

  readonly params = computed<ListParams>(() => ({
    page: this.page(),
    size: this.perPage(),
    search: this.search() || undefined,
    sortKey: this.sortKey(),
    sortDir: this.sortDir(),
  }));

  resetToFirst() {
    this.page.set(1);
  }

  setSort(key?: string, dir: SortDir = '') {
    this.sortKey.set(key);
    this.sortDir.set(dir);
    this.resetToFirst();
  }
}
