"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 cell_states_plugin_exports = {};
__export(cell_states_plugin_exports, {
  cellStatesPlugin: () => cellStatesPlugin
});
module.exports = __toCommonJS(cell_states_plugin_exports);
var import_consts = require("./../../../ricos-content/consts.js");
var import_state = require("./../../../tiptap/pm/state.js");
var import_view = require("./../../../tiptap/pm/view.js");
var import_queries = require("../commands/queries.js");
var import_consts2 = require("../consts.js");
var import_decorations = require("../decorations/index.js");
var import_is_selection_type = require("../utilities/is-selection-type.js");
const cellStatesPlugin = (editor, _services, isMobile) => {
  const key = new import_state.PluginKey("cell-states");
  return new import_state.Plugin({
    key,
    state: {
      init: () => {
        return {};
      },
      apply(tr, prev, oldState, newState) {
        if (!(0, import_queries.isInTable)(newState)) {
          return {};
        }
        if ((0, import_is_selection_type.isRicosCellSelection)(newState.selection)) {
          return {};
        }
        const { pos: editCell } = tr.getMeta(import_consts2.TRANSACTION_META_KEYS.EDITING_CELL) || {};
        const { pos: selectCell } = tr.getMeta(import_consts2.TRANSACTION_META_KEYS.SELECTED_CELL) || {};
        if (newState.selection.ranges.length > 1) {
          return {};
        } else if (selectCell) {
          return {
            selectCell
          };
        } else if (editCell) {
          return {
            editCell
          };
        }
        return prev;
      }
    },
    props: {
      handleDOMEvents: {
        mousedown: (view, event) => {
          if (isMobile || event.button !== 0) {
            return false;
          }
          let target = event.target;
          const clickTarget = event.target;
          let isClickOnBounds = false;
          let cellElement = null;
          while (target && target !== view.dom) {
            const tagName = target.tagName;
            if (tagName === "TD" || tagName === "TH") {
              cellElement = target;
              if (clickTarget === target) {
                isClickOnBounds = true;
              } else if (clickTarget.tagName === "DIV" && clickTarget.parentElement === target) {
                isClickOnBounds = true;
              } else {
                const isNodeView = clickTarget.hasAttribute("data-node-view-wrapper") || clickTarget.classList.contains("node-view-wrapper") || clickTarget.hasAttribute("data-node-view-content");
                if (isNodeView) {
                  isClickOnBounds = true;
                }
              }
              break;
            }
            target = target.parentElement;
          }
          if (!cellElement) {
            return false;
          }
          if (!isClickOnBounds) {
            return false;
          }
          const pos = view.posAtDOM(cellElement, 0);
          if (pos === null || pos === void 0) {
            return false;
          }
          let cellPos = null;
          const $pos = view.state.doc.resolve(pos);
          for (let d = $pos.depth; d > 0; d--) {
            const nodeType = $pos.node(d).type.name;
            if (nodeType === import_consts.TIPTAP_TABLE_CELL_TYPE || nodeType === import_consts.TIPTAP_TABLE_HEADER_CELL_TYPE) {
              cellPos = $pos.before(d);
              break;
            }
          }
          if (cellPos === null) {
            return false;
          }
          const { selection } = view.state;
          if ((0, import_is_selection_type.isRicosCellSelection)(selection)) {
            const isSingleCell = selection.$anchorCell.pos === selection.$headCell.pos;
            if (isSingleCell && selection.$anchorCell.pos === cellPos) {
              event.preventDefault();
              const cellNode = view.state.doc.nodeAt(cellPos);
              if (!cellNode) {
                return false;
              }
              const cellContentEnd = cellPos + cellNode.nodeSize - 1;
              const tr = view.state.tr.setSelection(
                import_state.TextSelection.near(view.state.doc.resolve(cellContentEnd), -1)
              );
              view.dispatch(tr);
              view.focus();
              return true;
            }
          }
          event.preventDefault();
          editor.commands.setCellSelection(cellPos);
          editor.view.focus();
          return true;
        }
      },
      decorations(state) {
        const tablePluginState = state && key.getState(state);
        if (tablePluginState.editCell) {
          const editDecorations = (0, import_decorations.cellEditingDecorations)(tablePluginState.editCell);
          return import_view.DecorationSet.create(state.doc, editDecorations);
        }
      }
    }
  });
};
//# sourceMappingURL=cell-states-plugin.js.map
