import classNames from "classnames";
import React, { useContext, useMemo } from "react";
import { RicosNodeContext, RicosNodeContextProvider } from "./../../ricos-context/index.js";
import { resolveCssColor } from "./../../_shared/resolve-css-color.js";
import { getCornerOffset } from "../borders/resolve-cell-borders.js";
import { useCellBorders, useTableBordersContext } from "../borders/table-borders-context.js";
import { TABLE_PLUGIN_CELL } from "../data-hooks.js";
import styles from "../statics/styles/cell-scss.js";
import { TableMap } from "../tiptap/tablemap.js";
const CellBorderDivs = ({ cellId }) => {
  const borders = useCellBorders(cellId);
  const context = useTableBordersContext();
  if (!borders || !context) {
    return null;
  }
  const { isRTL } = context;
  return /* @__PURE__ */ React.createElement(React.Fragment, null, borders.top && /* @__PURE__ */ React.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: -borders.top.width / 2,
        [isRTL ? "right" : "left"]: getCornerOffset({
          thisZ: borders.top.zIndex,
          perpZ: borders.top.perpStartZ,
          parallelZ: borders.top.parallelStartZ,
          extAmount: borders.top.extendStart,
          isHorizontal: true,
          isStartCorner: true
        }),
        [isRTL ? "left" : "right"]: 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: resolveCssColor(borders.top.color),
        zIndex: borders.top.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.bottom && /* @__PURE__ */ React.createElement(
    "div",
    {
      style: {
        position: "absolute",
        bottom: -borders.bottom.width / 2,
        [isRTL ? "right" : "left"]: getCornerOffset({
          thisZ: borders.bottom.zIndex,
          perpZ: borders.bottom.perpStartZ,
          parallelZ: borders.bottom.parallelStartZ,
          extAmount: borders.bottom.extendStart,
          isHorizontal: true,
          isStartCorner: true
        }),
        [isRTL ? "left" : "right"]: 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: resolveCssColor(borders.bottom.color),
        zIndex: borders.bottom.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.left && /* @__PURE__ */ React.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: getCornerOffset({
          thisZ: borders.left.zIndex,
          perpZ: borders.left.perpStartZ,
          parallelZ: borders.left.parallelStartZ,
          extAmount: borders.left.extendStart,
          isHorizontal: false,
          isStartCorner: true
        }),
        bottom: 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: resolveCssColor(borders.left.color),
        zIndex: borders.left.zIndex,
        pointerEvents: "none"
      }
    }
  ), borders.right && /* @__PURE__ */ React.createElement(
    "div",
    {
      style: {
        position: "absolute",
        top: getCornerOffset({
          thisZ: borders.right.zIndex,
          perpZ: borders.right.perpStartZ,
          parallelZ: borders.right.parallelStartZ,
          extAmount: borders.right.extendStart,
          isHorizontal: false,
          isStartCorner: true
        }),
        bottom: 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: resolveCssColor(borders.right.color),
        zIndex: borders.right.zIndex,
        pointerEvents: "none"
      }
    }
  ));
};
const TableCell = ({ node, nodeIndex, RicosNodesRenderer }) => {
  const { parentNode, parentIndex } = useContext(RicosNodeContext);
  const { cellStyle, colspan, rowspan } = node?.tableCellData || {};
  const visualPosition = 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 = 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 = 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__ */ React.createElement(
    Tag,
    {
      "data-hook": TABLE_PLUGIN_CELL,
      "data-node-id": node.id,
      "data-visual-col": visualPosition.visualCol,
      "data-visual-row": visualPosition.visualRow,
      className: classNames(styles.cell, isHeaderCell && styles.header),
      style,
      scope,
      ...htmlAttrs
    },
    /* @__PURE__ */ React.createElement(CellBorderDivs, { cellId: node.id }),
    /* @__PURE__ */ React.createElement("div", { className: classNames(styles.editor, styles.view) }, /* @__PURE__ */ React.createElement(RicosNodeContextProvider, { parentNode, parentIndex: nodeIndex }, /* @__PURE__ */ React.createElement(RicosNodesRenderer, { nodes: node.nodes ?? [], shouldParagraphApplyTextStyle: true })))
  );
};
const TableCellRenderer = ({ node, ...props }) => {
  return /* @__PURE__ */ React.createElement(TableCell, { key: node.id, node, ...props });
};
var table_cell_default = TableCellRenderer;
export {
  table_cell_default as default
};
//# sourceMappingURL=table-cell.js.map
