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); }
import React, { memo, useState, useEffect } from "react";
import Flex from "../../../templates/flex";
import { useTableState } from "../../provider";
import useActions from "./useActions";
import Action from "./action";
import ColumnVisibility from "./columnVisibility";
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;
  useTableState(rerenderSelector);
  var _useState = useState([]),
    selectedRows = _useState[0],
    setActualSelectedRows = _useState[1];
  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 = useActions(bulkActions);
  if (!actions.length && !enableColumnVisibility) return null;
  return /*#__PURE__*/React.createElement(Flex, {
    gap: 0.5,
    "data-testid": "bulk-actions",
    flex: "1",
    justifyContent: "end"
  }, actions.map(function (action) {
    return /*#__PURE__*/React.createElement(Action, _extends({
      key: action.id
    }, action, {
      columnPinning: columnPinning,
      dataGa: dataGa,
      enableColumnPinning: enableColumnPinning,
      selectedRows: selectedRows,
      table: table,
      testPrefix: testPrefix
    }));
  }), /*#__PURE__*/React.createElement(ColumnVisibility, {
    isVisible: enableColumnVisibility,
    table: table,
    testPrefix: testPrefix,
    columnPinning: columnPinning,
    dataGa: dataGa,
    enableColumnPinning: enableColumnPinning
  }));
};
export default /*#__PURE__*/memo(HeaderActions);