import type { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import {
  CustomModalLayoutUniDriver,
  FormFieldUniDriver,
  InputUniDriver,
  TextButtonUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';
import { baseUniDriverFactory } from '../../../../unidriver';

export const CollectionSaveViewModalUniDriver = (
  base: UniDriver,
  body: UniDriver,
) => {
  const getModalBase = () => body.$('[data-hook="save-view-modal"]');

  const saveViewModalDriver = () =>
    CustomModalLayoutUniDriver(getModalBase(), body);

  const viewNameFormFiledDriver = () =>
    FormFieldUniDriver(
      body.$('[data-hook="save-view-modal-form-field"]'),
      body,
    );

  const viewNameInputDriver = () =>
    InputUniDriver(body.$('[data-hook="save-view-modal-input"]'), body);

  const getSaveButton = () => {
    return saveViewModalDriver().getPrimaryButtonDriver();
  };
  const getCancelButton = () => {
    return saveViewModalDriver().getSecondaryButtonDriver();
  };

  const getLearnMoreButton = () =>
    TextButtonUniDriver(
      body.$('[data-hook="save-view-modal-learn-more-link"]'),
      body,
    );

  return {
    ...baseUniDriverFactory(base),
    exists: () => saveViewModalDriver().exists(),
    getTitle: () => saveViewModalDriver().getTitleText(),
    getSubTitle: () => saveViewModalDriver().getSubtitleText(),
    getSaveButtonText: async () => {
      const saveButton = await getSaveButton();
      return saveButton.getButtonTextContent();
    },
    isSaveButtonDisabled: async () => {
      const saveButton = await getSaveButton();
      return saveButton.isButtonDisabled();
    },
    clickSaveButton: async () => {
      const saveButton = await getSaveButton();
      await saveButton.click();
    },
    getCancelButtonText: async () => {
      const cancelButton = await getCancelButton();
      return cancelButton.getButtonTextContent();
    },
    clickCancelButton: async () => {
      const cancelButton = await getCancelButton();
      await cancelButton.click();
    },
    clickCloseButton: () => saveViewModalDriver().clickCloseButton(),
    viewNameFormFieldExists: () => viewNameFormFiledDriver().exists(),
    getViewNameInputValue: () => viewNameInputDriver().getValue(),
    getViewNameFormFieldTitle: () => viewNameFormFiledDriver().getLabel(),
    getViewNameInputPlaceholder: () => viewNameInputDriver().getPlaceholder(),
    fillViewNameInput: (value: string) =>
      viewNameInputDriver().enterText(value),
    getViewNameInputStatusMessage: () =>
      viewNameInputDriver().getStatusMessage(),
    getViewNameHasErrorStatus: () => viewNameInputDriver().hasStatus('error'),
    getLearnMoreButton,
  };
};
