"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));
var _values = _interopRequireDefault(require("../../utils/operators/values"));
var arbitraryEmptyElement = () => document.createElement('div');
var dataTableDriverFactory = _ref => {
  var {
    element
  } = _ref;
  // Node is a DOM API which does not exist in jsdom. Using a constant instead: https://developer.mozilla.org/en-US/docs/Web/API/Node
  var COMMENT_NODE = 8;
  // When a React component renders null in React 15, a Comment-Element is rendered to the DOM.
  var _isDisplayingNothing = !element || element.nodeType === COMMENT_NODE;
  /* Since a Comment-Element has no methods like querySelector(),
   * we replace (if needed) it with an arbitrary Element.
   * This allows simple implementation of methods like getRows().
   */
  var protectedElement = _isDisplayingNothing ? arbitraryEmptyElement() : element;
  var getHeader = () => protectedElement.querySelector('thead');
  var getHeaderRow = () => getHeader().querySelector('tr');
  var hasHeader = () => !!getHeader();
  var _isHeaderVisible = () => {
    var header = getHeader();
    return !!(header && header.style.display !== 'none');
  };
  var getRows = () => protectedElement.querySelectorAll('tbody tr[data-table-row="dataTableRow"]');
  var getRowsCount = () => getRows().length;
  var getRow = rowIndex => getRows()[rowIndex];
  var getCell = (rowIndex, cellIndex) => getRow(rowIndex).querySelectorAll('td')[cellIndex];
  var _getRowDetails = index => protectedElement.querySelector("tbody tr td[data-hook=\"".concat(index, "_details\"]"));
  var getHeaderCell = index => getHeader().querySelectorAll('th')[index];
  var getHeaderCellByDataHook = dataHook => getHeader().querySelector("th[data-hook=\"".concat(dataHook, "\"]"));
  var getSortableTitle = index => protectedElement.querySelector("th [data-hook=\"".concat(index, "_title\"]"));
  var getTitleInfoIcon = index => protectedElement.querySelector("th [data-hook=\"".concat(index, "_info_tooltip\"]"));
  var getSortableTitleArrowDesc = index => element.querySelector("th [data-hook=\"".concat(index, "_title\"]  [data-hook=\"sort_arrow_dec\"]"));
  return {
    exists: () => !_isDisplayingNothing,
    getRow,
    getRowsCount,
    getRowsWithClassCount: className => {
      return _isDisplayingNothing ? 0 : (0, _values.default)(getRows()).filter(elem => elem.classList.contains(className)).length;
    },
    getRowsWithDataHook: dataHookName => protectedElement.querySelectorAll("[data-hook=\"".concat(dataHookName, "\"]")),
    getRowWithDataHook: dataHookName => protectedElement.querySelector("[data-hook=\"".concat(dataHookName, "\"]")),
    /** Returns an array representing the text content of the cells in a given row `index`.  */
    getRowText: index => (0, _values.default)(getRow(index).querySelectorAll('td')).map(td => td.textContent),
    getRowClasses: index => (0, _values.default)(getRow(index).classList),
    getHeaderRowClasses: () => (0, _values.default)(getHeaderRow().classList),
    getHeaderClasses: () => (0, _values.default)(getHeader().classList),
    /** Get header cell element: (columnIndex) => Element */
    getHeaderCell,
    getHeaderCellByDataHook,
    getHeaderCellStyle: index => getHeaderCell(index).style,
    getHeaderCellWidth: index => getHeaderCell(index).style.width,
    /** Get cell element: (rowIndex, columnIndex) => Element */
    getCell,
    getCellStyle: (rowIndex, colIndex) => getCell(rowIndex, colIndex).style,
    getCellWidth: (rowIndex, colIndex) => getCell(rowIndex, colIndex).width,
    isRowClickable: index => getRow(index).hasAttribute('data-clickable'),
    isRowAnimated: index => getRow(index).hasAttribute('data-animated'),
    isRowHighlighted: index => getRow(index).hasAttribute('data-highlighted'),
    getTitles: () => (0, _values.default)(getHeader().querySelectorAll('th')).map(th => th.textContent),
    isDisplayingNothing: () => _isDisplayingNothing,
    isDisplayingHeaderOnly: () => hasHeader() && getRowsCount() === 0,
    isDisplayingHeader: () => hasHeader(),
    isHeaderVisible: () => _isHeaderVisible(),
    hasChildWithId: id => !!protectedElement.querySelector("#".concat(id)),
    clickRow: (index, eventData) => _testUtils.default.Simulate.click(getRow(index), eventData),
    clickColumn: (rowIndex, colIndex, eventData) => _testUtils.default.Simulate.click(getCell(rowIndex, colIndex), eventData),
    mouseEnterRow: (index, eventData) => _testUtils.default.Simulate.mouseEnter(getRow(index), eventData),
    mouseLeaveRow: (index, eventData) => _testUtils.default.Simulate.mouseLeave(getRow(index), eventData),
    hasRowDetails: index => !!_getRowDetails(index),
    getRowDetailsText: index => _getRowDetails(index).textContent,
    hasSortableTitle: index => !!getSortableTitle(index),
    hasInfoIcon: index => !!getTitleInfoIcon(index),
    hasSortDescending: index => !!getSortableTitleArrowDesc(index),
    clickSort: (index, eventData) => _testUtils.default.Simulate.click(getHeaderCell(index), eventData),
    getRowDetails: index => _getRowDetails(index),
    isCellMasked: (rowIndex, colIndex) => getCell(rowIndex, colIndex).getAttribute('data-mask') === 'true',
    getStickyColumnsCount: () => getHeader().querySelectorAll("th[data-sticky=\"true\"]").length,
    getRowTabIndex: rowIndex => {
      var row = getRow(rowIndex);
      return row ? row.tabIndex : undefined;
    },
    pressKeyOnRow: (rowIndex, key) => {
      _testUtils.default.Simulate.keyDown(getRow(rowIndex), {
        key
      });
    }
  };
};
var _default = exports.default = dataTableDriverFactory;
//# sourceMappingURL=DataTable.driver.js.map