import { TIPTAP_TABLE_CELL_TYPE, TIPTAP_TABLE_HEADER_CELL_TYPE } from "./../../../ricos-content/consts.js";
import { Plugin, PluginKey, TextSelection } from "./../../../tiptap/pm/state.js";
import { DecorationSet } from "./../../../tiptap/pm/view.js";
import { isInTable } from "../commands/queries.js";
import { TRANSACTION_META_KEYS } from "../consts.js";
import { cellEditingDecorations } from "../decorations/index.js";
import { isRicosCellSelection } from "../utilities/is-selection-type.js";
const cellStatesPlugin = (editor, _services, isMobile) => {
  const key = new PluginKey("cell-states");
  return new Plugin({
    key,
    state: {
      init: () => {
        return {};
      },
      apply(tr, prev, oldState, newState) {
        if (!isInTable(newState)) {
          return {};
        }
        if (isRicosCellSelection(newState.selection)) {
          return {};
        }
        const { pos: editCell } = tr.getMeta(TRANSACTION_META_KEYS.EDITING_CELL) || {};
        const { pos: selectCell } = tr.getMeta(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 === TIPTAP_TABLE_CELL_TYPE || nodeType === TIPTAP_TABLE_HEADER_CELL_TYPE) {
              cellPos = $pos.before(d);
              break;
            }
          }
          if (cellPos === null) {
            return false;
          }
          const { selection } = view.state;
          if (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(
                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 = cellEditingDecorations(tablePluginState.editCell);
          return DecorationSet.create(state.doc, editDecorations);
        }
      }
    }
  });
};
export {
  cellStatesPlugin
};
//# sourceMappingURL=cell-states-plugin.js.map
