import { useEffect } from 'react';
import { TableFoldersState } from '../../state';
import { FiltersMap } from '@wix/bex-core';
import {
  getBulkAssignStatus,
  getDisabledTooltipContent,
} from './TagsBulkAssignUtils';

interface UseTagsBulkAssignFoldersUtilsProps<
  T1,
  F1 extends FiltersMap,
  T2,
  F2 extends FiltersMap,
> {
  state: TableFoldersState<T1, F1, T2, F2>;
}

export function useTagsBulkAssignFoldersUtils<
  T1,
  F1 extends FiltersMap,
  T2,
  F2 extends FiltersMap,
>({ state }: UseTagsBulkAssignFoldersUtilsProps<T1, F1, T2, F2>) {
  const { tagsBulkAssignButtonState } =
    state.toolbar.tagsCollectionState?.tagsBulkAssignPopoverState ?? {};

  const { hasReachedMaxSelectedItems, isDuringApplyChanges } =
    getBulkAssignStatus({
      state: state.toolbar,
    });

  useEffect(() => {
    if (tagsBulkAssignButtonState) {
      const tooltipContent = getDisabledTooltipContent({
        state: state.toolbar,
        hasSelectedFolders: state.collections.hasSelectedFolders,
      });
      tagsBulkAssignButtonState.setTooltipContent(tooltipContent);
    }
  }, [
    hasReachedMaxSelectedItems,
    isDuringApplyChanges,
    state.collections.hasSelectedFolders,
  ]);
}
