/*
 * Copyright 2026 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, { useCallback, useMemo, useState } from "react";
import styles from "./BaseExcelViewer.module.css";

/**
 * Converts a 0-based column index to a spreadsheet column letter (0=A, 1=B, ..., 25=Z, 26=AA).
 */
function columnIndexToLetter(index) {
  let letter = "";
  let num = index;
  do {
    const remainder = num % 26;
    letter = String.fromCharCode(65 + remainder) + letter;
    num = Math.floor(num / 26) - 1;
  } while (num >= 0);
  return letter;
}
const SheetTable = /*#__PURE__*/React.memo(({
  sheet
}) => {
  const maxCols = useMemo(() => sheet.rows.reduce((max, row) => Math.max(max, row.length), 0), [sheet.rows]);
  if (sheet.rows.length === 0) {
    return /*#__PURE__*/React.createElement("div", {
      className: styles.emptySheet
    }, "Empty sheet");
  }
  return /*#__PURE__*/React.createElement("div", {
    className: styles.tableContainer
  }, /*#__PURE__*/React.createElement("table", {
    className: styles.table
  }, /*#__PURE__*/React.createElement("thead", null, /*#__PURE__*/React.createElement("tr", null, /*#__PURE__*/React.createElement("th", {
    className: styles.cornerCell
  }), Array.from({
    length: maxCols
  }, (_, i) => /*#__PURE__*/React.createElement("th", {
    key: i,
    className: styles.columnHeader
  }, columnIndexToLetter(i))))), /*#__PURE__*/React.createElement("tbody", null, sheet.rows.map((row, rowIndex) => /*#__PURE__*/React.createElement("tr", {
    key: rowIndex
  }, /*#__PURE__*/React.createElement("td", {
    className: styles.rowHeader
  }, rowIndex + 1), Array.from({
    length: maxCols
  }, (_, cellIndex) => /*#__PURE__*/React.createElement("td", {
    key: cellIndex,
    className: styles.cell
  }, row[cellIndex] ?? "")))))));
});
SheetTable.displayName = "SheetTable";
export function BaseExcelViewer({
  spreadsheet,
  className
}) {
  const [activeSheetIndex, setActiveSheetIndex] = useState(0);
  const rootClassName = classnames(styles.container, className);
  const safeIndex = Math.min(activeSheetIndex, Math.max(0, spreadsheet.sheets.length - 1));
  const activeSheet = useMemo(() => spreadsheet.sheets[safeIndex], [spreadsheet.sheets, safeIndex]);
  const handleTabClick = useCallback(index => {
    setActiveSheetIndex(index);
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: rootClassName
  }, activeSheet != null ? /*#__PURE__*/React.createElement(SheetTable, {
    sheet: activeSheet
  }) : /*#__PURE__*/React.createElement("div", {
    className: styles.emptySheet
  }, "No sheets"), spreadsheet.sheets.length > 1 && /*#__PURE__*/React.createElement("div", {
    className: styles.tabBar
  }, spreadsheet.sheets.map((sheet, index) => /*#__PURE__*/React.createElement("button", {
    key: sheet.name,
    className: classnames(styles.tab, {
      [styles.tabActive]: index === safeIndex
    }),
    onClick: () => handleTabClick(index),
    type: "button"
  }, sheet.name))));
}
//# sourceMappingURL=BaseExcelViewer.js.map