"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _DataTable = _interopRequireDefault(require("./DataTable/DataTable.driver"));
var _Checkbox = _interopRequireDefault(require("../Checkbox/Checkbox.driver"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var tableDriverFactory = _ref => {
  var {
    element,
    eventTrigger
  } = _ref;
  var dataTableDriver = (0, _DataTable.default)({
    element
  });
  var getTitlebar = () => element.querySelector('[data-hook="table-title-bar"]');
  var getRowCheckbox = index => {
    var _dataTableDriver$getR;
    return (_dataTableDriver$getR = dataTableDriver.getRow(index)) == null || (_dataTableDriver$getR = _dataTableDriver$getR.querySelector('[data-hook="bulk-selection-cell"]')) == null ? void 0 : _dataTableDriver$getR.querySelector('[data-hook="row-select"]');
  };
  var getRowCheckboxDriver = index => (0, _Checkbox.default)({
    element: getRowCheckbox(index),
    eventTrigger
  });
  var getBulkSelectionCheckboxDriver = () => {
    var _dataTableDriver$getH;
    return (0, _Checkbox.default)({
      element: dataTableDriver == null || (_dataTableDriver$getH = dataTableDriver.getHeaderCellByDataHook('bulk-selection-cell')) == null ? void 0 : _dataTableDriver$getH.querySelector('[data-hook="table-select"]'),
      eventTrigger
    });
  };
  var isBulkSelectionChecked = () => {
    var checkboxDriver = getBulkSelectionCheckboxDriver();
    return checkboxDriver.isChecked() && !checkboxDriver.isIndeterminate();
  };
  var isBulkSelectionIndeterminate = () => {
    var checkboxDriver = getBulkSelectionCheckboxDriver();
    return !checkboxDriver.isChecked() && checkboxDriver.isIndeterminate();
  };
  var isBulkSelectionUnchecked = () => {
    var checkboxDriver = getBulkSelectionCheckboxDriver();
    return !checkboxDriver.isChecked() && !checkboxDriver.isIndeterminate();
  };
  var isBulkSelectionDisabled = () => {
    var checkboxDriver = getBulkSelectionCheckboxDriver();
    return checkboxDriver.isDisabled();
  };
  var isRowSelectionDisabled = index => {
    var checkboxDriver = getRowCheckboxDriver(index);
    return checkboxDriver.isDisabled();
  };
  return _objectSpread(_objectSpread({}, dataTableDriver), {}, {
    element,
    /** Get driver of row selection checkbox by row index */
    getRowCheckboxDriver,
    /** Get driver of row bulk-selection checkbox */
    getBulkSelectionCheckboxDriver,
    /** Whether bulk selection checkbox is disabled */
    isBulkSelectionDisabled,
    /** Whether specific row selection checkbox is disabled */
    isRowSelectionDisabled,
    /** Click the row selection checkbox */
    clickRowCheckbox: index => eventTrigger.click(getRowCheckbox(index)),
    /** Click the bulk-selection checkbox */
    clickBulkSelectionCheckbox: () => getBulkSelectionCheckboxDriver().click(),
    /** Is row selected by index */
    isRowSelected: index => getRowCheckboxDriver(index).isChecked(),
    /** Get bulk selection state. Possible value 'ALL', 'SOME', 'NONE. */
    getBulkSelectionState: () => {
      if (isBulkSelectionChecked()) {
        return 'ALL';
      }
      if (isBulkSelectionIndeterminate()) {
        return 'SOME';
      }
      if (isBulkSelectionUnchecked()) {
        return 'NONE';
      }
    },
    /** Get title-bar (column titles) */
    getTitlebar,
    getCellTextValue: (rowIndex, cellIndex) => dataTableDriver.getCell(rowIndex, cellIndex).textContent,
    isSelectionTooltipEnabled: index => getRowCheckboxDriver(index).isTooltipEnabled(),
    getSelectionTooltipContent: index => getRowCheckboxDriver(index).getTooltipContent()
  });
};
var _default = exports.default = tableDriverFactory;
//# sourceMappingURL=Table.driver.js.map