"use strict";

exports.__esModule = true;
exports.PickerUniDriver = PickerUniDriver;
var _unidriver = require("@wix/wix-ui-test-utils/unidriver");
var _unidriver2 = require("../../unidriver");
var _unidriver3 = require("@wix/design-system/dist/testkit/unidriver");
var _PickerTableListItemUni = require("../PickerTableListItem/PickerTableListItem.uni.driver");
function PickerUniDriver(base, body) {
  const selectorList = (0, _unidriver3.SelectorListUniDriver)(base, body);
  const getEmptyState = async factory => factory(await base.getNative());
  const getSelectors = () => base.$('[data-hook="selector-list-content"]').$$('[data-hook="selector-list-selector"]');
  const selectorDriverAt = i => (0, _unidriver3.SelectorUniDriver)(getSelectors().get(i), body);
  const tableListItemDriverAt = i => (0, _PickerTableListItemUni.PickerTableListItemUniDriver)(getSelectors().get(i), body);
  const getSearchLoader = () => base.$('[data-hook="search-loader"]');
  const getFloatingNotification = () => (0, _unidriver3.FloatingNotificationUniDriver)(base.$('[data-hook="floating-notification"]'), body);
  const getPrimaryActionButtonAt = i => getSelectors().get(i).$('[data-hook="primary-action-button"]');
  return {
    ...(0, _unidriver2.baseUniDriverFactory)(base),
    wait: timeout => base.wait(timeout),
    waitSearchLoader: timeout => getSearchLoader().wait(timeout),
    searchLoaderExists: () => getSearchLoader().exists(),
    waitSearchLoaderRemoved: timeout => (0, _unidriver.waitFor)(async () => !(await getSearchLoader().exists()), timeout),
    numberOfItemsInList: () => getSelectors().count(),
    primaryButtonExistsAt: i => getPrimaryActionButtonAt(i).exists(),
    getPrimaryActionButtonAt,
    scrollDown: async () => {
      const element = await base.getNative();

      // puppeteer
      if ((0, _unidriver2.isSled2)(element)) {
        const {
          page
        } = element;
        await page.evaluate(() => {
          const e = document.querySelector('[data-hook="selector-list-content"]');
          e.scrollBy({
            behavior: 'smooth',
            top: e.scrollHeight
          });
        });
      }
      // playwright — run the same browser-side scroll via the locator's page
      else if ((0, _unidriver2.isSled3)(element)) {
        const locator = element;
        await locator.page().evaluate(() => {
          const e = document.querySelector('[data-hook="selector-list-content"]');
          e.scrollBy({
            behavior: 'smooth',
            top: e.scrollHeight
          });
        });
      }
      // jsdom
      else {
        await selectorList.scrollDown();
      }
    },
    scrollTo: i => {
      var _getSelectors$get$scr, _getSelectors$get;
      return (_getSelectors$get$scr = (_getSelectors$get = getSelectors().get(i)).scrollIntoView) == null ? void 0 : _getSelectors$get$scr.call(_getSelectors$get);
    },
    toggleSelectorAt: selectorList.toggleSelectorAt,
    getTotalSelected: async () => {
      const checked = await Promise.all(await getSelectors().map(selector => (0, _unidriver3.SelectorUniDriver)(selector, body).isChecked()));
      return checked.filter(Boolean).length;
    },
    isSelectorCheckedAt: selectorList.isSelectorCheckedAt,
    isListItemDisabledAt: i => selectorDriverAt(i).isDisabled(),
    waitListItemAt: async (i, timeout) => (0, _unidriver.waitFor)(() => selectorDriverAt(i).exists(), timeout),
    getTableListItemAt: i => tableListItemDriverAt(i),
    waitRowEntered: (i, timeout) => base.$('[data-hook="selector-list-content"]').$$('[data-hook="picker-list-fade-item"].fade-entered').get(i).wait(timeout),
    hoverItemAt: i => getSelectors().get(i).hover(),
    getTitleTextAt: async i => selectorDriverAt(i).titleTextDriver().getText(),
    isIndeterminateAt: async i => selectorDriverAt(i).isIndeterminate(),
    getTitleTextContentAt: async i => getSelectors().get(i).$('[data-hook="selector-title"]').text(),
    getSubtitleTextAt: async i => selectorDriverAt(i).subtitleTextDriver().getText(),
    getSubtitleTextContentAt: async i => getSelectors().get(i).$('[data-hook="selector-subtitle"]').text(),
    getExtraNodeElementAt: i => selectorDriverAt(i).getExtraNode(),
    getImageElementAt: i => selectorDriverAt(i).getImage(),
    getToggleTooltipTextAt: i => selectorDriverAt(i).getTooltipText(),
    floatingNotificationExists: () => getFloatingNotification().exists(),
    getFloatingNotificationText: () => getFloatingNotification().getText(),
    getEmptyState,
    getEmptyStateByDataHook: (dataHook = 'picker-empty-state') => (0, _unidriver3.EmptyStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body),
    getErrorStateByDataHook: dataHook => (0, _unidriver3.EmptyStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body)
  };
}
//# sourceMappingURL=Picker.uni.driver.js.map