"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useToggle2 = _interopRequireDefault(require("../../../../../hooks/useToggle"));
var _action = _interopRequireDefault(require("../action"));
var _columnsMenu = _interopRequireDefault(require("./columnsMenu"));
var _useActions = require("../useActions");
var _provider = require("../../../provider");
var _excluded = ["alwaysEnabled", "columnPinning", "dataGa", "enableColumnPinning", "handleAction", "id", "icon", "isDisabled", "isOpen", "isVisible", "onClose", "selectedRows", "table", "testPrefix", "tooltipText"],
  _excluded2 = ["isVisible"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var rerenderSelector = function rerenderSelector(state) {
  return state.columnVisibility;
};
var excludedById = {
  selectionColumn: true,
  rowActionsColumn: true
};
var ColumnVisibilityAction = function ColumnVisibilityAction(_ref) {
  var alwaysEnabled = _ref.alwaysEnabled,
    _ref$columnPinning = _ref.columnPinning,
    columnPinning = _ref$columnPinning === void 0 ? {} : _ref$columnPinning,
    dataGa = _ref.dataGa,
    enableColumnPinning = _ref.enableColumnPinning,
    _handleAction = _ref.handleAction,
    id = _ref.id,
    icon = _ref.icon,
    isDisabled = _ref.isDisabled,
    isOpen = _ref.isOpen,
    isVisible = _ref.isVisible,
    onClose = _ref.onClose,
    selectedRows = _ref.selectedRows,
    table = _ref.table,
    testPrefix = _ref.testPrefix,
    tooltipText = _ref.tooltipText,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  (0, _provider.useTableState)(rerenderSelector);
  var actionRef = (0, _react.useRef)();
  var disabled = typeof isDisabled === "function" ? isDisabled() : isDisabled;
  var visible = typeof isVisible === "function" ? isVisible() : isVisible;
  var columnGroups = (0, _react.useMemo)(function () {
    var groups = [];
    table.getHeaderGroups().forEach(function (headerGroup) {
      headerGroup.headers.forEach(function (header) {
        if (excludedById[header.column.id]) return;
        if (header.column.columns && header.column.columns.length > 0) {
          var groupColumns = header.column.columns.filter(function (col) {
            return col.getCanHide();
          });
          if (groupColumns.length > 0) {
            groups.push({
              id: header.column.id,
              name: header.column.columnDef.name || (typeof header.column.columnDef.headerString === "function" ? header.column.columnDef.headerString() : header.column.columnDef.headerString) || header.column.id,
              columns: groupColumns
            });
          }
        }
      });
    });
    return groups;
  }, [table.getAllLeafColumns()]);
  var allColumns = (0, _react.useMemo)(function () {
    return _toConsumableArray(table.getAllLeafColumns()).sort(function (a, b) {
      return a.id.localeCompare(b.id, undefined, {
        sensitivity: "accent",
        ignorePunctuation: true
      });
    });
  }, [table.getAllLeafColumns()]);
  var allPinnedColumns = enableColumnPinning ? [].concat(_toConsumableArray((columnPinning == null ? void 0 : columnPinning.left) || []), _toConsumableArray((columnPinning == null ? void 0 : columnPinning.right) || [])) : [];
  var _ref2 = enableColumnPinning ? allColumns.reduce(function (accumulator, column) {
      var _extends2;
      if (!column.getCanHide()) return accumulator;
      var key = "columns";
      if (allPinnedColumns.includes(column.id)) {
        key = "pinnedColumns";
      }
      return _extends({}, accumulator, (_extends2 = {}, _extends2[key] = [].concat(_toConsumableArray(accumulator[key]), [column]), _extends2));
    }, {
      columns: [],
      pinnedColumns: []
    }) : {
      columns: allColumns,
      pinnedColumns: []
    },
    columns = _ref2.columns,
    pinnedColumns = _ref2.pinnedColumns;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_action["default"], _extends({
    ref: actionRef,
    testPrefix: "-bulk" + testPrefix,
    visible: visible,
    id: id,
    icon: icon,
    handleAction: function handleAction() {
      return _handleAction(selectedRows, table);
    },
    tooltipText: tooltipText,
    disabled: !alwaysEnabled && (selectedRows == null ? void 0 : selectedRows.length) < 1 || disabled,
    background: "elementBackground",
    selectedRows: selectedRows,
    dataGa: dataGa
  }, rest)), /*#__PURE__*/_react["default"].createElement(_columnsMenu["default"], {
    columns: columns,
    columnGroups: columnGroups,
    dataGa: dataGa,
    isOpen: isOpen,
    onClose: onClose,
    parentRef: actionRef,
    pinnedColumns: pinnedColumns
  }));
};
var ColumnVisibility = function ColumnVisibility(_ref3) {
  var isVisible = _ref3.isVisible,
    rest = _objectWithoutPropertiesLoose(_ref3, _excluded2);
  var _useToggle = (0, _useToggle2["default"])(false),
    isOpen = _useToggle[0],
    toggle = _useToggle[1],
    close = _useToggle[3];
  return /*#__PURE__*/_react["default"].createElement(ColumnVisibilityAction, _extends({
    key: "columnVisibility"
  }, _useActions.supportedBulkActions.columnVisibility, {
    handleAction: toggle,
    isOpen: isOpen,
    isVisible: isVisible,
    id: "columnVisibility",
    onClose: close
  }, rest));
};
var _default = exports["default"] = ColumnVisibility;