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, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const DataTableSettings = ({
  hasChangeableDensity,
  rowDensity,
  setRowDensity,
  hasColumnVisibility,
  columns,
  columnVisibility,
  setColumnVisibility
}) => {
  const {
    t
  } = useTranslation();
  const changeRowDensity = useCallback(value => _event => {
    if (process.env.NODE_ENV === "development") console.debug(_event);
    setRowDensity(value);
  }, [setRowDensity]);
  const changeColumnVisibility = useCallback(columnId => _event => {
    if (process.env.NODE_ENV === "development") console.debug(_event);
    setColumnVisibility(prevVisibility => ({
      ...prevVisibility,
      [columnId]: prevVisibility ? prevVisibility[columnId] === false : false
    }));
  }, [setColumnVisibility]);
  const isColumnVisibilityChecked = useMemo(() => {
    return columns.reduce((acc, column) => {
      const isChecked = columnVisibility ? columnVisibility[column.accessorKey] !== false : true;
      acc[column.accessorKey] = isChecked;
      return acc;
    }, {});
  }, [columns, columnVisibility]);
  return _jsxs(_Fragment, {
    children: [hasChangeableDensity && _jsx(MenuButton, {
      ariaLabel: t("table.density.arialabel"),
      endIcon: _jsx(ListIcon, {}),
      menuAlignment: "right",
      shouldCloseOnSelect: false,
      children: _jsx(_Fragment, {
        children: densityValues.map(value => _jsx(MenuItem, {
          isSelected: rowDensity === value,
          onClick: changeRowDensity(value),
          children: t(`table.density.${value}`)
        }, value))
      })
    }), hasColumnVisibility && _jsx(MenuButton, {
      ariaLabel: t("table.columnvisibility.arialabel"),
      endIcon: _jsx(ShowIcon, {}),
      menuAlignment: "right",
      shouldCloseOnSelect: false,
      children: _jsx(_Fragment, {
        children: columns.filter(column => column.enableHiding !== false).map(column => _jsxs(MenuItem, {
          onClick: changeColumnVisibility(column.id),
          children: [_jsx(_Checkbox, {
            checked: isColumnVisibilityChecked[column.accessorKey]
          }), column.header]
        }, column.accessorKey))
      })
    })]
  });
};
const MemoizedDataTableSettings = memo(DataTableSettings);
MemoizedDataTableSettings.displayName = "DataTableSettings";
export { MemoizedDataTableSettings as DataTableSettings };
//# sourceMappingURL=DataTableSettings.js.map