"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var table_query_exports = {};
__export(table_query_exports, {
  TableQuery: () => TableQuery
});
module.exports = __toCommonJS(table_query_exports);
var import_tablemap = require("./tablemap.js");
var import_find_table = require("./utilities/find-table.js");
var import_is_selection_type = require("./utilities/is-selection-type.js");
class TableQuery {
  static of(selection) {
    const table = (0, import_find_table.findTable)(selection);
    if (!table) {
      return null;
    }
    return new TableQuery(selection, table);
  }
  constructor(selection, table) {
    this.selection = selection;
    this.table = table;
    this.map = import_tablemap.TableMap.get(this.table.node);
  }
  getStartPos() {
    return this.table.start;
  }
  getHeight() {
    return this.map.height;
  }
  getWidth() {
    return this.map.width;
  }
  getMap() {
    return this.map.map;
  }
  getNode() {
    return this.table.node;
  }
  getRowsIndexesInSelection() {
    const rows = [];
    if (this.selection && (0, import_is_selection_type.isRicosCellSelection)(this.selection)) {
      this.selection.forEachCell((node, pos) => {
        const cell = this.map.findCell(pos - this.getStartPos());
        !rows.includes(cell.row) && rows.push(cell.row);
      });
    }
    return rows;
  }
  getColsIndexesInSelection() {
    const cols = [];
    if (this.selection && (0, import_is_selection_type.isRicosCellSelection)(this.selection)) {
      this.selection.forEachCell((node, pos) => {
        const cell = this.map.findCell(pos - this.getStartPos());
        !cols.includes(cell.col) && cols.push(cell.col);
      });
    }
    return cols;
  }
  getSelectionRowRange() {
    if (!this.selection || !(0, import_is_selection_type.isRicosCellSelection)(this.selection)) {
      return null;
    }
    let first = Infinity;
    let last = -1;
    this.selection.forEachCell((node, pos) => {
      const cell = this.map.findCell(pos - this.getStartPos());
      first = Math.min(first, cell.row);
      last = Math.max(last, cell.row + cell.rowspan - 1);
    });
    return first <= last ? { first, last } : null;
  }
  getSelectionColRange() {
    if (!this.selection || !(0, import_is_selection_type.isRicosCellSelection)(this.selection)) {
      return null;
    }
    let first = Infinity;
    let last = -1;
    this.selection.forEachCell((node, pos) => {
      const cell = this.map.findCell(pos - this.getStartPos());
      first = Math.min(first, cell.col);
      last = Math.max(last, cell.col + cell.colspan - 1);
    });
    return first <= last ? { first, last } : null;
  }
  getDomWidthInSelection(view) {
    const cols = this.getColsIndexesInSelection();
    let totalWidth = 0;
    const dom = view.domAtPos(this.getStartPos() - 1);
    const tableNode = dom.node.childNodes[dom.offset].firstChild;
    if (tableNode) {
      const colsgroup = tableNode.firstChild;
      Array.from(colsgroup?.childNodes || []).forEach(
        (col, index) => cols.includes(index) && (totalWidth += col.offsetWidth)
      );
    }
    return totalWidth;
  }
  getCellNeighbors(cellPos) {
    const cellRelativePos = cellPos - this.getStartPos();
    const map = this.getMap();
    const cellIndex = map.indexOf(cellRelativePos);
    const width = this.getWidth();
    const height = this.getHeight();
    const neighbors = {};
    cellIndex % width !== 0 && (neighbors.left = map[cellIndex - 1]);
    (cellIndex + 1) % width !== 0 && (neighbors.right = map[cellIndex + 1]);
    cellIndex >= width && (neighbors.top = map[cellIndex - width]);
    cellIndex < height * width - width && (neighbors.bottom = map[cellIndex + width]);
    return neighbors;
  }
  isRectSelected(rect) {
    const map = this.map;
    const start = this.getStartPos();
    const cells = map.cellsInRect(rect);
    if (this.selection && (0, import_is_selection_type.isRicosCellSelection)(this.selection)) {
      const selectedCells = this.selection.cellPositions;
      return cells.every((cell) => selectedCells.has(cell + start));
    }
    return false;
  }
  isColumnSelected(columnIndex) {
    const map = this.map;
    return this.isRectSelected({
      left: columnIndex,
      right: columnIndex + 1,
      top: 0,
      bottom: map.height
    });
  }
  isRowSelected(rowIndex) {
    const map = this.map;
    return this.isRectSelected({
      left: 0,
      right: map.width,
      top: rowIndex,
      bottom: rowIndex + 1
    });
  }
  isTableSelected() {
    const map = this.map;
    return this.isRectSelected({
      left: 0,
      right: map.width,
      top: 0,
      bottom: map.height
    });
  }
  getRowsHeight(view) {
    const dom = view.domAtPos(this.getStartPos() - 1);
    const tableNode = dom.node.childNodes[dom.offset].firstChild;
    return tableNode ? Array.from(tableNode.querySelectorAll("tbody > tr")).map(
      (node) => node.offsetHeight
    ) : [];
  }
  getTableRect() {
    return {
      map: this.map,
      tableStart: this.getStartPos(),
      table: this.getNode(),
      left: 0,
      right: this.getWidth(),
      top: 0,
      bottom: this.getHeight()
    };
  }
  getColumnWidth(view) {
    const dom = view.domAtPos(this.getStartPos());
    return dom.node?.parentElement ? Array.from(dom.node.parentElement.children[0].children)?.map(
      (node) => node.offsetWidth
    ) : [];
  }
}
//# sourceMappingURL=table-query.js.map
