import { DataCollection, DataCollectionSmall } from "@wix/wix-ui-icons-common";
import { TIPTAP_TABLE_TYPE } from "./../ricos-content/index.js";
import {
  Node_Type,
  RicosPluginToolbarNames,
  quickActionsModalSections
} from "./../ricos-types/index.js";
import { getToolbarReferenceElement } from "./../_shared/toolbar-reference-element.js";
import { findParentNodeClosestToPos, posToDOMRect } from "./../tiptap/index.js";
import { INSERT_TABLE_MODAL_ADD_BUTTON } from "./data-hooks.js";
import { getAddButtons } from "./get-add-buttons.js";
import { getToolbarButtons } from "./get-toolbar-buttons.js";
import { InsertModal } from "./modals/insert-modal.js";
import { shortcuts } from "./shortcuts.js";
import { tableCellConverter, tableConverter, tableRowConverter } from "./table-converters.js";
import { isSelectingCells } from "./tiptap/plugins/table-editing-plugin.js";
import { tiptapExtensions } from "./tiptap/tiptap.js";
import { isRicosCellSelection } from "./tiptap/utilities/is-selection-type.js";
import { INSERT_TABLE_MODAL_ID, TABLE_TYPE } from "./types.js";
const isTableToolbarVisible = (selection, editor) => {
  const { tiptapEditor } = editor.adapter;
  if (isSelectingCells(tiptapEditor.state)) {
    return false;
  }
  if (!isRicosCellSelection(selection)) {
    return false;
  }
  const closestTable = findParentNodeClosestToPos(
    selection.$from,
    (node) => node.type.name === TIPTAP_TABLE_TYPE
  );
  return !!closestTable;
};
const pluginTable = (config = {}) => {
  return {
    config: { ...config, type: "table" },
    type: TABLE_TYPE,
    pmMarkConverters: [],
    pmNodeConverters: [tableConverter, tableRowConverter, tableCellConverter],
    tiptapExtensions,
    getAddButtons,
    toolbar: {
      names: [
        RicosPluginToolbarNames.TABLE,
        RicosPluginToolbarNames.TABLE_CELL,
        RicosPluginToolbarNames.TABLE_ROW
      ],
      getButtons: getToolbarButtons,
      isVisible: (selection, editor) => isTableToolbarVisible(selection, editor),
      resolveReferenceElement: (tiptapEditor) => getToolbarReferenceElement(tiptapEditor, {
        handleSelector: "[data-table-handle]",
        containerSelector: "[data-table-container]",
        topOffset: -5,
        bottomOffset: 38,
        nodeType: TIPTAP_TABLE_TYPE
      })
    },
    getQuickActions: ({ pluginServices }) => {
      const isMobile = pluginServices?.context.isMobile;
      return [
        {
          id: Node_Type.TABLE,
          title: "QuickActionsPlugin_ActionTitle_Table",
          icon: isMobile ? DataCollection : DataCollectionSmall,
          section: quickActionsModalSections.FormatLayout,
          tags: ["grid", "chart", "data", "spreadsheet", "matrix", "rows and columns"],
          command: ({ modalService, editor }) => {
            modalService.togglePluginPanel({
              id: INSERT_TABLE_MODAL_ID,
              Component: InsertModal,
              componentProps: {},
              isMobile,
              initialFocus: `[data-hook="${INSERT_TABLE_MODAL_ADD_BUTTON}"]`,
              positioning: {
                referenceElement: {
                  getBoundingClientRect: () => posToDOMRect(
                    editor.view,
                    editor.view.state.selection.from,
                    editor.view.state.selection.to
                  )
                },
                placement: isMobile ? "bottom" : "bottom-start"
              },
              mobileLayout: "drawer"
            });
          }
        }
      ];
    },
    shortcuts
  };
};
export {
  pluginTable
};
//# sourceMappingURL=editor.js.map

import { injectCss } from "./viewer-styles-inject.js";
injectCss();
