/*!
 * 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 { MenuItem } from "../../Buttons/MenuItem.js";
import { useTranslation } from "../../i18n.generated/i18n.js";
import { ArrowBottomIcon, ArrowDownIcon, ArrowTopIcon, ArrowUpIcon } from "../../icons.generated/index.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const RowActions = ({
  isRowReorderingDisabled,
  row: incomingRow,
  rowActionMenuItems,
  rowIndex,
  totalRows,
  updateRowOrder
}) => {
  const {
    t
  } = useTranslation();
  const row = incomingRow.original ? incomingRow.original : incomingRow;
  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(_Fragment, {
    children: [rowActionMenuItems && rowActionMenuItems(row), rowActionMenuItems && updateRowOrder && _jsx("hr", {}), updateRowOrder && _jsxs(_Fragment, {
      children: [_jsxs(MenuItem, {
        isDisabled: rowIndex <= 0 || isRowReorderingDisabled,
        onClick: handleToFrontClick,
        children: [_jsx(ArrowTopIcon, {}), " ", t("table.reorder.tofront")]
      }), _jsxs(MenuItem, {
        isDisabled: rowIndex <= 0 || isRowReorderingDisabled,
        onClick: handleForwardClick,
        children: [_jsx(ArrowUpIcon, {}), " ", t("table.reorder.forward")]
      }), _jsxs(MenuItem, {
        isDisabled: (totalRows ? rowIndex >= totalRows - 1 : false) || isRowReorderingDisabled,
        onClick: handleBackwardClick,
        children: [_jsx(ArrowDownIcon, {}), " ", t("table.reorder.backward")]
      }), totalRows && _jsxs(MenuItem, {
        isDisabled: rowIndex >= totalRows - 1 || isRowReorderingDisabled,
        onClick: handleToBackClick,
        children: [_jsx(ArrowBottomIcon, {}), " ", t("table.reorder.toback")]
      })]
    })]
  });
};
const MemoizedRowActions = memo(RowActions);
MemoizedRowActions.displayName = "RowActions";
export { MemoizedRowActions as RowActions };
//# sourceMappingURL=RowActions.js.map