"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_cell_exports = {};
__export(table_cell_exports, {
  default: () => table_cell_default
});
module.exports = __toCommonJS(table_cell_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_resolve_css_color = require("./../../_shared/resolve-css-color.js");
var import_resolve_cell_borders = require("../borders/resolve-cell-borders.js");
var import_table_borders_context = require("../borders/table-borders-context.js");
var import_data_hooks = require("../data-hooks.js");
var import_cell_scss = __toESM(require("../statics/styles/cell-scss.js"));
var import_tablemap = require("../tiptap/tablemap.js");
const CellBorderDivs = ({ cellId }) => {
  const borders = (0, import_table_borders_context.useCellBorders)(cellId);
  const context = (0, import_table_borders_context.useTableBordersContext)();
  if (!borders || !context) {
    return null;
  }
  const { isRTL } = context;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, borders.top && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: -borders.top.width / 2,
        [isRTL ? "right" : "left"]: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.top.zIndex,
          perpZ: borders.top.perpStartZ,
          parallelZ: borders.top.parallelStartZ,
          extAmount: borders.top.extendStart,
          isHorizontal: true,
          isStartCorner: true
        }),
        [isRTL ? "left" : "right"]: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.top.zIndex,
          perpZ: borders.top.perpEndZ,
          parallelZ: borders.top.parallelEndZ,
          extAmount: borders.top.extendEnd,
          isHorizontal: true,
          isStartCorner: false
        }),
        height: borders.top.width,
        backgroundColor: (0, import_resolve_css_color.resolveCssColor)(borders.top.color),
        zIndex: borders.top.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.bottom && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        position: "absolute",
        bottom: -borders.bottom.width / 2,
        [isRTL ? "right" : "left"]: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.bottom.zIndex,
          perpZ: borders.bottom.perpStartZ,
          parallelZ: borders.bottom.parallelStartZ,
          extAmount: borders.bottom.extendStart,
          isHorizontal: true,
          isStartCorner: true
        }),
        [isRTL ? "left" : "right"]: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.bottom.zIndex,
          perpZ: borders.bottom.perpEndZ,
          parallelZ: borders.bottom.parallelEndZ,
          extAmount: borders.bottom.extendEnd,
          isHorizontal: true,
          isStartCorner: false
        }),
        height: borders.bottom.width,
        backgroundColor: (0, import_resolve_css_color.resolveCssColor)(borders.bottom.color),
        zIndex: borders.bottom.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.left && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.left.zIndex,
          perpZ: borders.left.perpStartZ,
          parallelZ: borders.left.parallelStartZ,
          extAmount: borders.left.extendStart,
          isHorizontal: false,
          isStartCorner: true
        }),
        bottom: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.left.zIndex,
          perpZ: borders.left.perpEndZ,
          parallelZ: borders.left.parallelEndZ,
          extAmount: borders.left.extendEnd,
          isHorizontal: false,
          isStartCorner: false
        }),
        [isRTL ? "right" : "left"]: -borders.left.width / 2,
        width: borders.left.width,
        backgroundColor: (0, import_resolve_css_color.resolveCssColor)(borders.left.color),
        zIndex: borders.left.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.right && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.right.zIndex,
          perpZ: borders.right.perpStartZ,
          parallelZ: borders.right.parallelStartZ,
          extAmount: borders.right.extendStart,
          isHorizontal: false,
          isStartCorner: true
        }),
        bottom: (0, import_resolve_cell_borders.getCornerOffset)({
          thisZ: borders.right.zIndex,
          perpZ: borders.right.perpEndZ,
          parallelZ: borders.right.parallelEndZ,
          extAmount: borders.right.extendEnd,
          isHorizontal: false,
          isStartCorner: false
        }),
        [isRTL ? "left" : "right"]: -borders.right.width / 2,
        width: borders.right.width,
        backgroundColor: (0, import_resolve_css_color.resolveCssColor)(borders.right.color),
        zIndex: borders.right.zIndex,
        pointerEvents: "none"
      }
    }
  ));
};
const TableCell = ({ node, nodeIndex, RicosNodesRenderer }) => {
  const { parentNode, parentIndex } = (0, import_react.useContext)(import_ricos_context.RicosNodeContext);
  const { cellStyle, colspan, rowspan } = node?.tableCellData || {};
  const visualPosition = (0, import_react.useMemo)(() => {
    if (!parentNode || parentIndex === void 0) {
      return { visualCol: 0, visualRow: 0 };
    }
    const rows = parentNode.nodes || [];
    const grid = rows.map(
      (row) => (row.nodes || []).map((cell) => ({
        colspan: cell.tableCellData?.colspan || 1,
        rowspan: cell.tableCellData?.rowspan || 1
      }))
    );
    const map = import_tablemap.TableMap.fromGrid(grid);
    let cellIndex = 0;
    for (let r = 0; r < parentIndex; r++) {
      cellIndex += grid[r].length;
    }
    cellIndex += nodeIndex;
    const placement = map.findCell(cellIndex);
    return { visualCol: placement.col, visualRow: placement.row };
  }, [parentNode, parentIndex, nodeIndex, colspan]);
  const isRowHeader = parentIndex === 0 && !!parentNode?.tableData?.rowHeader;
  const isColumnHeader = visualPosition.visualCol === 0 && !!parentNode?.tableData?.columnHeader;
  const isHeaderCell = isRowHeader || isColumnHeader;
  const Tag = isHeaderCell ? "th" : "td";
  const scope = isRowHeader ? "col" : isColumnHeader ? "row" : void 0;
  const style = {};
  cellStyle?.backgroundColor && (style.backgroundColor = (0, import_resolve_css_color.resolveCssColor)(cellStyle.backgroundColor));
  cellStyle?.verticalAlignment && (style.verticalAlign = cellStyle.verticalAlignment.toLowerCase());
  const htmlAttrs = {};
  if (colspan && colspan > 1) {
    htmlAttrs.colSpan = colspan;
  }
  if (rowspan && rowspan > 1) {
    htmlAttrs.rowSpan = rowspan;
  }
  return /* @__PURE__ */ import_react.default.createElement(
    Tag,
    {
      "data-hook": import_data_hooks.TABLE_PLUGIN_CELL,
      "data-node-id": node.id,
      "data-visual-col": visualPosition.visualCol,
      "data-visual-row": visualPosition.visualRow,
      className: (0, import_classnames.default)(import_cell_scss.default.cell, isHeaderCell && import_cell_scss.default.header),
      style,
      scope,
      ...htmlAttrs
    },
    /* @__PURE__ */ import_react.default.createElement(CellBorderDivs, { cellId: node.id }),
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(import_cell_scss.default.editor, import_cell_scss.default.view) }, /* @__PURE__ */ import_react.default.createElement(import_ricos_context.RicosNodeContextProvider, { parentNode, parentIndex: nodeIndex }, /* @__PURE__ */ import_react.default.createElement(RicosNodesRenderer, { nodes: node.nodes ?? [], shouldParagraphApplyTextStyle: true })))
  );
};
const TableCellRenderer = ({ node, ...props }) => {
  return /* @__PURE__ */ import_react.default.createElement(TableCell, { key: node.id, node, ...props });
};
var table_cell_default = TableCellRenderer;
//# sourceMappingURL=table-cell.js.map
