"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var spacing_button_exports = {};
__export(spacing_button_exports, {
  SpacingButton: () => SpacingButton,
  createSpacingButton: () => createSpacingButton
});
module.exports = __toCommonJS(spacing_button_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_toolbar_button = require("./../../../_shared/toolbar-button/toolbar-button.js");
var import_consts = require("../../consts.js");
var import_data_hooks = require("../../data-hooks.js");
var import_table_spacing_modal = require("../../modals/table-spacing-modal.js");
var import_capture_table_cell_selection = require("../capture-table-cell-selection.js");
var import_resolvers = require("../resolvers.js");
var import_table_style_preview = require("../table-style-preview.js");
const SpacingButton = ({ dataHook, toolbarItem }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const [referenceElement, setReferenceElement] = (0, import_react.useState)(null);
  return /* @__PURE__ */ import_react.default.createElement(
    import_toolbar_button.ToolbarButton,
    {
      ref: setReferenceElement,
      prefix: import_wix_ui_icons_common.Width,
      onClick: () => {
        toolbarItem.commands?.click({ referenceElement });
      },
      dataHook,
      tooltip: t("TablePlugin_Toolbar_Spacing_Tooltip"),
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
const createSpacingButton = (services) => ({
  id: import_consts.TABLE_BUTTONS.SPACING,
  type: "modal",
  dataHook: import_data_hooks.PLUGIN_TOOLBAR_SPACING_BUTTON,
  icon: import_wix_ui_icons_common.Width,
  tooltip: "TablePlugin_Toolbar_Spacing_Tooltip",
  command: ({ tiptapEditor, modals, attributes, referenceElement }) => {
    const modalId = import_consts.TABLE_BUTTONS_MODALS_ID.SPACING;
    const isModalOpen = modals.isModalOpen(modalId);
    if (isModalOpen) {
      modals.closeModal(modalId);
      return;
    }
    const selectedCellPositions = (0, import_capture_table_cell_selection.captureSelectedCellPositions)(
      tiptapEditor,
      attributes?.selectedCellIds
    );
    const spacingPreview = (0, import_table_style_preview.createTableStylePreview)(tiptapEditor, selectedCellPositions);
    modals.togglePluginPanel({
      id: modalId,
      isMobile: services.context.isMobile,
      Component: import_table_spacing_modal.TableSpacingModal,
      positioning: { placement: "bottom", referenceElement },
      componentProps: {
        referenceElement,
        nodeId: attributes?.selectedNode?.attrs?.id,
        cellSpacing: attributes?.cellSpacing,
        cellPadding: attributes?.cellPadding,
        onCellSpacingPreview: (spacing) => {
          spacingPreview.previewCellSpacing(spacing);
        },
        onCellSpacingCommit: (spacing) => {
          spacingPreview.restore();
          (0, import_capture_table_cell_selection.runWithTableCellSelection)(tiptapEditor, selectedCellPositions, () => {
            tiptapEditor.chain().setCellSpacing(spacing).run();
          });
        },
        onCellPaddingPreview: (padding) => {
          spacingPreview.previewCellPadding(padding);
        },
        onCellPaddingCommit: (padding) => {
          spacingPreview.restore();
          (0, import_capture_table_cell_selection.runWithTableCellSelection)(tiptapEditor, selectedCellPositions, () => {
            tiptapEditor.chain().setCellPadding(padding).run();
          });
        }
      }
    });
  },
  attributes: {
    cellSpacing: import_resolvers.getCellSpacingResolver,
    cellPadding: import_resolvers.getCellPaddingResolver,
    selectedNode: import_resolvers.selectedNodeResolver,
    selectedCellIds: import_resolvers.selectedCellIdsResolver,
    visible: import_resolvers.isTableSelectedResolver
  },
  renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(SpacingButton, { toolbarItem })
});
//# sourceMappingURL=spacing-button.js.map
