import {
  TextButtonUniDriver,
  TextUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';
import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';

export const SummaryBarUniDriver = async (base: UniDriver, body: UniDriver) => {
  const summaryBarBase = await base.get('[data-hook="summary-bar"]');

  const isSuccessState = async () =>
    (await summaryBarBase.get('[data-hook="summary-bar-content"]')).exists();

  const isLoadingState = async () =>
    (await summaryBarBase.get('[data-hook="skeleton-summary-bar"]')).exists();

  const isErrorState = async () =>
    (await summaryBarBase.get('[data-hook="error-summary-bar"]')).exists();

  const getRetryButton = async () =>
    TextButtonUniDriver(
      await summaryBarBase.get('[data-hook="summary-bar-retry-button"]'),
      body,
    );

  const getErrorText = async () =>
    TextUniDriver(
      await summaryBarBase.get('[data-hook="summary-bar-error-text"]'),
      body,
    );

  const getItemTextAt = async (index: number) =>
    (await summaryBarBase.getAll('[data-hook="summary-bar-item"]'))
      .at(index)
      .text();

  return {
    ...summaryBarBase,
    /** Is SummaryBar in success state */
    isSuccessState,
    /** Is SummaryBar in loading state */
    isLoadingState,
    /** Is SummaryBar in error state */
    isErrorState,
    /**  Gets retry button text */
    getRetryButton,
    /**  Gets error text */
    getErrorText,
    getItemTextAt,
  };
};

export default SummaryBarUniDriver;
