import { useMemo } from 'react';
import { CollectionState, FiltersMap } from '@wix/bex-core';
import { TableColumn as TableColumnWSR } from '@wix/design-system/dist/types/Table';
import { ActionCell } from './ActionCell';
import { useRenderActionCellColumn } from './useRenderActionCellColumn';
import { useActionCellColumnBase } from './useActionCellColumnBase';

export interface UseActionCellColumnParams<T, F extends FiltersMap> {
  actionCellWidth?: string | number;
  actionCellProps?: Partial<Omit<TableColumnWSR<T>, 'render' | 'style'>>;
  sticky?: boolean;
  actionCell?: ActionCell<T, F>;
  collection: CollectionState<T, F>;
}

export function useActionCellColumn<T, F extends FiltersMap>(
  params: UseActionCellColumnParams<T, F>,
) {
  const { collection, actionCell, actionCellProps, actionCellWidth, sticky } =
    params;

  const baseColumn = useActionCellColumnBase({
    actionCellProps,
    actionCellWidth,
  });

  const render = useRenderActionCellColumn({
    actionCell,
    sticky,
    collection,
  });

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

    return {
      ...baseColumn,
      render,
    };
  }, [baseColumn, render]);
}
