"use strict";

exports.__esModule = true;
exports.FiltersPanelUniDriver = void 0;
var _SlidingModalUni = require("../SlidingModal/SlidingModal.uni.driver");
var _unidriver = require("@wix/design-system/dist/testkit/unidriver");
var _CollectionDropdownFilterUni = require("../DropdownFilter/CollectionDropdownFilter.uni.driver");
var _MultiInlineCheckboxUni = require("../MultiInlineCheckbox/MultiInlineCheckbox.uni.driver");
var _DateRangeFilterUni = require("../DateRangePicker/DateRangeFilter.uni.driver");
var _RadioGroupFilterUni = require("../RadioGroupFilter/RadioGroupFilter.uni.driver");
const FiltersPanelUniDriver = (base, body) => {
  const slidingModal = (0, _SlidingModalUni.SlidingModalUniDriver)(base.$('[data-hook="cairo-side-panel-modal"]'), body);
  const sidePanel = (0, _unidriver.SidePanelUniDriver)(base.$('[data-hook="filters-side-panel"]'), body);
  const accordionBase = base.$('[data-hook="filters-panel-accordion"]');
  const accordion = (0, _unidriver.AccordionUniDriver)(accordionBase, body);
  const getInlineCheckboxFilter = filterName => (0, _MultiInlineCheckboxUni.MultiInlineCheckboxUniDriver)(accordionBase.$(`[data-hook="collection-filter-${filterName}"]`), body);
  const getDateRangeFilter = filterName => (0, _DateRangeFilterUni.DateRangePickerUniDriver)(accordionBase.$(`[data-hook="date-range-filter-${filterName}"]`), body);
  const getRadioGroupFilter = filterName => (0, _RadioGroupFilterUni.RadioGroupFilterUniDriver)(accordionBase.$(`[data-hook="radio-group-filter-${filterName}"]`), body);
  const getAccordionItemTitle = filterName => accordionBase.$(`[data-hook="accordion-item-title-${filterName}"]`);
  const getAccordionItem = async filterName => {
    const items = accordionBase.$$('[data-hook="accordion-item"]');
    const count = await items.count();
    for (let i = 0; i < count; i++) {
      const item = items.get(i);
      if (await item.$(`[data-hook="accordion-item-title-${filterName}"]`).exists()) {
        return item;
      }
    }
    throw new Error(`Accordion item for filter: "${filterName}" was not found`);
  };
  const getAccordionItemAt = index => accordionBase.$$('[data-hook="accordion-item"]').get(index);
  const isFilterExpanded = async filterName => (await getAccordionItem(filterName)).$('[data-hook="children"]').exists();
  const isFilterExpandedAt = index => getAccordionItemAt(index).$('[data-hook="children"]').exists();
  const clickFilter = async filterName => {
    if (!(await isFilterExpanded(filterName))) {
      await clickFilterTitle(filterName);
    }
  };
  const checkFilterInlineCheckboxes = async (filterName, ids, {
    closePanel = true,
    closeAccordionItem = true
  } = {}) => {
    await clickFilter(filterName);
    await getInlineCheckboxFilter(filterName).checkOptions(ids);
    if (closeAccordionItem) {
      await clickFilterTitle(filterName);
    }
    if (closePanel) {
      await sidePanel.clickClose();
    }
  };
  const chooseDatesFilterDateRange = async (filterName, {
    dates = undefined,
    closePanel = true,
    closeAccordionItem = true
  } = {}) => {
    await clickFilter(filterName);
    const dateRange = await getDateRangeFilter(filterName);
    let datesResult;
    if (dates && dates.length > 1) {
      const from = await dateRange.chooseDate('from', dates[0]);
      const to = await dateRange.chooseDate('to', dates[1]);
      datesResult = {
        from,
        to
      };
    } else {
      datesResult = await dateRange.chooseRandomDates();
    }
    if (closeAccordionItem) {
      await clickFilterTitle(filterName);
    }
    if (closePanel) {
      await sidePanel.clickClose();
    }
    return datesResult;
  };
  const getInputWithOptionsDriver = filterName => {
    const dataHook = `collection-filter-${filterName}`;
    const optionsBase = accordionBase.$(`[data-hook="collection-filter-${filterName}"]`);
    const inputWithOptionsDriver = (0, _unidriver.InputWithOptionsUniDriver)(optionsBase, body);
    return (0, _CollectionDropdownFilterUni.CollectionDropdownFilterUniDriver)(inputWithOptionsDriver, dataHook, optionsBase, body);
  };
  const selectFilterOptions = async (filterName, ids, {
    closePanel = true,
    closeAccordionItem = true,
    closeDropdown = true
  } = {}) => {
    if (!(await isFilterExpanded(filterName))) {
      await clickFilterTitle(filterName);
    }
    await getInputWithOptionsDriver(filterName).selectOptions(ids, {
      closeDropdown
    });
    if (closeAccordionItem) {
      await clickFilterTitle(filterName);
    }
    if (closePanel) {
      await sidePanel.clickClose();
    }
  };
  const selectFilterRandomOptions = async (filterName, count, {
    closePanel = true,
    closeAccordionItem = true,
    closeDropdown = true
  } = {}) => {
    if (!(await isFilterExpanded(filterName))) {
      await clickFilterTitle(filterName);
    }
    const ids = await getInputWithOptionsDriver(filterName).selectRandomOptions(count, {
      closeDropdown
    });
    if (closeAccordionItem) {
      await getAccordionItemTitle(filterName).click();
    }
    if (closePanel) {
      await sidePanel.clickClose();
    }
    return ids;
  };
  const getFilterTitleText = filterName => (0, _unidriver.TextUniDriver)(getAccordionItemTitle(filterName), body).getText();
  const getFilterAppliedText = () => (0, _unidriver.TextUniDriver)(body.$('[data-hook="applied-filters"]'), body).getText();
  const clickFilterTitle = filterName => getAccordionItemTitle(filterName).click();
  const getCustomFilter = async (filterName, fn) => {
    if (!(await isFilterExpanded(filterName))) {
      await clickFilterTitle(filterName);
    }
    return fn(await accordion.element());
  };
  const clickClearAllFilters = () => getClearAllFiltersButton().click();
  const getClearAllFiltersButton = () => (0, _unidriver.TextButtonUniDriver)(base.$('[data-hook="clear-all-filters"]'), body);
  const getPanelFiltersCount = () => {
    const items = accordionBase.$$('[data-hook="accordion-item"]');
    return items.count();
  };
  const getItemTextAt = index => {
    return accordionBase.$$(':scope > div').get(index).text();
  };
  const getItemsCount = () => {
    return accordionBase.$$(':scope > div').count();
  };
  const filterSearchBase = base.$('[data-hook="filters-panel-search"]');
  const filterSearch = () => (0, _unidriver.SearchUniDriver)(filterSearchBase, body);
  const noResultsBase = base.$('[data-hook="filters-panel-search-no-results"]');
  const noResultsTitle = base.$('[data-hook="filters-panel-search-no-results-title"]');
  const clearSearchButton = () => (0, _unidriver.TextButtonUniDriver)(base.$('[data-hook="filters-panel-search-clear-button"]'), body);
  return {
    isOpen: slidingModal.isOpen,
    clickCloseButton: async () => {
      if (await sidePanel.isCloseButtonExists()) {
        return sidePanel.clickClose();
      }
      if (await sidePanel.isBackButtonExists()) {
        return sidePanel.clickBack();
      }
    },
    getTitleText: sidePanel.getTitleText,
    selectFilterOptions,
    selectFilterRandomOptions,
    checkFilterInlineCheckboxes,
    chooseDatesFilterDateRange,
    getFilterTitleText,
    getFilterAppliedText,
    clickFilterTitle,
    isFilterExpanded,
    isFilterExpandedAt,
    getCustomFilter,
    getInputWithOptionsDriver,
    clickClearAllFilters,
    getClearAllFiltersButton,
    getPanelFiltersCount,
    getDateRangeFilter,
    getNumberRangeDriver: filterName => {
      const getNumberRangeFilter = () => accordionBase.$(`[data-hook="number-range-filter-${filterName}"]`);
      const getNumberRangeFilterFromInput = () => (0, _unidriver.InputUniDriver)(getNumberRangeFilter().$('[data-hook="number-range-filter-from-input"]'), body);
      const getNumberRangeFilterToInput = () => (0, _unidriver.InputUniDriver)(getNumberRangeFilter().$('[data-hook="number-range-filter-to-input"]'), body);
      const getNumberRangeFilterFromError = () => {
        const fieldsetDriver = (0, _unidriver.FieldSetUniDriver)(getNumberRangeFilter(), body);
        return {
          exists: () => fieldsetDriver.hasStatus('error'),
          getText: () => fieldsetDriver.getStatusMessage()
        };
      };
      const getNumberRangeFilterToError = () => {
        const formField = (0, _unidriver.FormFieldUniDriver)(getNumberRangeFilter().$('[data-hook="number-range-filter-to-form-field"]'), body);
        return {
          exists: () => formField.hasStatus('error'),
          getText: () => formField.getStatusMessage()
        };
      };
      return {
        getNumberRangeFilter,
        getNumberRangeFilterFromInput,
        getNumberRangeFilterToInput,
        getNumberRangeFilterFromError,
        getNumberRangeFilterToError
      };
    },
    getRadioGroupFilter,
    getOperatorPickerDriver: filterName => {
      const getOperatorPicker = () => accordionBase.$(`[data-hook="operator-picker-${filterName}"]`);
      const getOperatorDropdown = () => (0, _unidriver.DropdownBaseUniDriver)(getOperatorPicker().$('[data-hook="operator-picker-operator-dropdown"]'), body);
      const getValueInput = () => (0, _unidriver.InputUniDriver)(getOperatorPicker().$('[data-hook="operator-picker-value-input"]'), body);
      return {
        exists: () => getOperatorPicker().exists(),
        getOperatorPicker,
        getOperatorDropdown,
        getValueInput,
        selectOperator: async optionIndex => {
          const dropdown = getOperatorDropdown();
          await dropdown.clickTargetElement('operator-picker-trigger');
          await dropdown.selectOption(optionIndex);
        },
        getSelectedOperator: async () => {
          const dropdown = getOperatorDropdown();
          return dropdown.getSelectedOptionId();
        },
        enterValue: async value => {
          const input = getValueInput();
          await input.enterText(value);
        },
        getValue: () => getValueInput().getText(),
        blur: () => getValueInput().blur()
      };
    },
    getItemTextAt,
    getItemsCount,
    applyButton: () => (0, _unidriver.ButtonUniDriver)(base.$('[data-hook="apply-button"]'), body),
    discardChangesModal: () => (0, _unidriver.MessageModalLayoutUniDriver)(body.$('[data-hook="discard-changes-modal"]'), body),
    getFiltersPanelMultiSelect: filterName => (0, _unidriver.MultiSelectUniDriver)(accordionBase.$(`[data-hook="collection-filter-${filterName}"]`), base),
    _getAccordionItemTitle: (filterName, {
      index = 0
    } = {}) => accordionBase.$$(`[data-hook="accordion-item-title-${filterName}"]`).get(index),
    _getInlineCheckboxFilter: getInlineCheckboxFilter,
    isFilterSearchVisible: () => filterSearchBase.exists(),
    enterFilterSearch: term => filterSearch().inputDriver.enterText(term),
    clearFilterSearchInput: () => filterSearch().inputDriver.clearText(),
    getFilterSearchValue: () => filterSearch().inputDriver.getText(),
    isNoFiltersMatchVisible: () => noResultsBase.exists(),
    getNoFiltersMatchText: () => (0, _unidriver.TextUniDriver)(noResultsTitle, body).getText(),
    clickClearSearchButton: () => clearSearchButton().click()
  };
};
exports.FiltersPanelUniDriver = FiltersPanelUniDriver;
//# sourceMappingURL=FiltersPanel.uni.driver.js.map