import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import {
  TextButtonUniDriver,
  TooltipUniDriver,
  PopoverMenuUniDriver,
  ButtonUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';
import { baseUniDriverFactory } from '../../unidriver';

export function MultiBulkActionToolbarUniDriver(
  base: UniDriver,
  body: UniDriver,
) {
  const button = (dataHook = 'bulk-action-button') =>
    ButtonUniDriver(base.$(`[data-hook="${dataHook}"]`), body);
  const popover = (dataHook?: string) =>
    PopoverMenuUniDriver(base.$(`[data-hook="${dataHook}"]`), body);
  const tooltip = (dataHook = 'bulk-action-button') =>
    TooltipUniDriver(base.$(`[data-hook="${dataHook}-tooltip"]`), body);
  const selectAll = () =>
    TextButtonUniDriver(base.$(`[data-hook="select-all"]`), body);

  return {
    ...baseUniDriverFactory(base),
    getButton: (dataHook: string) => button(dataHook),
    clickButton: (dataHook: string) => button(dataHook).click(),
    isButtonExists: (dataHook: string) => button(dataHook).exists(),
    isButtonDisabled: (dataHook: string) => button(dataHook).isButtonDisabled(),
    getButtonTextContent: (dataHook: string) =>
      button(dataHook).getButtonTextContent(),
    getPopover: (dataHook?: string) => popover(dataHook),
    isPopoverExists: (dataHook?: string) => popover(dataHook).exists(),
    openMoreActionPopover: () =>
      PopoverMenuUniDriver(base.$(`[data-hook="more-actions-popover"]`), body)
        .getTriggerElement('more-actions')
        .click(),
    getMoreActionsPopoverItemAt: (index: number) =>
      PopoverMenuUniDriver(
        base.$(`[data-hook="more-actions-popover"]`),
        body,
      ).itemContentAt(index),
    clickMoreActionsPopoverItemAt: (index: number) =>
      PopoverMenuUniDriver(
        base.$(`[data-hook="more-actions-popover"]`),
        body,
      ).clickAtChild(index),
    clickMoreActionsPopoverItem: (dataHook: string) =>
      PopoverMenuUniDriver(
        base.$(`[data-hook="more-actions-popover"]`),
        body,
      ).clickAtChildByDataHook(dataHook),
    getMoreActionsItemsCount: () =>
      PopoverMenuUniDriver(
        base.$(`[data-hook="more-actions-popover"]`),
        body,
      ).childrenCount(),
    isMoreActionsOpen: () =>
      PopoverMenuUniDriver(
        base.$(`[data-hook="more-actions-popover"]`),
        body,
      ).isMenuOpen(),
    selectAll: () => selectAll().click(),
    selectAllText: () => selectAll().getButtonTextContent(),
    isTooltipExist: (dataHook: string) => tooltip(dataHook).exists(),
    isTooltipVisible: (dataHook: string) => tooltip(dataHook).tooltipExists(),
    getTooltipText: (dataHook: string) => tooltip(dataHook).getTooltipText(),
    clickOutsideTooltip: (dataHook: string) => tooltip(dataHook).clickOutside(),
    mouseEnterTooltip: (dataHook: string) => tooltip(dataHook).mouseEnter(),
    mouseLeaveTooltip: (dataHook: string) => tooltip(dataHook).mouseLeave(),

    getSelectedCountText: () => base.$('[data-hook="selected-count"]').text(),
  };
}
