"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var table_grid_selector_exports = {};
__export(table_grid_selector_exports, {
  TableGridSelector: () => TableGridSelector
});
module.exports = __toCommonJS(table_grid_selector_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_table_grid_selector_scss = __toESM(require("./table-grid-selector-scss.js"));
const MAX_COLS = 8;
const MAX_ROWS = 6;
const TableGridSelector = ({
  colCount,
  rowCount,
  onSelectionChange,
  dataHook
}) => {
  const [hoverCell, setHoverCell] = (0, import_react.useState)(null);
  const isHovering = hoverCell !== null;
  const effectiveCol = hoverCell?.col ?? Math.min(colCount, MAX_COLS);
  const effectiveRow = hoverCell?.row ?? Math.min(rowCount, MAX_ROWS);
  const getCellFromEvent = (0, import_react.useCallback)((e) => {
    const target = e.target;
    const col = target.dataset.col;
    const row = target.dataset.row;
    if (col && row) {
      return { col: Number(col), row: Number(row) };
    }
    return null;
  }, []);
  const handleMouseOver = (0, import_react.useCallback)(
    (e) => {
      const cell = getCellFromEvent(e);
      if (cell) {
        setHoverCell((prev) => prev?.col === cell.col && prev.row === cell.row ? prev : cell);
      }
    },
    [getCellFromEvent]
  );
  const handleClick = (0, import_react.useCallback)(
    (e) => {
      const cell = getCellFromEvent(e);
      if (cell) {
        onSelectionChange(cell.col, cell.row);
      }
    },
    [getCellFromEvent, onSelectionChange]
  );
  const handleMouseLeave = (0, import_react.useCallback)(() => {
    setHoverCell(null);
  }, []);
  const renderCells = () => {
    const cells = [];
    const lockedCol = Math.min(colCount, MAX_COLS);
    const lockedRow = Math.min(rowCount, MAX_ROWS);
    for (let row = 1; row <= MAX_ROWS; row++) {
      for (let col = 1; col <= MAX_COLS; col++) {
        const isInHoverArea = col <= effectiveCol && row <= effectiveRow;
        const isLocked = col <= lockedCol && row <= lockedRow;
        const isHoverPreview = isInHoverArea && !isLocked;
        const showLockedWithBorder = isLocked && (!isHovering || isInHoverArea);
        const showLockedHidden = isLocked && isHovering && !isInHoverArea;
        cells.push(
          /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              key: `${col}-${row}`,
              "data-col": col,
              "data-row": row,
              className: (0, import_classnames.default)(import_table_grid_selector_scss.default.cell, {
                [import_table_grid_selector_scss.default.locked]: showLockedWithBorder,
                [import_table_grid_selector_scss.default.lockedHidden]: showLockedHidden,
                [import_table_grid_selector_scss.default.hoverPreview]: isHoverPreview
              }),
              role: "presentation"
            }
          )
        );
      }
    }
    return cells;
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      "data-hook": dataHook,
      className: import_table_grid_selector_scss.default.gridContainer,
      onMouseOver: handleMouseOver,
      onMouseLeave: handleMouseLeave,
      onClick: handleClick,
      "aria-hidden": "true",
      tabIndex: -1
    },
    renderCells()
  );
};
//# sourceMappingURL=table-grid-selector.js.map
