"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 capture_table_cell_selection_exports = {};
__export(capture_table_cell_selection_exports, {
  captureSelectedCellPositions: () => captureSelectedCellPositions,
  runWithTableCellSelection: () => runWithTableCellSelection
});
module.exports = __toCommonJS(capture_table_cell_selection_exports);
var import_data_hooks = require("../data-hooks.js");
var import_cellselection = require("../tiptap/plugins/cellselection.js");
var import_find_table = require("../tiptap/utilities/find-table.js");
var import_is_selection_type = require("../tiptap/utilities/is-selection-type.js");
const findCellDomById = (editor, id) => {
  const dom = [...editor.view.dom.querySelectorAll("[data-node-id]")].find(
    (element) => element.getAttribute("data-node-id") === id
  );
  return dom instanceof HTMLElement ? dom : void 0;
};
const getCellElement = (editor, pos, id) => {
  const dom = editor.view.nodeDOM(pos);
  if (dom instanceof HTMLElement) {
    return dom;
  }
  return id ? findCellDomById(editor, id) : void 0;
};
const captureCellsById = (editor, selectedCellIds) => {
  if (!selectedCellIds?.length) {
    return null;
  }
  const selectedIds = new Set(selectedCellIds);
  const tableInfo = (0, import_find_table.findTable)(editor.state.selection);
  const cells = [];
  const visit = (node, basePos) => {
    node.descendants((child, offset) => {
      const id = child.attrs.id;
      if (id && selectedIds.has(id)) {
        cells.push({
          element: getCellElement(editor, basePos + offset, id),
          id,
          pos: basePos + offset
        });
      }
    });
  };
  if (tableInfo) {
    visit(tableInfo.node, tableInfo.start);
  } else {
    visit(editor.state.doc, 0);
  }
  return cells.length > 0 ? cells : null;
};
const captureCellsFromDom = (editor) => {
  const { view } = editor;
  const cells = [];
  for (const element of view.dom.querySelectorAll(`[${import_data_hooks.DATA_CELL_SELECTED}="true"]`)) {
    if (!(element instanceof HTMLElement) || !view.dom.contains(element)) {
      continue;
    }
    try {
      cells.push({
        element,
        id: element.getAttribute("data-node-id") ?? void 0,
        pos: view.posAtDOM(element, 0)
      });
    } catch {
    }
  }
  return cells.length > 0 ? cells : null;
};
const captureSelectedCellPositions = (editor, selectedCellIds) => {
  const { selection } = editor.state;
  if ((0, import_is_selection_type.isRicosCellSelection)(selection)) {
    const cells = [];
    selection.forEachCell((node, pos) => {
      cells.push({ element: getCellElement(editor, pos, node.attrs.id), id: node.attrs.id, pos });
    });
    if (cells.length > 0) {
      return cells;
    }
  }
  return captureCellsFromDom(editor) ?? captureCellsById(editor, selectedCellIds);
};
const runWithTableCellSelection = (editor, capturedCells, run) => {
  if (!(0, import_is_selection_type.isRicosCellSelection)(editor.state.selection) && capturedCells?.length) {
    const { state, view } = editor;
    try {
      const cells = capturedCells.map(({ pos }) => state.doc.resolve(pos));
      view.dispatch(
        state.tr.setSelection(new import_cellselection.RicosCellSelection(cells, state.doc)).setMeta("addToHistory", false)
      );
    } catch {
    }
  }
  run();
};
//# sourceMappingURL=capture-table-cell-selection.js.map
