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

import PageErrorStateUniDriver from '../PageErrorState/PageErrorState.uni.driver';
import FormPageCardUniDriver from '../FormPageCard/FormPageCard.uni.driver';

export const FormPageContentUniDriver = (base: UniDriver, body: UniDriver) => {
  const moreActionsPopoverDriver = (dataHook: string) =>
    PopoverMenuUniDriver(base.$(`[data-hook="${dataHook}"]`), body);

  const getCard = (dataHook: string) =>
    FormPageCardUniDriver(base.$(`[data-hook="${dataHook}"]`), body);

  return {
    /** Returns Cancel button driver */
    cancelButton: () =>
      ButtonUniDriver(
        base.$$(`[data-hook="form-page-cancel-btn"]`).get(0),
        body,
      ),
    /** Returns Save button driver */
    saveButton: () =>
      ButtonUniDriver(base.$$(`[data-hook="form-page-save-btn"]`).get(0), body),

    getMoreActions: (dataHook: string) => ({
      exists: () => moreActionsPopoverDriver(dataHook).exists(),

      /** Opens the manage view popover */
      open: () =>
        moreActionsPopoverDriver(dataHook)
          .getTriggerElement('more-actions-popover-button')
          .click(),
      /** Indicates whether the manage view popover is open */
      isOpen: () => moreActionsPopoverDriver(dataHook).isMenuOpen(),

      /** Clicks at child by data-hook */
      clickAtChildByDataHook: (childDataHook: string) =>
        moreActionsPopoverDriver(dataHook).clickAtChildByDataHook(
          childDataHook,
        ),

      /** Clicks at child by index */
      clickAtChildByIndex: (index: number) =>
        moreActionsPopoverDriver(dataHook).clickAtChild(index),

      /** Gets content at index */
      childContentAt: (index: number) =>
        moreActionsPopoverDriver(dataHook).itemContentAt(index),

      /** Gets children count */
      childrenCount: () => moreActionsPopoverDriver(dataHook).childrenCount(),
    }),

    /** Returns EntityPage.Card driver by dataHook */
    getCard,

    /** Returns error state driver */
    getErrorState: () => PageErrorStateUniDriver(base, body),

    ...baseUniDriverFactory(base),
  };
};
