import { signal, type Signal } from '@angular/core';
import type { SortingState } from '@tanstack/angular-table';

export interface TableSortAtom {
  readonly state: Signal<SortingState>;
  set(s: SortingState): void;
}

/**
 * Atom: sort state. Single responsibility — owns a `SortingState` signal
 * compatible with TanStack Table.
 */
export function useTableSort(initialState: SortingState = []): TableSortAtom {
  const state = signal(initialState);
  return {
    state: state.asReadonly(),
    set: (s: SortingState) => state.set(s),
  };
}
