"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 apply_table_border_change_exports = {};
__export(apply_table_border_change_exports, {
  applyTableBorderChangeToTransaction: () => applyTableBorderChangeToTransaction
});
module.exports = __toCommonJS(apply_table_border_change_exports);
var import_canonical_remap = require("../borders/canonical-remap.js");
var import_edge_detection = require("../borders/edge-detection.js");
var import_table_query = require("../tiptap/table-query.js");
var import_is_selection_type = require("../tiptap/utilities/is-selection-type.js");
const getSelectionContext = (state) => {
  const { selection } = state;
  const table = import_table_query.TableQuery.of(selection);
  if (!table || !(0, import_is_selection_type.isRicosCellSelection)(selection)) {
    return null;
  }
  const tableNode = table.getNode();
  const tableStart = table.getStartPos();
  const { cells, width, height } = (0, import_canonical_remap.extractCellPositions)(tableNode, tableStart);
  const allSelectedCells = [];
  const selectedCellIds = /* @__PURE__ */ new Set();
  selection.forEachCell((node, pos) => {
    allSelectedCells.push({ node, pos });
    if (node.attrs.id) {
      selectedCellIds.add(node.attrs.id);
    }
  });
  const cellsById = new Map(cells.map((cell) => [cell.node.attrs.id, cell]));
  const grid = (0, import_edge_detection.buildCellGrid)(cells, width, height);
  return {
    tableStart,
    width,
    height,
    allSelectedCells,
    cellsById,
    isCellInSelection: (row, col) => {
      const cell = (0, import_edge_detection.getCellAt)(grid, row, col);
      return cell ? selectedCellIds.has(cell.node.attrs.id) : false;
    }
  };
};
const applyEditToCurrentTable = (tr, tableStart, selected, side, style, clear) => {
  const $pos = tr.doc.resolve(tableStart);
  const currentTable = $pos.node($pos.depth);
  if (currentTable?.type.spec.tableRole !== "table") {
    return;
  }
  const liveCells = selected.flatMap(({ pos }) => {
    const node = tr.doc.nodeAt(pos);
    return node ? [{ node, pos }] : [];
  });
  if (liveCells.length === 0) {
    return;
  }
  (0, import_canonical_remap.applyBorderEdit)(tr, currentTable, tableStart, liveCells, side, style, clear);
};
const applyOutsideBorder = (tr, ctx, style, clear) => {
  const grouped = (0, import_edge_detection.groupOuterEdgeCells)(
    ctx.allSelectedCells,
    ({ node }) => ctx.cellsById.get(node.attrs.id),
    ctx.width,
    ctx.height,
    ctx.isCellInSelection
  );
  for (const [side, cells] of grouped) {
    if (cells.length > 0) {
      applyEditToCurrentTable(tr, ctx.tableStart, cells, side, style, clear);
    }
  }
};
const applyBorderBySide = (tr, ctx, side, style, clear) => {
  const outerCells = ctx.allSelectedCells.filter(({ node }) => {
    const cellData = ctx.cellsById.get(node.attrs.id);
    return cellData ? (0, import_edge_detection.isCellOuterEdge)(cellData, side, ctx.width, ctx.height, ctx.isCellInSelection) : false;
  });
  if (outerCells.length > 0) {
    applyEditToCurrentTable(tr, ctx.tableStart, outerCells, side, style, clear);
  }
};
const applyInnerSide = (tr, ctx, side, style, clear) => {
  const innerCells = ctx.allSelectedCells.filter(({ node }) => {
    const cellData = ctx.cellsById.get(node.attrs.id);
    if (!cellData) {
      return false;
    }
    return side === "top" ? cellData.row > 0 && ctx.isCellInSelection(cellData.row - 1, cellData.col) : cellData.col > 0 && ctx.isCellInSelection(cellData.row, cellData.col - 1);
  });
  if (innerCells.length > 0) {
    applyEditToCurrentTable(tr, ctx.tableStart, innerCells, side, style, clear);
  }
};
const applyTableBorderChangeToTransaction = (tr, state, scope, change) => {
  const ctx = getSelectionContext(state);
  if (!ctx) {
    return false;
  }
  const style = change.width !== void 0 ? { width: change.width } : { color: change.color };
  const clear = change.width === void 0 && change.color === void 0;
  switch (scope) {
    case "borders":
      applyEditToCurrentTable(tr, ctx.tableStart, ctx.allSelectedCells, null, style, clear);
      return true;
    case "outsideBorders":
      applyOutsideBorder(tr, ctx, style, clear);
      return true;
    case "top":
    case "right":
    case "bottom":
    case "left":
      applyBorderBySide(tr, ctx, scope, style, clear);
      return true;
    case "inner":
      applyInnerSide(tr, ctx, "top", style, clear);
      applyInnerSide(tr, ctx, "left", style, clear);
      return true;
    case "innerHorizontal":
      applyInnerSide(tr, ctx, "top", style, clear);
      return true;
    case "innerVertical":
      applyInnerSide(tr, ctx, "left", style, clear);
      return true;
    default:
      return false;
  }
};
//# sourceMappingURL=apply-table-border-change.js.map
