import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import { baseUniDriverFactory } from '../../unidriver';
import {
  BadgeUniDriver,
  PageHeaderUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';
import { FormPageContentUniDriver } from '../FormPageContent/FormPageContent.uni.driver';

export const EntityPageUniDriver = (base: UniDriver, body: UniDriver) => {
  const formPageContentUniDriver = FormPageContentUniDriver(base, body);
  /** Check if the EntityPage.Card shows a skeleton header */
  const cardShowsSkeletonHeader = (dataHook: string) =>
    formPageContentUniDriver.getCard(dataHook).showsSkeletonHeader();

  /** Returns EntityPage.Slots driver by dataHook */
  const getSlots = (dataHook: string) => ({
    showsSkeletonHeader: () => cardShowsSkeletonHeader(dataHook),
    getChildByDataHook: (childDataHook: string) =>
      base.$(`[data-hook="${childDataHook}"]`),
    ...baseUniDriverFactory(base.$(`[data-hook="${dataHook}"]`)),
  });

  const getBadgeAt = (index: number) => {
    return BadgeUniDriver(
      base.$(`[data-hook="form-page-header-badge-${index}"]`),
      body,
    );
  };

  return {
    /** Returns EntityPage.Extensions driver by dataHook */
    getExtensions: getSlots,

    /** Returns EntityPage.Slots driver by dataHook */
    getSlots,

    /** Returns page header driver */
    getPageHeader: () => {
      const pageHeader = PageHeaderUniDriver(
        base.$(`[data-hook="entity-page-header"]`),
        body,
      );
      return {
        ...pageHeader,
        getBadgeAt,
      };
    },

    /** Returns badge driver at index */
    getBadgeAt,

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