"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DataTableRowActions = void 0;
var _Box2 = _interopRequireDefault(require("@mui/material/Box"));
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 _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 DataTableRowActions = ({
  row,
  rowIndex,
  rowActionButtons,
  rowActionMenuItems,
  totalRows,
  updateRowOrder
}) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const handleToFrontClick = (0, _react.useCallback)(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: 0
      });
    }
  }, [row.id, updateRowOrder]);
  const handleForwardClick = (0, _react.useCallback)(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: Math.max(0, rowIndex - 1)
      });
    }
  }, [row.id, rowIndex, updateRowOrder]);
  const handleBackwardClick = (0, _react.useCallback)(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: rowIndex + 1
      });
    }
  }, [row.id, rowIndex, updateRowOrder]);
  const handleToBackClick = (0, _react.useCallback)(() => {
    if (updateRowOrder) {
      updateRowOrder({
        rowId: row.id,
        newRowIndex: totalRows ? totalRows - 1 : rowIndex
      });
    }
  }, [row.id, rowIndex, totalRows, updateRowOrder]);
  return (0, _jsxRuntime.jsxs)(_Box2.default, {
    display: "flex",
    children: [rowActionButtons?.(row), (rowActionMenuItems || updateRowOrder) && (0, _jsxRuntime.jsxs)(_MenuButton.MenuButton, {
      ariaLabel: t("table.moreactions.arialabel"),
      buttonVariant: "floating",
      endIcon: (0, _jsxRuntime.jsx)(_index.MoreIcon, {}),
      menuAlignment: "right",
      size: "small",
      children: [rowActionMenuItems && (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: rowActionMenuItems(row)
      }), rowActionMenuItems && updateRowOrder && (0, _jsxRuntime.jsx)("hr", {}), updateRowOrder && (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [(0, _jsxRuntime.jsxs)(_MenuItem.MenuItem, {
          isDisabled: rowIndex <= 0,
          onClick: handleToFrontClick,
          children: [(0, _jsxRuntime.jsx)(_index.ArrowTopIcon, {}), " ", (0, _jsxRuntime.jsx)(_i18n.Trans, {
            i18nKey: "table.reorder.tofront"
          })]
        }), (0, _jsxRuntime.jsxs)(_MenuItem.MenuItem, {
          isDisabled: rowIndex <= 0,
          onClick: handleForwardClick,
          children: [(0, _jsxRuntime.jsx)(_index.ArrowUpIcon, {}), " ", (0, _jsxRuntime.jsx)(_i18n.Trans, {
            i18nKey: "table.reorder.forward"
          })]
        }), (0, _jsxRuntime.jsxs)(_MenuItem.MenuItem, {
          isDisabled: totalRows ? rowIndex >= totalRows - 1 : false,
          onClick: handleBackwardClick,
          children: [(0, _jsxRuntime.jsx)(_index.ArrowDownIcon, {}), " ", (0, _jsxRuntime.jsx)(_i18n.Trans, {
            i18nKey: "table.reorder.backward"
          })]
        }), totalRows && (0, _jsxRuntime.jsxs)(_MenuItem.MenuItem, {
          isDisabled: rowIndex >= totalRows - 1,
          onClick: handleToBackClick,
          children: [(0, _jsxRuntime.jsx)(_index.ArrowBottomIcon, {}), " ", (0, _jsxRuntime.jsx)(_i18n.Trans, {
            i18nKey: "table.reorder.toback"
          })]
        })]
      })]
    })]
  });
};
const MemoizedDataTableRowActions = exports.DataTableRowActions = (0, _react.memo)(DataTableRowActions);
MemoizedDataTableRowActions.displayName = "DataTableRowActions";
//# sourceMappingURL=DataTableRowActions.cjs.map