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

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

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

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

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

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

  return {
    ...baseUniDriverFactory(base),
    exists: () => renameViewModalDriver().exists(),
    getTitle: () => renameViewModalDriver().getTitleText(),
    getSubTitle: () => renameViewModalDriver().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: () => renameViewModalDriver().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'),
  };
};
