'use strict';

var chunkMVQKV5BJ_cjs = require('./chunk-MVQKV5BJ.cjs');
var classnames = require('classnames');
var React = require('react');
var icons = require('@blueprintjs/icons');
var xlsxRepublish = require('xlsx-republish');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var React__default = /*#__PURE__*/_interopDefault(React);

// src/excel-viewer/BaseExcelViewer.module.css
var BaseExcelViewer_default = {};

// src/excel-viewer/BaseExcelViewer.tsx
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;
}
var SheetTable = /* @__PURE__ */ React__default.default.memo(({
  sheet
}) => {
  const maxCols = React.useMemo(() => sheet.rows.reduce((max, row) => Math.max(max, row.length), 0), [sheet.rows]);
  if (sheet.rows.length === 0) {
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: BaseExcelViewer_default.emptySheet
    }, "Empty sheet");
  }
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseExcelViewer_default.tableContainer
  }, /* @__PURE__ */ React__default.default.createElement("table", {
    className: BaseExcelViewer_default.table
  }, /* @__PURE__ */ React__default.default.createElement("thead", null, /* @__PURE__ */ React__default.default.createElement("tr", null, /* @__PURE__ */ React__default.default.createElement("th", {
    className: BaseExcelViewer_default.cornerCell
  }), Array.from({
    length: maxCols
  }, (_, i) => /* @__PURE__ */ React__default.default.createElement("th", {
    key: i,
    className: BaseExcelViewer_default.columnHeader
  }, columnIndexToLetter(i))))), /* @__PURE__ */ React__default.default.createElement("tbody", null, sheet.rows.map((row, rowIndex) => /* @__PURE__ */ React__default.default.createElement("tr", {
    key: rowIndex
  }, /* @__PURE__ */ React__default.default.createElement("td", {
    className: BaseExcelViewer_default.rowHeader
  }, rowIndex + 1), Array.from({
    length: maxCols
  }, (_, cellIndex) => /* @__PURE__ */ React__default.default.createElement("td", {
    key: cellIndex,
    className: BaseExcelViewer_default.cell
  }, row[cellIndex] ?? "")))))));
});
SheetTable.displayName = "SheetTable";
function BaseExcelViewer({
  spreadsheet,
  className
}) {
  const [activeSheetIndex, setActiveSheetIndex] = React.useState(0);
  const rootClassName = classnames__default.default(BaseExcelViewer_default.container, className);
  const safeIndex = Math.min(activeSheetIndex, Math.max(0, spreadsheet.sheets.length - 1));
  const activeSheet = React.useMemo(() => spreadsheet.sheets[safeIndex], [spreadsheet.sheets, safeIndex]);
  const handleTabClick = React.useCallback((index) => {
    setActiveSheetIndex(index);
  }, []);
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: rootClassName
  }, activeSheet != null ? /* @__PURE__ */ React__default.default.createElement(SheetTable, {
    sheet: activeSheet
  }) : /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseExcelViewer_default.emptySheet
  }, "No sheets"), spreadsheet.sheets.length > 1 && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseExcelViewer_default.tabBar
  }, spreadsheet.sheets.map((sheet, index) => /* @__PURE__ */ React__default.default.createElement("button", {
    key: sheet.name,
    className: classnames__default.default(BaseExcelViewer_default.tab, {
      [BaseExcelViewer_default.tabActive]: index === safeIndex
    }),
    onClick: () => handleTabClick(index),
    type: "button"
  }, sheet.name))));
}
async function parseSpreadsheetFromResponse(response) {
  const buffer = await response.arrayBuffer();
  const workbook = xlsxRepublish.read(buffer, {
    type: "array"
  });
  const sheets = workbook.SheetNames.map((name) => {
    const sheet = workbook.Sheets[name];
    const rows = sheet != null ? xlsxRepublish.utils.sheet_to_json(sheet, {
      header: 1,
      defval: "",
      raw: false
    }) : [];
    return {
      name,
      rows
    };
  });
  return {
    sheets
  };
}

// src/excel-viewer/ExcelViewer.tsx
function _extends() {
  return _extends = Object.assign ? Object.assign.bind() : function(n) {
    for (var e = 1; e < arguments.length; e++) {
      var t = arguments[e];
      for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
    }
    return n;
  }, _extends.apply(null, arguments);
}
function ExcelViewer({
  media,
  className,
  ...excelViewerProps
}) {
  const {
    data: spreadsheet,
    loading,
    error
  } = chunkMVQKV5BJ_cjs.useMediaContents(media, React.useCallback(parseSpreadsheetFromResponse, []));
  const rootClassName = classnames__default.default(BaseExcelViewer_default.container, className);
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: rootClassName
  }, loading && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseExcelViewer_default.loadingContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Spin, {
    className: BaseExcelViewer_default.spinnerIcon
  }), "Loading\u2026"), error != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseExcelViewer_default.errorContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Error, {
    className: BaseExcelViewer_default.errorIcon
  }), "Failed to load spreadsheet: ", error.message), spreadsheet != null && /* @__PURE__ */ React__default.default.createElement(BaseExcelViewer, _extends({
    spreadsheet
  }, excelViewerProps)));
}

exports.BaseExcelViewer = BaseExcelViewer;
exports.ExcelViewer = ExcelViewer;
//# sourceMappingURL=chunk-GKDO6NU7.cjs.map
//# sourceMappingURL=chunk-GKDO6NU7.cjs.map