/*!
 * 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.
 */

import { MaterialReactTable, useMaterialReactTable } from "material-react-table";
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "../i18n.generated/i18n.js";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
const StaticTable = ({
  columns = [],
  data = [],
  getRowId,
  hasError,
  initialState,
  onGlobalFilterChange,
  state,
  ToolbarButtons
}) => {
  const {
    t
  } = useTranslation();
  const rowVirtualizerInstanceRef = useRef(null);
  const [columnFilters, setColumnFilters] = useState([]);
  const [globalFilter, setGlobalFilter] = useState();
  useEffect(() => {
    if (globalFilter !== undefined) {
      onGlobalFilterChange?.(globalFilter);
    }
  }, [globalFilter, onGlobalFilterChange]);
  const modifiedState = useMemo(() => ({
    globalFilter,
    columnFilters,
    ...state
  }), [globalFilter, columnFilters, state]);
  const renderTopToolbarCustomActions = useCallback(({
    table
  }) => _jsx(_Fragment, {
    children: ToolbarButtons && _jsx(ToolbarButtons, {
      table: table
    })
  }), [ToolbarButtons]);
  useEffect(() => {
    try {
      rowVirtualizerInstanceRef.current?.scrollToIndex?.(0);
    } catch (error) {
      console.error(error);
    }
  }, [columnFilters, globalFilter]);
  const table = 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 _jsx(MaterialReactTable, {
    table: table
  });
};
const MemoizedStaticTable = memo(StaticTable);
MemoizedStaticTable.displayName = "StaticTable";
export { MemoizedStaticTable as StaticTable };
//# sourceMappingURL=StaticTable.js.map