import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import { TableUniDriver } from '../Table/Table.uni.driver';
import { GridUniDriver } from '../Grid/Grid.uni.driver';
import { CollectionToolbarUniDriver } from '../CollectionToolbar/CollectionToolbar.uni.driver';
import { baseUniDriverFactory } from '../../unidriver';
import { SummaryBarUniDriver } from '../SummaryBar/SummaryBar.uni.driver';

export function TableGridSwitchUniDriver(base: UniDriver, body: UniDriver) {
  const getToolbar = () =>
    CollectionToolbarUniDriver(
      base.$('[data-hook="collection-toolbar"]'),
      body,
    );

  const getTable = () => TableUniDriver(base, body);
  const getGrid = () => GridUniDriver(base, body);
  const getSummaryBar = () => SummaryBarUniDriver(base, body);

  return {
    ...baseUniDriverFactory(base),
    /**
     * Wait until root element to exist
     * @param timeout - time to wait until bailing
     */
    wait: (timeout?: number) => base.wait(timeout),
    /** Get layout switch button */
    getSwitchButton: () => getToolbar().getSwitchButton(),
    /** Get layout switch button divider */
    getSwitchButtonDivider: () => getToolbar().getSwitchButtonDivider(),
    /** Get Table driver */
    getTable,
    /** Get Grid driver */
    getGrid,
    isShowingGrid: () => body.$(`[data-hook="layout-view-grid"]`).exists(),
    isShowingTable: () => body.$(`[data-hook="layout-view-table"]`).exists(),
    /**
     * Gets collection summary bar
     */
    getSummaryBar,
  };
}

export default TableGridSwitchUniDriver;
