import type {
  CellContext,
  HeaderContext,
  RowData,
} from '@tanstack/table-core';
import type { Snippet } from 'svelte';

export function renderHeader<TData extends RowData, TValue>(
  ctx: HeaderContext<TData, TValue>,
  render: ((ctx: HeaderContext<TData, TValue>) => unknown) | undefined,
  snippet?: Snippet<[{ props: Record<string, unknown> }]>
): unknown {
  if (snippet) return snippet;
  if (typeof render === 'function') return render(ctx);
  return render;
}

export function renderCells<TData extends RowData, TValue>(
  ctx: CellContext<TData, TValue>,
  render: ((ctx: CellContext<TData, TValue>) => unknown) | undefined,
  snippet?: Snippet<[{ props: Record<string, unknown> }]>
): unknown {
  if (snippet) return snippet;
  if (typeof render === 'function') return render(ctx);
  return render;
}