import { UniDriver, waitFor } from '@wix/wix-ui-test-utils/unidriver';
import { baseUniDriverFactory } from '../../unidriver';
import {
  ButtonUniDriver,
  CustomModalLayoutUniDriver,
  HeadingUniDriver,
  ModalUniDriver,
  RadioGroupUniDriver,
  TextUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';

export function ExportModalUniDriver(base: UniDriver, body: UniDriver) {
  const modalBase = () => body.$('.portal-export-modal');
  const modalLayoutBase = () => body.$('[data-hook="export-modal-layout"]');
  const footerBase = () =>
    modalLayoutBase().$('[data-hook="export-modal-footer-text"]');

  const linearProgressBarBase = () =>
    modalLayoutBase().$('[data-hook="export-linear-progress-bar"]');

  const getAbsoluteCounter = () =>
    TextUniDriver(
      linearProgressBarBase().$('[data-hook="progress-count"]'),
      body,
    );

  const getTotalCounter = () =>
    HeadingUniDriver(
      linearProgressBarBase().$('[data-hook="progress-total"]'),
      body,
    );

  const getExportRadioGroup = () =>
    RadioGroupUniDriver(
      modalLayoutBase().$('[data-hook="export-group"]'),
      body,
    );

  const getModal = () => ModalUniDriver(modalBase(), modalBase());

  const getModalLayout = () => ({
    ...CustomModalLayoutUniDriver(modalLayoutBase(), body),
    getPrimaryButtonDriver: () => ButtonUniDriver(
      modalLayoutBase().$('[data-hook="baseModalLayout-primary-button"]'),
      body,
    ),
  });

  const getFooterText = async () =>
    (await TextUniDriver(footerBase(), body)).getText();

  const footerExists = () => footerBase().exists();

  const getCsvMessageText = async () =>
    (
      await TextUniDriver(
        modalLayoutBase().$('[data-hook="export-csv-message"]'),
        body,
      ).getText()
    ).replace('&nbsp;', ' ');

  return {
    wait: (timeout?: number) => base.wait(timeout),
    ...baseUniDriverFactory(base),
    getModal,
    /** Get RadioGroupDriver of export types list */
    getExportRadioGroup,
    getModalLayout,
    getCsvMessageText,
    waitModalClosed: (timeout?: number) =>
      waitFor(async () => !(await getModalLayout().exists()), timeout),
    /** Clicks export modal primary action button*/
    clickPrimaryAction: async () =>
      (await getModalLayout().getPrimaryButtonDriver()).click(),
    /** Get export modal primary action text*/
    getPrimaryActionText: async () =>
      (await getModalLayout().getPrimaryButtonDriver()).getButtonTextContent(),
    /** Get export modal secondary action text */
    getSecondaryButtonText: async () =>
      (
        await getModalLayout().getSecondaryButtonDriver()
      ).getButtonTextContent(),
    /** Clicks export modal secondary action button*/
    clickSecondaryAction: async () =>
      (await getModalLayout().getSecondaryButtonDriver()).click(),
    /** Clicks export modal close button*/
    clickCloseButton: () => getModalLayout().clickCloseButton(),
    /** Get export modal title*/
    getModalTitle: () => getModalLayout().getTitleText(),
    /** Get export progress absolute counter*/
    getAbsoluteCounter: () => getAbsoluteCounter().getText(),
    /** Get export total counter*/
    getTotalCounter: () => getTotalCounter().getText(),
    /** Get text from error message without the space value that is added with the widows prop
    Tests are failing with this Hex valus, so it is replaced here */
    getErrorMessageText: async () =>
      (
        await modalLayoutBase().$('[data-hook="export-error-message"]').text()
      ).replace('\u00A0', ' '),
    getErrorMessageAction: async () =>
      modalLayoutBase().$('[data-hook="export-error-message-action"]'),
    /** Get export modal footer text*/
    getFooterText,
    /** Indicates whether footer exists in the export modal */
    footerExists,
  };
}

export default ExportModalUniDriver;
