import { ToolbarCollectionState } from '../../state';
import { FiltersMap } from '@wix/bex-core';

export function getBulkAssignStatus<T, F extends FiltersMap>({
  state,
}: {
  state: ToolbarCollectionState<T, F>;
}) {
  const canUseBulkSync =
    state.tagsCollectionState?.tagsBulkAssignPopoverState.bulkAssignSyncState
      .canUseBulkSync;
  const canBulkAssignAsync =
    state.tagsCollectionState?.tagsBulkAssignPopoverState.bulkAssignAsyncState
      .canBulkAssignAsync;
  const hasReachedMaxSelectedItems = !canUseBulkSync && !canBulkAssignAsync;

  const isDuringApplyChanges =
    state.tagsCollectionState?.tagsBulkAssignPopoverState.bulkAssignAsyncState
      .isDuringApplyChanges;

  return { hasReachedMaxSelectedItems, isDuringApplyChanges };
}

export function getDisabledTooltipContent<T, F extends FiltersMap>({
  state,
  hasSelectedFolders,
}: {
  state: ToolbarCollectionState<T, F>;
  hasSelectedFolders?: boolean;
}) {
  const { hasReachedMaxSelectedItems, isDuringApplyChanges } =
    getBulkAssignStatus({ state });

  const assignButtonConfig =
    state?.tagsCollectionState?.tagsBulkAssignPopoverState.assignButtonConfig;

  const assignButtomTooltip = assignButtonConfig?.isDisabled
    ? assignButtonConfig.tooltipContent
    : null;

  switch (true) {
    case isDuringApplyChanges:
      return state.container.translate(
        'cairo.tags.popover.bulkItemUpdate.tooltip',
      );
    case hasSelectedFolders:
      return state.container.translate(
        'cairo.tags.popover.assignFolder.tooltip',
      );
    case hasReachedMaxSelectedItems:
      return state.container.translate(
        'cairo.tags.popover.bulkItemLimit.tooltip',
      );
    case assignButtonConfig?.isDisabled:
      return assignButtomTooltip;
    default:
      return null;
  }
}
