"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionDropdownFilterUniDriver = void 0;
var _unidriver = require("@wix/design-system/dist/testkit/unidriver");
var _unidriver2 = require("@wix/wix-ui-test-utils/unidriver");
var _shuffle = _interopRequireDefault(require("lodash/shuffle"));
const CollectionDropdownFilterUniDriver = (optionsDriver, dataHook, base, body) => {
  const waitForOptions = async (total = 1, timeout = 3000) => {
    await (0, _unidriver2.waitFor)(async () => (await optionsDriver.dropdownLayoutDriver.optionsLength()) >= total, timeout);
    return optionsDriver.dropdownLayoutDriver.options();
  };
  const getDropdownLayoutBase = async () => {
    const contentDataHook = await base.attr('data-content-hook');
    if (contentDataHook) {
      return body.$(`[data-content-element="${contentDataHook}"]`);
    }
    const baseDataHook = await base.attr('data-hook');
    return body.$(`[data-hook="${baseDataHook}-content"]`);
  };
  const getOptions = async ids => {
    return ids.map(async id => {
      return optionsDriver.dropdownLayoutDriver.optionById(id);
    });
  };
  const getOptionsCount = () => {
    return optionsDriver.dropdownLayoutDriver.optionsLength();
  };
  const getAvailableOptionsIds = async () => {
    const options = await optionsDriver.dropdownLayoutDriver.options();
    const maybeIds = await Promise.all(options.map(async option => {
      const hook = await option.element().attr('data-hook');
      return hook == null ? void 0 : hook.replace(/^dropdown-item-/, '');
    }));
    return maybeIds.filter(value => value != null);
  };
  const getInputPlaceholder = () => {
    return optionsDriver.inputDriver.getPlaceholder();
  };
  const isNoSearchResults = async () => (await getDropdownLayoutBase()).$('[data-hook="empty-state"]').exists();
  const isErrorStateExists = async () => (await getDropdownLayoutBase()).$('[data-hook="filter-error-state"]').exists();
  const getEmptyStateText = async () => (0, _unidriver.TextUniDriver)((await getDropdownLayoutBase()).$('[data-hook="empty-state-text"]'), body).getText();
  const initialLoaderExists = async () => (await getDropdownLayoutBase()).$('[data-hook="initial-loader"]').exists();
  const infiniteScrollLoaderExists = async () => (await getDropdownLayoutBase()).$('[data-hook="dropdownLayout-loader"]').exists();
  const clickOptions = async ids => {
    for (const id of ids) {
      await optionsDriver.dropdownLayoutDriver.clickAtOptionByDataHook(`dropdown-item-${id}`);
    }
    return ids;
  };
  const existInFilter = async dataHook => (await getDropdownLayoutBase()).$(`[data-hook="${dataHook}"]`).exists();
  const selectRandomOptions = async (count, options = {}) => {
    await optionsDriver.inputDriver.click();
    const ids = await getRandomOptions(count);
    return selectOptions(ids, options);
  };
  const close = () => optionsDriver.dropdownLayoutDriver.mouseClickOutside();
  const clickInputClear = () => optionsDriver.inputDriver.clickClear();
  const getRandomOptions = async count => {
    await waitForOptions(count);
    const options = await optionsDriver.dropdownLayoutDriver.options();
    const maybeIds = await Promise.all(options.map(async option => {
      const dataHook = await option.element().attr('data-hook');
      return dataHook == null ? void 0 : dataHook.replace(/^dropdown-item-/, '');
    }));
    const ids = maybeIds.filter(value => value != null);
    return (0, _shuffle.default)(ids).slice(0, count);
  };
  const selectOptions = async (ids, {
    closeDropdown = true
  } = {}) => {
    await optionsDriver.inputDriver.click();
    await waitForOptions();
    const clickedIds = await clickOptions(ids);
    if (closeDropdown) {
      await close();
    }
    return clickedIds;
  };
  const selectOption = async id => {
    await optionsDriver.inputDriver.click();
    await waitForOptions();
    const clickedId = await clickOptions([id]);
    return clickedId[0] || [];
  };
  const hasDivider = async () => {
    return (await getAvailableOptionsIds()).some(id => id === 'promoted-section-divider');
  };
  return {
    ...optionsDriver,
    waitForOptions,
    getOptions,
    clickOptions,
    close,
    clickInputClear,
    selectOptions,
    selectOption,
    selectRandomOptions,
    getRandomOptions,
    getOptionsCount,
    getAvailableOptionsIds,
    getInputPlaceholder,
    isNoSearchResults,
    getEmptyStateText,
    isErrorStateExists,
    initialLoaderExists,
    infiniteScrollLoaderExists,
    hasDivider,
    existInFilter,
    _getDropdownLayoutBase: getDropdownLayoutBase,
    scrollDown: async () => {
      const element = await base.getNative();
      const {
        page
      } = element;
      const contentHook = await base.attr('data-content-hook');
      const baseDataHook = await base.attr('data-hook');
      const contentSelector = contentHook ? `[data-content-element="${contentHook}"]` : `[data-hook="${baseDataHook}-content"]`;

      // puppeteer
      if (page != null && page.evaluate) {
        await page.evaluate((_dataHook, _contentSelector) => {
          const e = document.querySelector(`[data-hook="${_dataHook}"] ${_contentSelector}`);
          e.scrollBy({
            behavior: 'smooth',
            top: e.scrollHeight
          });
        }, dataHook, contentSelector);
      }
      // jsdom
      else {
        await (await base.$(contentSelector).getNative()).dispatchEvent(new Event('scroll'));
      }
    }
  };
};
exports.CollectionDropdownFilterUniDriver = CollectionDropdownFilterUniDriver;
//# sourceMappingURL=CollectionDropdownFilter.uni.driver.js.map