import _Box from "@mui/material/Box";
/*!
 * 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 } from "react";
import { MenuButton } from "../Buttons/MenuButton.js";
import { MenuItem } from "../Buttons/MenuItem.js";
import { Trans, useTranslation } from "../i18n.generated/i18n.js";
import { ArrowBottomIcon, ArrowDownIcon, ArrowTopIcon, ArrowUpIcon, MoreIcon } from "../icons.generated/index.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const DataTableRowActions = ({
  row,
  rowIndex,
  rowActionButtons,
  rowActionMenuItems,
  totalRows,
  updateRowOrder
}) => {
  const {
    t
  } = useTranslation();
  const handleToFrontClick = useCallback(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: 0
      });
    }
  }, [row.id, updateRowOrder]);
  const handleForwardClick = useCallback(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: Math.max(0, rowIndex - 1)
      });
    }
  }, [row.id, rowIndex, updateRowOrder]);
  const handleBackwardClick = useCallback(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: rowIndex + 1
      });
    }
  }, [row.id, rowIndex, updateRowOrder]);
  const handleToBackClick = useCallback(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: totalRows ? totalRows - 1 : rowIndex
      });
    }
  }, [row.id, rowIndex, totalRows, updateRowOrder]);
  return _jsxs(_Box, {
    display: "flex",
    children: [rowActionButtons?.(row), (rowActionMenuItems || updateRowOrder) && _jsxs(MenuButton, {
      ariaLabel: t("table.moreactions.arialabel"),
      buttonVariant: "floating",
      endIcon: _jsx(MoreIcon, {}),
      menuAlignment: "right",
      size: "small",
      children: [rowActionMenuItems && _jsx(_Fragment, {
        children: rowActionMenuItems(row)
      }), rowActionMenuItems && updateRowOrder && _jsx("hr", {}), updateRowOrder && _jsxs(_Fragment, {
        children: [_jsxs(MenuItem, {
          isDisabled: rowIndex <= 0,
          onClick: handleToFrontClick,
          children: [_jsx(ArrowTopIcon, {}), " ", _jsx(Trans, {
            i18nKey: "table.reorder.tofront"
          })]
        }), _jsxs(MenuItem, {
          isDisabled: rowIndex <= 0,
          onClick: handleForwardClick,
          children: [_jsx(ArrowUpIcon, {}), " ", _jsx(Trans, {
            i18nKey: "table.reorder.forward"
          })]
        }), _jsxs(MenuItem, {
          isDisabled: totalRows ? rowIndex >= totalRows - 1 : false,
          onClick: handleBackwardClick,
          children: [_jsx(ArrowDownIcon, {}), " ", _jsx(Trans, {
            i18nKey: "table.reorder.backward"
          })]
        }), totalRows && _jsxs(MenuItem, {
          isDisabled: rowIndex >= totalRows - 1,
          onClick: handleToBackClick,
          children: [_jsx(ArrowBottomIcon, {}), " ", _jsx(Trans, {
            i18nKey: "table.reorder.toback"
          })]
        })]
      })]
    })]
  });
};
const MemoizedDataTableRowActions = memo(DataTableRowActions);
MemoizedDataTableRowActions.displayName = "DataTableRowActions";
export { MemoizedDataTableRowActions as DataTableRowActions };
//# sourceMappingURL=DataTableRowActions.js.map