"use strict";

exports.__esModule = true;
exports.GridUniDriver = GridUniDriver;
exports.default = void 0;
var _unidriver = require("@wix/wix-ui-test-utils/unidriver");
var _unidriver2 = require("@wix/design-system/dist/testkit/unidriver");
var _CollectionToolbarUni = require("../CollectionToolbar/CollectionToolbar.uni.driver");
var _DragAndDropUni = require("../DragAndDrop/DragAndDrop.uni.driver");
var _CollectionEmptyStateUni = require("../EmptyState/CollectionEmptyState.uni.driver");
var _ToolbarCollectionErrorStateUni = require("../ErrorState/ToolbarCollectionErrorState.uni.driver");
var _ExportButtonUni = require("../ExportButton/ExportButton.uni.driver");
var _ExportModalUni = require("../ExportButton/ExportModal.uni.driver");
var _SkeletonGridUni = require("./SkeletonGrid.uni.driver");
var _unidriver3 = require("../../unidriver");
var _SummaryBarUni = require("../SummaryBar/SummaryBar.uni.driver");
var _CollectionPremiumEmptyStateUni = require("../EmptyState/CollectionPremiumEmptyState.uni.driver");
var _CollectionNoResultsStateUni = require("../EmptyState/CollectionNoResultsState.uni.driver");
var _CollectionSectionHeaderUni = require("../CollectionSectionHeader/CollectionSectionHeader.uni.driver");
function GridUniDriver(base, body) {
  const initialLoaderBase = () => base.$(`[data-hook="grid-initial-loader"]`);
  const initialLoader = () => (0, _SkeletonGridUni.SkeletonGridUniDriver)(initialLoaderBase(), body);
  const infiniteScrollLoaderBase = () => base.$(`[data-hook="infinite-scroll-loader"]`);
  const infiniteScrollLoader = () => (0, _SkeletonGridUni.SkeletonGridUniDriver)(infiniteScrollLoaderBase(), body);
  const slidingModalBase = () => body.$('[data-hook="cairo-side-panel-modal"]');
  const getEmptyStateByDataHook = (dataHook = 'collection-empty-state') => (0, _CollectionEmptyStateUni.CollectionEmptyStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body);
  const getPremiumEmptyStateByDataHook = dataHook => (0, _CollectionPremiumEmptyStateUni.CollectionPremiumEmptyStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body);
  const getNoResultsStateByDataHook = (dataHook = 'collection-empty-state') => (0, _CollectionNoResultsStateUni.CollectionNoResultsStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body);
  const viewsToolbarItem = () => base.$('[data-hook="collection-table-toolbar-views-item"]');
  const getCardGalleryItemByIndexBase = index => {
    return base.$(`[data-hook="virtual-grid-item"][data-index="index-${index}"]`);
  };
  const getCardGalleryItemFooterImageByIndexBase = index => {
    return base.$(`[data-hook="virtual-grid-item"][data-index="index-${index}"] [data-hook="footer-image"]`);
  };
  const getCardDragElementByIndexBase = index => {
    return getCardGalleryItemByIndexBase(index).$('[data-hook="grid-card-item-events"]');
  };
  const getCardGalleryItemByIndex = index => {
    const itemDriver = (0, _unidriver2.CardGalleryItemUniDriver)(getCardGalleryItemByIndexBase(index), body);
    return {
      ...itemDriver,
      isPrimaryActionExists: () => base.$('[data-hook="primary-action"]').exists()
    };
  };
  const getAddItem = () => (0, _unidriver2.AddItemUniDriver)(base.$(`[data-hook="grid-add-item-container"]`), body);
  const getCardGalleryItemPopoverMenuByIndex = async index => {
    // Popover isn't active until we specifically call it
    await (await getCardGalleryItemByIndex(index)).getSettingsMenu();
    const popoverBase = base.$$(`[data-hook="popover"]`).get(index);
    const popoverMenu = (0, _unidriver2.PopoverMenuUniDriver)(popoverBase, body);
    const trigger = popoverBase.$$('[data-hook="popover-icon-button-trigger"]').get(0);
    const iconButton = (0, _unidriver2.IconButtonUniDriver)(trigger, base);
    return {
      ...popoverMenu,
      clickTriggerElement: () => iconButton.click()
    };
  };
  const getActionModal = () => (0, _unidriver2.MessageModalLayoutUniDriver)(body.$('[data-hook="collection-action-modal"]'), body);
  const getToolbar = () => (0, _CollectionToolbarUni.CollectionToolbarUniDriver)(base.$('[data-hook="collection-toolbar"]'), body);
  const scrollDown = async (scrollableElementDataHook = 'page-scrollable-content') => {
    const {
      page
    } = await body.$(`[data-hook="${scrollableElementDataHook}"]`).getNative();
    // puppeteer
    if (page != null && page.evaluate) {
      await page.evaluate(_dataHook => {
        const scrollableElement = document.querySelector(`[data-hook="${_dataHook}"]`);
        scrollableElement.scrollBy({
          behavior: 'smooth',
          top: scrollableElement.scrollHeight
        });
      }, scrollableElementDataHook);
      await page.waitForTimeout(1000); // delay scroll resolve by 1 sec
    }
    // jsdom
    else {
      const pageScrollContent = body.$(`[data-hook="${scrollableElementDataHook}"]`);
      if (await pageScrollContent.exists()) {
        await (await pageScrollContent.getNative()).dispatchEvent(new Event('scroll'));
      }
    }
  };
  const getDragAndDrop = () => (0, _DragAndDropUni.DragAndDropUniDriver)(base);
  const getErrorStateByDataHook = (dataHook = 'placeholder-states-error-state') => (0, _ToolbarCollectionErrorStateUni.ToolbarCollectionErrorStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body);
  const refreshLoaderBase = () => base.$(`[data-hook="grid-refresh-loader"]`);
  const refreshLoader = () => (0, _SkeletonGridUni.SkeletonGridUniDriver)(refreshLoaderBase(), body);
  const getNavigationModal = () => (0, _unidriver2.MessageModalLayoutUniDriver)(body.$('[data-hook="navigation-modal"]'), body);
  const getExportModal = () => (0, _ExportModalUni.ExportModalUniDriver)(base, body);
  const getExportButton = () => (0, _ExportButtonUni.ExportButtonUniDriver)(base.$('[data-hook="export-button"]'), body);
  const getRenderedItemIndexAt = async i => {
    const card = base.$$('[data-hook="virtual-grid-item"]').filter(e => e.$('[data-hook="grid-card-item"]').exists()).get(i);
    const dataIndexAttr = await card.attr('data-index');
    const index = Number(dataIndexAttr == null ? void 0 : dataIndexAttr.replace(/^index-/, ''));
    return Number.isInteger(index) ? index : -1;
  };
  const getLastRenderedItemIndex = async () => {
    const cards = base.$$('[data-hook="virtual-grid-item"]').filter(e => e.$('[data-hook="grid-card-item"]').exists());
    const lastCard = cards.get((await cards.count()) - 1);
    const dataIndexAttr = await lastCard.attr('data-index');
    const index = Number(dataIndexAttr == null ? void 0 : dataIndexAttr.replace(/^index-/, ''));
    return Number.isInteger(index) ? index : null;
  };
  const scrollDownToItem = (index, {
    timeout = 15000,
    delay = 1000,
    scrollableElementDataHook = 'page-scrollable-content',
    scrollIntoView
  } = {}) => {
    return (0, _unidriver.waitFor)(async () => {
      if (await getCardGalleryItemByIndexBase(index).exists()) {
        if (scrollIntoView) {
          var _getCardGalleryItemBy, _getCardGalleryItemBy2;
          await ((_getCardGalleryItemBy = (_getCardGalleryItemBy2 = getCardGalleryItemByIndexBase(index)).scrollIntoView) == null ? void 0 : _getCardGalleryItemBy.call(_getCardGalleryItemBy2));
        }
        return true;
      }
      await scrollDown(scrollableElementDataHook);
      return getCardGalleryItemByIndexBase(index).exists();
    }, timeout, delay);
  };
  const scrollDownToAddItem = ({
    timeout = 5000,
    delay = 1000,
    scrollableElementDataHook = 'page-scrollable-content'
  } = {}) => {
    return (0, _unidriver.waitFor)(async () => {
      if (await getAddItem().exists()) {
        return true;
      }
      await scrollDown(scrollableElementDataHook);
      return getAddItem().exists();
    }, timeout, delay);
  };
  const _getInnerElement = () => base.$('[data-hook="virtual-inner-element"]');
  const getLiveRegion = () => (0, _unidriver2.LiveRegionUniDriver)(base.$('[data-hook="collection-live-region"]'), body);
  const getSummaryBar = () => (0, _SummaryBarUni.SummaryBarUniDriver)(base, body);
  return {
    ...(0, _unidriver3.baseUniDriverFactory)(base),
    /**
     * Wait until root element to exist
     * @param timeout - time to wait until bailing
     */
    wait: timeout => base.wait(timeout),
    /** Wait for the initial fetch loader to be shown */
    waitInitialLoader: timeout => initialLoaderBase().wait(timeout),
    /** Wait for the initial fetch loader to be removed */
    waitInitialLoaderRemoved: timeout => (0, _unidriver.waitFor)(async () => !(await initialLoaderBase().exists()), timeout),
    /** Indicates whether initial loader is in loading status */
    isInitialLoaderLoading: () => initialLoader().exists(),
    /** Indicates whether initial loader exists */
    initialLoaderExists: () => initialLoader().exists(),
    /** Wait for the infinite scroll loader to be shown */
    waitInfiniteScrollLoader: timeout => infiniteScrollLoaderBase().wait(timeout),
    infiniteScrollSkeletonCount: () => infiniteScrollLoader().skeletonItemsCount(),
    /** Indicates whether infinite scroll loader exists */
    infiniteScrollLoaderExists: () => infiniteScrollLoader().exists(),
    /** Indicates whether table refresh loader exists */
    refreshLoaderExists: () => refreshLoader().exists(),
    refreshLoaderSkeletonCount: () => refreshLoader().skeletonItemsCount(),
    getErrorStateByDataHook,
    /** Get initial error state before table renders */
    getCardErrorStateByDataHook: (dataHook = 'placeholder-states-error-state') => getErrorStateByDataHook(dataHook),
    /** Get collection empty state within the table that by a specific data hook */
    getEmptyStateByDataHook,
    /** Get premium empty state within the table that by a specific data hook */
    getPremiumEmptyStateByDataHook,
    /** Get collection no results state within the table that by a specific data hook */
    getNoResultsStateByDataHook,
    /** Get the number of actually rendered grid cards */
    getRenderedItemsCount: () => base.$$('[data-hook="grid-card-item"]').count(),
    gridContainerExists: () => _getInnerElement().exists(),
    getVirtualizedRowsCount: async () => {
      const str = await _getInnerElement().attr('data-count');
      return !isNaN(Number(str)) ? Number(str) : 0;
    },
    initialLoaderSkeletonCount: () => infiniteScrollLoader().skeletonItemsCount(),
    /** Get the number rendered grid cards available on the client */
    getLastRenderedItemIndex,
    getLastRenderedItem: async () => {
      const i = await getLastRenderedItemIndex();
      return i != null ? getCardGalleryItemByIndex(i) : null;
    },
    /** Get the CardGalleryItem by index */
    getCardGalleryItemByIndex,
    /** Wait CardGalleryItem by index */
    waitCardGalleryItemByIndex: async (index, timeout) => (await getCardGalleryItemByIndexBase(index)).wait(timeout),
    /** Get the CardGalleryItem's PopoverMenu by index */
    getCardGalleryItemPopoverMenuByIndex,
    getCardDefaultEmptyState: getEmptyStateByDataHook,
    getActionModal,
    /** Get the `<AddItem />` driver for the `renderAddItem` */
    getAddItem,
    getToolbar,
    viewsToolbarItemLabelExists: () => viewsToolbarItem().$('span').exists(),
    getViewsToolbarItemLabelText: () => viewsToolbarItem().$('span').text(),
    /** Get tabs filter driver */
    getToolbarTabs: () => getToolbar().getTabs(),
    /** Enter text into the search input */
    enterSearchText: text => getToolbar().enterSearchText(text),
    clearSearchText: () => getToolbar().clearSearchText(),
    getEmptyStateClearFiltersButton: emptyStateDataHook => (0, _unidriver2.TextButtonUniDriver)(base.$(`[data-hook="${emptyStateDataHook}"]`).$('[data-hook="empty-state-clear-filters-button"]'), body),
    /**
     * Triggers "scroll" event on the scroll element.
     * @param scrollableElementDataHook - The data hook of the scrollable element (Optional, defaults to "page-scrollable-content").
     */
    scrollDown,
    /**
     * Triggers "scroll" event on the scroll element, until item exists
     * @param index
     * @param timeout - how much to to wait for item exists
     * @param delay - how much to to wait between each check
     * @param scrollableElementDataHook
     */
    scrollDownToItem,
    scrollDownToAddItem,
    scrollToTop: async (scrollableElementDataHook = 'page-scrollable-content') => {
      const pageScrollContent = body.$(`[data-hook="${scrollableElementDataHook}"]`);
      if (await pageScrollContent.exists()) {
        await (await pageScrollContent.getNative()).dispatchEvent(Object.assign(new Event('scroll'), {
          _testScroll: 'top'
        }));
      }
    },
    /** @private */
    _getInnerElement,
    getDragHandle: index => {
      return getCardGalleryItemByIndexBase(index).$('[data-hook="drag-handle-button"]');
    },
    _getDragAndDropDriver: getDragAndDrop,
    escapeDrag: async sourceIndex => getDragAndDrop().escapeDrag((await getCardGalleryItemByIndexBase(sourceIndex)).$(`[data-hook="drag-handle-button"]`)),
    dragStart: async (sourceIndex, options = {}) => getDragAndDrop().dragStart(await getCardDragElementByIndexBase(sourceIndex), options),
    dragStartFromHandleAndKeyboard: async sourceIndex => getDragAndDrop().startKeyboardDrag((await getCardGalleryItemByIndexBase(sourceIndex)).$(`[data-hook="drag-handle-button"]`)),
    dragStartFromHandle: async sourceIndex => getDragAndDrop().dragStart((await getCardGalleryItemByIndexBase(sourceIndex)).$(`[data-hook="drag-handle-button"]`)),
    dragMove: () => getDragAndDrop().dragMove(),
    dragEnd: async targetIndex => getDragAndDrop().dragEnd(await getCardDragElementByIndexBase(targetIndex)),
    /**
     * Drags an element over another element.
     * @param {number} sourceIndex
     * @param {number} targetIndex
     * @param [options]
     * @param {number} [options.steps] number of movement steps (only for puppeteer)
     */
    dragOver: async (sourceIndex, targetIndex, options = {}) => {
      return getDragAndDrop().dragOver(await getCardDragElementByIndexBase(sourceIndex), await getCardDragElementByIndexBase(targetIndex), options);
    },
    getNavigationModal,
    getLiveRegionText: async () => (await getLiveRegion()).getMessage(),
    getLiveRegionRole: async () => (await getLiveRegion()).getRole(),
    getDragAndDropLiveRegionText: () => body.$('[data-hook="grid-dnd-live-region"]').text(),
    _getCardDragElementByIndexBase: getCardDragElementByIndexBase,
    _getCardGalleryItemByIndexBase: getCardGalleryItemByIndexBase,
    waitSidePanelFullyOpened: () => {
      return (0, _unidriver.waitFor)(async () => (await slidingModalBase().attr('data-transition-status')) === 'entered');
    },
    /** Get an export button driver **/
    getExportButton,
    /** Get an export modal driver **/
    getExportModal,
    /** Is 'ExportTo' modal open */
    isExportModalOpen: () => getExportButton().getModal().isOpen(),
    getCardGalleryItemFooterImageByIndexBase,
    getRenderedItemIndexAt,
    getDragOverlayCardGalleryItem: () => (0, _unidriver2.CardGalleryItemUniDriver)(body.$('[data-hook="draggable-card-overlay"]'), body),
    internalScrollDownToItem: (index, options = {}) => scrollDownToItem(index, {
      ...options,
      scrollableElementDataHook: 'toolbar-collection-content-auto-sizer'
    }),
    /**
     * Gets collection summary bar
     */
    getSummaryBar,
    getSectionHeaderAt: index => (0, _CollectionSectionHeaderUni.CollectionSectionHeaderUniDriver)(base.$$('[data-hook="grid-section-header"]').get(index), body),
    getSectionHeadersCount: () => base.$$('[data-hook="grid-section-header"]').count(),
    getSectionGridAt: index => GridUniDriver(base.$$('[data-hook="grid-section-and-repeater"]').get(index), body)
  };
}
var _default = exports.default = GridUniDriver;
//# sourceMappingURL=Grid.uni.driver.js.map