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

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

/**
 * Atom: column pinning. Single responsibility — owns a `ColumnPinningState`
 * signal (`{ left: string[]; right: string[] }`). Used by `useDataTable` to
 * power TanStack Table's column pinning feature, which guarantees that
 * pinned columns form a contiguous group on the chosen edge and exposes
 * pre-calculated cumulative offsets via `column.getStart('left')` /
 * `column.getAfter('right')`.
 */
export function useTableColumnPinning(
  initial: ColumnPinningState = { left: [], right: [] },
): TableColumnPinningAtom {
  const state = signal(initial);
  return {
    state: state.asReadonly(),
    set: (s: ColumnPinningState) => state.set(s),
  };
}
