/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with 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 classNames from "classnames";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { SkeletonBar } from "../base-components/skeleton/SkeletonBar.js";
import { LoadingRow } from "./LoadingRow.js";
import { TableHeader } from "./TableHeader.js";
import { DEFAULT_LOADING_COLUMN_WIDTH, DEFAULT_ROW_HEIGHT, MIN_LOADING_ROWS } from "./utils/constants.js";
import loadingStyles from "./LoadingCell.module.css.js";
import bodyStyles from "./TableBody.module.css.js";
import headerStyles from "./TableHeader.module.css.js";
export function LoadingStateTable({
  table,
  tableContainerRef,
  headerGroups,
  rowHeight = DEFAULT_ROW_HEIGHT,
  columnWidth = DEFAULT_LOADING_COLUMN_WIDTH
}) {
  // If selection enabled, there will be a header for the selection column
  const enableRowSelection = table.options.enableRowSelection;
  const minHeaderCount = enableRowSelection ? 1 : 0;
  const headers = useMemo(() => headerGroups.length > 0 ? headerGroups[0].headers : [], [headerGroups]);
  const hasHeadersLoaded = headers.length > minHeaderCount;
  const headerRef = useRef(null);
  const bodyRef = useRef(null);
  const [loadingRowCount, setLoadingRowCount] = useState(MIN_LOADING_ROWS);
  const [loadingColumnCount, setLoadingColumnCount] = useState(headers.length);
  // Calculate number of columns needed to fill container width
  useEffect(() => {
    if (hasHeadersLoaded) {
      setLoadingColumnCount(headers.length);
    } else {
      if (tableContainerRef.current) {
        const tableWidth = tableContainerRef.current.clientWidth;
        if (tableWidth > 0) {
          const columnsNeeded = Math.ceil(tableWidth / columnWidth);
          setLoadingColumnCount(columnsNeeded);
        }
      }
    }
  }, [columnWidth, hasHeadersLoaded, headers, tableContainerRef]);

  // Calculate number of rows needed to fill container height
  useEffect(() => {
    if (tableContainerRef.current) {
      const containerHeight = tableContainerRef.current.clientHeight;
      const availableHeight = containerHeight - rowHeight;
      if (availableHeight > 0) {
        const rowsNeeded = Math.ceil(availableHeight / rowHeight);
        setLoadingRowCount(Math.max(rowsNeeded, MIN_LOADING_ROWS));
      }
    }
  }, [tableContainerRef, rowHeight]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, hasHeadersLoaded ? /*#__PURE__*/React.createElement(TableHeader, {
    table: table
  }) : /*#__PURE__*/React.createElement("thead", {
    className: headerStyles.osdkTableHeader,
    ref: headerRef
  }, /*#__PURE__*/React.createElement("tr", {
    className: headerStyles.osdkTableHeaderRow
  }, Array.from({
    length: loadingColumnCount
  }).map((_, index) => {
    const width = headers.length > index ? headers[index].getSize() : columnWidth;
    return /*#__PURE__*/React.createElement("th", {
      key: `loading-header-${index}`,
      className: headerStyles.osdkTableHeaderCell,
      style: {
        width
      }
    }, /*#__PURE__*/React.createElement(SkeletonBar, {
      className: classNames(headerStyles.osdkLoadingHeaderCell, loadingStyles.osdkCellSkeleton)
    }));
  }))), /*#__PURE__*/React.createElement("tbody", {
    className: bodyStyles.osdkTableBody,
    ref: bodyRef
  }, Array.from({
    length: loadingRowCount
  }).map((_, index) => /*#__PURE__*/React.createElement(LoadingRow, {
    key: `skeleton-${index}`,
    columnCount: loadingColumnCount,
    headers: headers,
    translateY: rowHeight * index,
    rowHeight: rowHeight,
    columnWidth: columnWidth
  }))));
}
//# sourceMappingURL=LoadingStateTable.js.map