"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StaticTable = void 0;
var _materialReactTable = require("material-react-table");
var _react = require("react");
var _i18n = require("../i18n.generated/i18n.cjs");
var _jsxRuntime = require("react/jsx-runtime");
/*!
 * Copyright (c) 2022-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 StaticTable = ({
  columns = [],
  data = [],
  getRowId,
  hasError,
  initialState,
  onGlobalFilterChange,
  state,
  ToolbarButtons
}) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const rowVirtualizerInstanceRef = (0, _react.useRef)(null);
  const [columnFilters, setColumnFilters] = (0, _react.useState)([]);
  const [globalFilter, setGlobalFilter] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    if (globalFilter !== undefined) {
      onGlobalFilterChange?.(globalFilter);
    }
  }, [globalFilter, onGlobalFilterChange]);
  const modifiedState = (0, _react.useMemo)(() => ({
    globalFilter,
    columnFilters,
    ...state
  }), [globalFilter, columnFilters, state]);
  const renderTopToolbarCustomActions = (0, _react.useCallback)(({
    table
  }) => (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: ToolbarButtons && (0, _jsxRuntime.jsx)(ToolbarButtons, {
      table: table
    })
  }), [ToolbarButtons]);
  (0, _react.useEffect)(() => {
    try {
      rowVirtualizerInstanceRef.current?.scrollToIndex?.(0);
    } catch (error) {
      console.error(error);
    }
  }, [columnFilters, globalFilter]);
  const table = (0, _materialReactTable.useMaterialReactTable)({
    columns: columns,
    data: data,
    enableBottomToolbar: false,
    enablePagination: false,
    enableRowVirtualization: true,
    enableSorting: false,
    getRowId: getRowId,
    initialState: initialState,
    muiToolbarAlertBannerProps: hasError ? {
      children: t("table.error"),
      color: "error"
    } : undefined,
    onColumnFiltersChange: setColumnFilters,
    onGlobalFilterChange: setGlobalFilter,
    renderTopToolbarCustomActions: renderTopToolbarCustomActions,
    rowVirtualizerInstanceRef: rowVirtualizerInstanceRef,
    rowVirtualizerOptions: {
      overscan: 4
    },
    state: modifiedState
  });
  return (0, _jsxRuntime.jsx)(_materialReactTable.MaterialReactTable, {
    table: table
  });
};
const MemoizedStaticTable = exports.StaticTable = (0, _react.memo)(StaticTable);
MemoizedStaticTable.displayName = "StaticTable";
//# sourceMappingURL=StaticTable.cjs.map