"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableSettings = void 0;
var _Checkbox2 = _interopRequireDefault(require("@mui/material/Checkbox"));
var _react = require("react");
var _MenuButton = require("../../Buttons/MenuButton.cjs");
var _MenuItem = require("../../Buttons/MenuItem.cjs");
var _i18n = require("../../i18n.generated/i18n.cjs");
var _index = require("../../icons.generated/index.cjs");
var _constants = require("./constants.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const TableSettings = ({
  setTableState,
  tableLayoutOptions,
  tableState
}) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const {
    hasChangeableDensity,
    hasColumnVisibility,
    columns
  } = tableLayoutOptions;
  const {
    rowDensity,
    columnVisibility
  } = tableState;
  const changeRowDensity = (0, _react.useCallback)(value => {
    setTableState(prevState => ({
      ...prevState,
      rowDensity: value
    }));
  }, [setTableState]);
  const changeColumnVisibility = (0, _react.useCallback)(columnId => {
    setTableState(prevState => ({
      ...prevState,
      columnVisibility: {
        ...prevState.columnVisibility,
        [columnId]: prevState.columnVisibility ? prevState.columnVisibility[columnId] === false : false
      }
    }));
  }, [setTableState]);
  const visibleColumns = (0, _react.useMemo)(() => new Set(columns.filter(column => columnVisibility ? columnVisibility[column.accessorKey] !== false : true).map(column => column.accessorKey)), [columns, columnVisibility]);
  const memoizedDensityMenu = (0, _react.useMemo)(() => hasChangeableDensity && (0, _jsxRuntime.jsx)(_MenuButton.MenuButton, {
    ariaLabel: t("table.density.arialabel"),
    endIcon: (0, _jsxRuntime.jsx)(_index.ListIcon, {}),
    menuAlignment: "right",
    shouldCloseOnSelect: false,
    children: _constants.densityValues.map(value => (0, _jsxRuntime.jsx)(_MenuItem.MenuItem, {
      isSelected: rowDensity === value,
      onClick: () => changeRowDensity(value),
      children: t(`table.density.${value}`)
    }, value))
  }), [hasChangeableDensity, t, rowDensity, changeRowDensity]);
  const memoizedColumnVisibilityMenu = (0, _react.useMemo)(() => hasColumnVisibility && (0, _jsxRuntime.jsx)(_MenuButton.MenuButton, {
    ariaLabel: t("table.columnvisibility.arialabel"),
    endIcon: (0, _jsxRuntime.jsx)(_index.ShowIcon, {}),
    menuAlignment: "right",
    shouldCloseOnSelect: false,
    children: columns.filter(column => Boolean(column.enableHiding !== false)).map(column => (0, _jsxRuntime.jsxs)(_MenuItem.MenuItem, {
      onClick: () => changeColumnVisibility(column.id),
      children: [(0, _jsxRuntime.jsx)(_Checkbox2.default, {
        checked: visibleColumns.has(column.id)
      }), column.header]
    }, column.accessorKey))
  }), [hasColumnVisibility, t, columns, changeColumnVisibility, visibleColumns]);
  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [memoizedDensityMenu, memoizedColumnVisibilityMenu]
  });
};
const MemoizedTableSettings = exports.TableSettings = (0, _react.memo)(TableSettings);
MemoizedTableSettings.displayName = "TableSettings";
//# sourceMappingURL=TableSettings.cjs.map