'use client';

import type { FlatRow, TreeItemId } from '../../types';

/** Pick the id one row below the current focused row (clamped at the end). */
export function nextRowId<T>(rows: readonly FlatRow<T>[], idx: number): TreeItemId | null {
  if (rows.length === 0) return null;
  const next = rows[Math.min(idx + 1, rows.length - 1)] ?? rows[0];
  return next.node.id;
}

/** Pick the id one row above the current focused row (clamped at the start). */
export function prevRowId<T>(rows: readonly FlatRow<T>[], idx: number): TreeItemId | null {
  if (rows.length === 0) return null;
  const prev = rows[Math.max(idx - 1, 0)] ?? rows[0];
  return prev.node.id;
}

/** First / last visible row id, or null if the list is empty. */
export function edgeRowId<T>(
  rows: readonly FlatRow<T>[],
  edge: 'first' | 'last',
): TreeItemId | null {
  if (rows.length === 0) return null;
  return edge === 'first' ? rows[0].node.id : rows[rows.length - 1].node.id;
}
