import _Checkbox from "@mui/material/Checkbox";
/*!
 * 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.
 */
import { memo, useCallback, useMemo } from "react";
import { MenuButton } from "../../Buttons/MenuButton.js";
import { MenuItem } from "../../Buttons/MenuItem.js";
import { useTranslation } from "../../i18n.generated/i18n.js";
import { ListIcon, ShowIcon } from "../../icons.generated/index.js";
import { densityValues } from "./constants.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const TableSettings = ({
  setTableState,
  tableLayoutOptions,
  tableState
}) => {
  const {
    t
  } = useTranslation();
  const {
    hasChangeableDensity,
    hasColumnVisibility,
    columns
  } = tableLayoutOptions;
  const {
    rowDensity,
    columnVisibility
  } = tableState;
  const changeRowDensity = useCallback(value => {
    setTableState(prevState => ({
      ...prevState,
      rowDensity: value
    }));
  }, [setTableState]);
  const changeColumnVisibility = useCallback(columnId => {
    setTableState(prevState => ({
      ...prevState,
      columnVisibility: {
        ...prevState.columnVisibility,
        [columnId]: prevState.columnVisibility ? prevState.columnVisibility[columnId] === false : false
      }
    }));
  }, [setTableState]);
  const visibleColumns = useMemo(() => new Set(columns.filter(column => columnVisibility ? columnVisibility[column.accessorKey] !== false : true).map(column => column.accessorKey)), [columns, columnVisibility]);
  const memoizedDensityMenu = useMemo(() => hasChangeableDensity && _jsx(MenuButton, {
    ariaLabel: t("table.density.arialabel"),
    endIcon: _jsx(ListIcon, {}),
    menuAlignment: "right",
    shouldCloseOnSelect: false,
    children: densityValues.map(value => _jsx(MenuItem, {
      isSelected: rowDensity === value,
      onClick: () => changeRowDensity(value),
      children: t(`table.density.${value}`)
    }, value))
  }), [hasChangeableDensity, t, rowDensity, changeRowDensity]);
  const memoizedColumnVisibilityMenu = useMemo(() => hasColumnVisibility && _jsx(MenuButton, {
    ariaLabel: t("table.columnvisibility.arialabel"),
    endIcon: _jsx(ShowIcon, {}),
    menuAlignment: "right",
    shouldCloseOnSelect: false,
    children: columns.filter(column => Boolean(column.enableHiding !== false)).map(column => _jsxs(MenuItem, {
      onClick: () => changeColumnVisibility(column.id),
      children: [_jsx(_Checkbox, {
        checked: visibleColumns.has(column.id)
      }), column.header]
    }, column.accessorKey))
  }), [hasColumnVisibility, t, columns, changeColumnVisibility, visibleColumns]);
  return _jsxs(_Fragment, {
    children: [memoizedDensityMenu, memoizedColumnVisibilityMenu]
  });
};
const MemoizedTableSettings = memo(TableSettings);
MemoizedTableSettings.displayName = "TableSettings";
export { MemoizedTableSettings as TableSettings };
//# sourceMappingURL=TableSettings.js.map