"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 _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); }
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) { _defineProperty(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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 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 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 _objectSpread2;
      if (!column.getCanHide()) return accumulator;
      var key = "columns";
      if (allPinnedColumns.includes(column.id)) {
        key = "pinnedColumns";
      }
      return _objectSpread(_objectSpread({}, accumulator), {}, (_objectSpread2 = {}, _objectSpread2[key] = [].concat(_toConsumableArray(accumulator[key]), [column]), _objectSpread2));
    }, {
      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,
    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;