import React, { useMemo } from 'react';
import { CollectionState, FiltersMap, KeyedItem } from '@wix/bex-core';
import { CollectionTableActionsColumn } from '../CollectionTableActionsColumn';
import { ActionCell } from './ActionCell';

export interface UseRenderActionCellColumnParams<T, F extends FiltersMap> {
  sticky?: boolean;
  actionCell?: ActionCell<T, F>;
  collection: CollectionState<T, F>;
}

export function useRenderActionCellColumn<T, F extends FiltersMap>(
  params: UseRenderActionCellColumnParams<T, F>,
) {
  const { collection, actionCell, sticky } = params;

  return useMemo(() => {
    if (actionCell == null) {
      return null;
    }

    return (keyedItem: KeyedItem<T>, index: number) => {
      return (
        <CollectionTableActionsColumn
          index={index}
          sticky={sticky}
          collection={collection}
          keyedItem={keyedItem}
          actionCell={actionCell}
        />
      );
    };
  }, [sticky, actionCell]);
}
