'use client';

import { useMemo, useState } from 'react';

export type SortDir = 'asc' | 'desc';

/**
 * Ordenamiento client-side de una tabla.
 *
 * Uso:
 *   const { sortedData, sortKey, sortDir, toggleSort, getSortIndicator } = useTableSort(
 *     data,
 *     {
 *       fecha: (row) => new Date(row.fechaEmision).getTime(),
 *       total: (row) => Number(row.totalMxn || 0),
 *     },
 *     'fecha',
 *   );
 */
export function useTableSort<T, K extends string>(
  data: T[] | undefined,
  accessors: Record<K, (row: T) => number | string>,
  initialKey: K,
  initialDir: SortDir = 'desc',
) {
  const [sortKey, setSortKey] = useState<K>(initialKey);
  const [sortDir, setSortDir] = useState<SortDir>(initialDir);

  const sortedData = useMemo(() => {
    if (!data) return data;
    const accessor = accessors[sortKey];
    const sign = sortDir === 'asc' ? 1 : -1;
    return [...data].sort((a, b) => {
      const va = accessor(a);
      const vb = accessor(b);
      if (typeof va === 'number' && typeof vb === 'number') {
        return (va - vb) * sign;
      }
      return String(va).localeCompare(String(vb)) * sign;
    });
  }, [data, sortKey, sortDir, accessors]);

  const toggleSort = (key: K) => {
    if (sortKey === key) {
      setSortDir(sortDir === 'asc' ? 'desc' : 'asc');
    } else {
      setSortKey(key);
      setSortDir('desc');
    }
  };

  /** Retorna 'asc' | 'desc' | null según el header sea activo o no */
  const getSortIndicator = (key: K): SortDir | null => (sortKey === key ? sortDir : null);

  return { sortedData, sortKey, sortDir, toggleSort, getSortIndicator };
}
