"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _flex = _interopRequireDefault(require("../../../templates/flex"));
var _provider = require("../../provider");
var _useActions = _interopRequireDefault(require("./useActions"));
var _action = _interopRequireDefault(require("./action"));
var _columnVisibility = _interopRequireDefault(require("./columnVisibility"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var noop = function noop() {};
var rerenderSelector = function rerenderSelector(state) {
  return state.selectedRows;
};
var useSelectedRowsObserver = function useSelectedRowsObserver(table, _ref) {
  var _ref$onRowSelected = _ref.onRowSelected,
    onRowSelected = _ref$onRowSelected === void 0 ? noop : _ref$onRowSelected,
    rowSelection = _ref.rowSelection;
  (0, _provider.useTableState)(rerenderSelector);
  var _useState = (0, _react.useState)([]),
    selectedRows = _useState[0],
    setActualSelectedRows = _useState[1];
  (0, _react.useEffect)(function () {
    var _table$getSelectedRow = table.getSelectedRowModel(),
      flatRows = _table$getSelectedRow.flatRows;
    if (flatRows) {
      var selected = flatRows.reduce(function (acc, _ref2) {
        var original = _ref2.original;
        if (original != null && original.disabled || original != null && original.unselectable) return acc;
        acc.push(original);
        return acc;
      }, []);
      setActualSelectedRows(selected);
      onRowSelected(selected);
    }
  }, [rowSelection, table]);
  return selectedRows;
};
var HeaderActions = function HeaderActions(_ref3) {
  var bulkActions = _ref3.bulkActions,
    columnPinning = _ref3.columnPinning,
    dataGa = _ref3.dataGa,
    enableColumnVisibility = _ref3.enableColumnVisibility,
    enableColumnPinning = _ref3.enableColumnPinning,
    table = _ref3.table,
    testPrefix = _ref3.testPrefix,
    onRowSelected = _ref3.onRowSelected,
    rowSelection = _ref3.rowSelection;
  var selectedRows = useSelectedRowsObserver(table, {
    onRowSelected: onRowSelected,
    rowSelection: rowSelection
  });
  var actions = (0, _useActions["default"])(bulkActions);
  if (!actions.length && !enableColumnVisibility) return null;
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    gap: 0.5,
    "data-testid": "bulk-actions",
    justifyContent: "end"
  }, actions.map(function (action) {
    return /*#__PURE__*/_react["default"].createElement(_action["default"], _extends({
      key: action.id
    }, action, {
      columnPinning: columnPinning,
      dataGa: dataGa,
      enableColumnPinning: enableColumnPinning,
      selectedRows: selectedRows,
      table: table,
      testPrefix: testPrefix
    }));
  }), /*#__PURE__*/_react["default"].createElement(_columnVisibility["default"], {
    isVisible: enableColumnVisibility,
    table: table,
    testPrefix: testPrefix,
    columnPinning: columnPinning,
    dataGa: dataGa,
    enableColumnPinning: enableColumnPinning
  }));
};
var _default = exports["default"] = /*#__PURE__*/(0, _react.memo)(HeaderActions);