"use strict";

exports.__esModule = true;
exports.paginationCoreDriverFactory = void 0;
var _constants = require("./constants");
var paginationCoreDriverFactory = _ref => {
  var {
    element,
    eventTrigger
  } = _ref;
  var pageStrip = element == null ? void 0 : element.querySelector('[data-hook="page-strip"]');
  var getNavButton = name => element == null ? void 0 : element.querySelector("[data-hook=".concat(name, "]"));
  var getPageByNumber = n => {
    var _pageStrip$firstEleme;
    return pageStrip == null || (_pageStrip$firstEleme = pageStrip.firstElementChild) == null ? void 0 : _pageStrip$firstEleme.querySelector("[data-hook~=page-".concat(n, "]"));
  };
  var getElement = dataHook => element == null ? void 0 : element.querySelector("[data-hook=\"".concat(dataHook, "\"]"));
  var getPageElements = () => {
    var _pageStrip$firstEleme2, _pageStrip$firstEleme3;
    return pageStrip != null && (_pageStrip$firstEleme2 = pageStrip.firstElementChild) != null && _pageStrip$firstEleme2.children ? Array.from(pageStrip == null || (_pageStrip$firstEleme3 = pageStrip.firstElementChild) == null ? void 0 : _pageStrip$firstEleme3.children) : [];
  };
  var inputElement = element == null ? void 0 : element.querySelector('[data-hook="page-input"]');
  return {
    /** Returns the root element*/
    root: element,
    /** Returns whether the element exists */
    exists: () => !!element,
    /** Simulates events */
    simulate: eventTrigger,
    /** Returns the container of page elements */
    getPageStrip: () => pageStrip,
    /** Returns displayed page elements */
    getPages: getPageElements,
    /** Returns the text content of the displayed pages in "pages" mode */
    getPageLabels: () => getPageElements().map(p => p.textContent),
    /** Returns the page element given its index in the page strip */
    getPageByIndex: idx => getPageElements()[idx || 0] || null,
    /** Returns the page element given page number */
    getPageByNumber,
    /** Returns the page element currently selected */
    getCurrentPage: () => element == null ? void 0 : element.querySelector('[data-hook~="current-page"]'),
    /** Returns the element for the navigation button - acceptable values are 'first', 'last', 'previous' or 'next' */
    getNavButton,
    /** Returns the page input element in "input" mode */
    getPageInput: inputElement,
    /** Returns the total amount of pages displayed in "input" mode */
    getTotalPagesField: () => element == null ? void 0 : element.querySelector('[data-hook="total-pages"]'),
    /** Simulates clicking a nav button */
    clickNavButton: name => {
      var navButton = getNavButton(name);
      if (navButton) {
        eventTrigger.click(navButton);
      }
    },
    /** Simulates clicking a page in "pages" mode */
    clickPage: page => {
      var pageButton = getPageByNumber(page);
      if (pageButton) {
        eventTrigger.click(pageButton);
      }
    },
    click: () => element && eventTrigger.click(element),
    /** Simulates changing the value of the input field in "input" mode */
    changeInput: newValue => {
      if (inputElement) {
        inputElement.value = newValue;
        eventTrigger.change(inputElement);
      }
    },
    /** Simulates committing the input field value in "input" mode */
    commitInput: () => eventTrigger.keyDown(inputElement, {
      keyCode: 13,
      key: 'Enter'
    }),
    /** Simulates blur in the input field in "input" mode */
    blurInput: () => eventTrigger.blur(inputElement),
    getElement,
    getPage: pageNumber => element == null ? void 0 : element.querySelector("[data-hook~=\"".concat(_constants.DataHooks.page, "-").concat(pageNumber, "\"]")),
    focusInput: () => eventTrigger.focus(inputElement),
    getElementAttribute: (dataHook, attributeName) => {
      var _getElement;
      return (_getElement = getElement(dataHook)) == null ? void 0 : _getElement.getAttribute(attributeName);
    }
  };
};
exports.paginationCoreDriverFactory = paginationCoreDriverFactory;
//# sourceMappingURL=PaginationCore.driver.js.map