import React, { createContext, useContext, useMemo } from 'react';
import { COLUMNS, resolveColumnIndices } from '../types';

type ColumnIndices = typeof COLUMNS;

const ColumnsContext = createContext<ColumnIndices>(COLUMNS);

export function ColumnsProvider({ headers, children }: { headers: string[], children: React.ReactNode }) {
  const resolved = useMemo(() => {
    if (!headers || headers.length === 0) return COLUMNS;
    return resolveColumnIndices(headers);
  }, [headers]);

  return (
    <ColumnsContext.Provider value={resolved}>
      {children}
    </ColumnsContext.Provider>
  );
}

export function useColumns() {
  return useContext(ColumnsContext);
}
