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';
import { useWixPatternsContainer } from '@wix/bex-core/react';

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 { isPanelLayout } = useWixPatternsContainer();

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

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

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

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

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