"use strict";

exports.__esModule = true;
exports.EditableTableUniDriver = EditableTableUniDriver;
exports.default = void 0;
var _unidriver = require("@wix/wix-ui-test-utils/unidriver");
var _unidriver2 = require("@wix/design-system/dist/testkit/unidriver");
var _unidriver3 = require("../../unidriver");
var _EditableCellUni = require("./EditableCell.uni.driver");
var _EditableTableContextMenuUni = require("./EditableTableContextMenu.uni.driver");
var _EditableColumnHeaderUni = require("./EditableColumnHeader.uni.driver");
const {
  fireEvent = null
} = process.env.NODE_ENV === 'test' ? (() => {
  try {
    return require('@testing-library/react');
  } catch {}
  return {};
})() : {};
function EditableTableUniDriver(base, body) {
  const getRows = () => base.$$('tbody tr[data-table-row="dataTableRow"]');
  const getCells = () => base.$$('[role="gridcell"]');
  const initialLoaderBase = () => base.$('[data-hook="placeholder-states-loading-state"]');
  return {
    ...(0, _unidriver3.baseUniDriverFactory)(base),
    /** Number of rendered data rows */
    getRowsCount: () => getRows().count(),
    /** Number of column headers */
    getColumnsCount: () => base.$$('thead th').count(),
    /** Get column header texts */
    getColumnHeaders: () => base.$$('thead th').map(th => th.text()),
    /** Get a cell driver by row key and column id */
    getCell: (rowKey, columnId) => (0, _EditableCellUni.EditableCellUniDriver)(base.$(`[role="gridcell"][data-row-key="${rowKey}"][data-column-id="${columnId}"]`), body),
    /** Get a cell driver by row index and column index */
    getCellByIndex: (rowIdx, colIdx) => {
      const row = getRows().get(rowIdx);
      const cell = row.$$('[role="gridcell"]').get(colIdx);
      return (0, _EditableCellUni.EditableCellUniDriver)(cell, body);
    },
    /** Total number of gridcells rendered */
    getCellsCount: () => getCells().count(),
    /** Wait until root element exists */
    wait: timeout => base.wait(timeout),
    /** Wait for the initial fetch loader to be shown */
    waitInitialLoader: timeout => initialLoaderBase().wait(timeout),
    /** Wait for the initial fetch loader to be removed */
    waitInitialLoaderRemoved: (timeout, delay) => (0, _unidriver.waitFor)(async () => !(await initialLoaderBase().exists()), timeout, delay),
    /** Wait for at least one data row to render */
    waitForRow: timeout => getRows().get(0).wait(timeout),
    /** Get the header actions driver for a column (portaled to body) */
    getHeaderActions: columnId => (0, _EditableColumnHeaderUni.EditableColumnHeaderUniDriver)(base.$(`[data-hook="header-actions-${columnId}"]`), body),
    /** Get the context menu driver (portaled to body) */
    getContextMenu: () => (0, _EditableTableContextMenuUni.EditableTableContextMenuUniDriver)(base, body),
    /** Get the add item row driver */
    getAddItemRow: () => (0, _unidriver2.AddItemUniDriver)(base.$('[data-hook="add-row-button"]'), body),
    /** Get the delete confirmation modal driver */
    getConfirmActionModal: () => (0, _unidriver2.MessageModalLayoutUniDriver)(body.$('[data-hook="collection-action-modal"]'), body),
    /** Press a key on the table container (triggers useKeyboardNavigation) */
    pressKey: async (key, opts) => {
      const el = await base.getNative();
      if (fireEvent) {
        fireEvent.keyDown(el, {
          key,
          ...opts
        });
      } else {
        el.dispatchEvent(new KeyboardEvent('keydown', {
          key,
          bubbles: true,
          ...opts
        }));
      }
    },
    /** Dispatch a paste event on the table container */
    dispatchPaste: async text => {
      const el = await base.getNative();
      const event = Object.assign(new Event('paste', {
        bubbles: true
      }), {
        clipboardData: {
          getData: () => text
        }
      });
      el.dispatchEvent(event);
    }
  };
}
var _default = exports.default = EditableTableUniDriver;
//# sourceMappingURL=EditableTable.uni.driver.js.map