"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 get_toolbar_buttons_exports = {};
__export(get_toolbar_buttons_exports, {
  getToolbarButtons: () => getToolbarButtons
});
module.exports = __toCommonJS(get_toolbar_buttons_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../ricos-content/index.js");
var import_ricos_context = require("./../ricos-context/index.js");
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_colors = require("./../_shared/colors.js");
var import_data_hooks = require("./../_shared/data-hooks.js");
var import_banner_icon = require("./../_shared/icons/banner-icon.js");
var import_footer_icon = require("./../_shared/icons/footer-icon.js");
var import_create_delete_button = require("./../_shared/plugin-toolbar/create-delete-button.js");
var import_create_link_button = require("./../_shared/plugin-toolbar/create-link-button.js");
var import_create_node_alignment_button = require("./../_shared/plugin-toolbar/create-node-alignment-button.js");
var import_create_replace_button = require("./../_shared/plugin-toolbar/create-replace-button.js");
var import_create_settings_button = require("./../_shared/plugin-toolbar/create-settings-button.js");
var import_create_node_size_button = require("./../_shared/plugin-toolbar/node-size-button/create-node-size-button.js");
var import_create_separator = require("./../_shared/plugin-toolbar/separator/create-separator.js");
var import_can_set_as_banner = require("./../_shared/resolvers/can-set-as-banner.js");
var import_can_set_as_footer = require("./../_shared/resolvers/can-set-as-footer.js");
var import_get_node_in_selection = require("./../_shared/resolvers/get-node-in-selection.js");
var import_toolbar_button = require("./../_shared/toolbar-button/toolbar-button.js");
var import_toolbars_v3 = require("./../toolbars-v3/index.js");
var import_helpers = require("./../tiptap-editor/helpers/index.js");
var import_shared_with_viewer = require("../ricos-common-plugins/layout/shared-with-viewer.js");
var import_shared = require("../ricos-common-plugins/layout/shared.js");
var import_consts = require("./consts.js");
var import_data_hooks2 = require("./data-hooks.js");
var import_handle_image_upload = require("./handle-image-upload.js");
var import_image_editor_modal = require("./image-editor-modal/image-editor-modal.js");
var import_settings = require("./settings/settings.js");
var import_image_editor_button = require("./toolbar/image-editor-button.js");
const createImageBannerLayoutContent = (position, backdropImage) => ({
  type: import_ricos_content.TIPTAP_LAYOUT_TYPE,
  attrs: {
    containerData: {},
    gap: import_shared_with_viewer.LAYOUT_DEFAULTS.gap,
    cellPadding: [...import_shared_with_viewer.LAYOUT_DEFAULTS.cellPadding],
    responsivenessBehaviour: import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBehaviour,
    responsivenessBreakpoint: import_shared_with_viewer.LAYOUT_DEFAULTS.responsivenessBreakpoint,
    designTarget: import_shared_with_viewer.LAYOUT_DEFAULTS.designTarget,
    id: (0, import_ricos_content.generateId)(),
    banner: { position, origin: "IMAGE" },
    backdropImage,
    backdropPaddingTop: import_shared.DEFAULT_BANNER_VERTICAL_SPACING,
    backdropPaddingBottom: import_shared.DEFAULT_BANNER_VERTICAL_SPACING,
    backgroundColor: import_colors.TRANSPARENT_HEX_COLOR,
    backdropColor: import_colors.TRANSPARENT_HEX_COLOR
  },
  content: [
    {
      type: import_ricos_content.TIPTAP_LAYOUT_CELL_TYPE,
      attrs: {
        id: (0, import_ricos_content.generateId)(),
        colSpan: 12
      },
      content: [
        {
          type: import_v1.Node_Type.PARAGRAPH,
          attrs: {
            id: (0, import_ricos_content.generateId)(),
            indentation: null,
            level: null,
            textStyle: null,
            style: null
          },
          content: []
        }
      ]
    }
  ]
});
const replaceFigureWithBannerLayout = ({
  tiptapEditor,
  figureId,
  cardId,
  position,
  layoutContent
}) => {
  const wrapperType = position === "TOP" ? import_ricos_content.TIPTAP_CARD_HEADER_TYPE : import_ricos_content.TIPTAP_CARD_FOOTER_TYPE;
  return tiptapEditor.chain().focus().deleteNode(figureId).command(({ tr, commands }) => {
    if (!cardId) {
      const insertPos = position === "TOP" ? 0 : tr.doc.content.size;
      return commands.insertContentAt(insertPos, layoutContent);
    }
    const card = (0, import_helpers.findNodeById)(tr, cardId)[0];
    if (!card) {
      return false;
    }
    let wrapperNode;
    let wrapperPos = -1;
    card.node.forEach((child, offset) => {
      if (child.type.name === wrapperType && !wrapperNode) {
        wrapperNode = child;
        wrapperPos = card.pos + 1 + offset;
      }
    });
    const newWrapper = tr.doc.type.schema.nodeFromJSON({
      type: wrapperType,
      content: [layoutContent]
    });
    if (wrapperNode) {
      tr.replaceWith(wrapperPos, wrapperPos + wrapperNode.nodeSize, newWrapper);
    } else if (position === "TOP") {
      tr.insert(card.pos + 1, newWrapper);
    } else {
      tr.insert(card.pos + card.node.nodeSize - 1, newWrapper);
    }
    return true;
  }).run();
};
const getToolbarButtons = (config, services) => {
  const {
    modals,
    context: { isMobile }
  } = services;
  const settings = (0, import_create_settings_button.createSettingsButton)({
    modalId: import_consts.IMAGE_SETTINGS_MODAL_ID,
    showLabel: true,
    command: ({ referenceElement, attributes: { selectedNode } }) => {
      modals?.togglePluginPanel({
        id: import_consts.IMAGE_SETTINGS_MODAL_ID,
        Component: import_settings.Settings,
        componentProps: {
          referenceElement,
          nodeId: selectedNode.attrs.id,
          config
        },
        positioning: {
          referenceElement: referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
          placement: "right-start"
        },
        onClickOutside: {
          on: "pointerup",
          handler: import_lodash.default.noop
        },
        focusReturnElement: referenceElement,
        isMobile: services.context.isMobile
      });
    }
  });
  const separator = (0, import_create_separator.createSeparator)({
    visible: getIsEditImageVisibleResolver(config, services, isMobile)
  });
  const nodeSize = (0, import_create_node_size_button.createNodeSizeButton)({
    services,
    modalId: import_consts.IMAGE_BUTTONS.size,
    options: ["SMALL", "CONTENT", "FULL_WIDTH", "ORIGINAL"]
  });
  const nodeAlignment = (0, import_create_node_alignment_button.createNodeAlignmentButton)({ services, modalId: import_consts.IMAGE_BUTTONS.alignment });
  const link = (0, import_create_link_button.createLinkButton)(services);
  const replace = (0, import_create_replace_button.createReplaceButton)({
    tooltip: "ReplaceImageButton_Tooltip",
    command: ({ attributes: { selectedNode }, publicCommands, referenceElement }) => {
      (0, import_handle_image_upload.handleImageUpload)({
        services,
        config,
        publicCommands,
        nodeId: selectedNode.attrs.id,
        referenceElement
      });
    }
  });
  const imageEditor = {
    id: import_consts.PLUGIN_TOOLBAR_BUTTON_ID_IMAGE_EDITOR,
    type: "modal",
    dataHook: import_data_hooks2.PLUGIN_TOOLBAR_IMAGE_EDITOR_BUTTON,
    icon: import_wix_ui_icons_common.Edit,
    label: "ImageEditorButton_Label",
    tooltip: "ImageEditorButton_Tooltip",
    command: ({ attributes: { selectedNode } }) => {
      modals?.openModal({
        Component: import_image_editor_modal.ImageEditorModal,
        id: import_consts.IMAGE_EDITOR_MODAL_ID,
        componentProps: {
          nodeId: selectedNode.attrs.id,
          src: selectedNode.attrs?.image?.src?.id,
          config
        },
        positioning: { placement: "top" },
        layout: "fullscreen"
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_image_editor_button.ImageEditorButton, { toolbarItem }),
    attributes: {
      visible: getIsEditImageVisibleResolver(config, services, isMobile),
      selectedNode: import_get_node_in_selection.getNodeInSelection
    }
  };
  const deleteButton = (0, import_create_delete_button.createDeleteButton)({
    command: (data) => {
      const figures = data.tiptapEditor.$nodes(import_ricos_content.TIPTAP_FIGURE_TYPE);
      const selectedFigure = figures?.find((f) => {
        return f.firstChild?.node.attrs.id === data.attributes.selectedNode.attrs.id;
      });
      if (selectedFigure) {
        data.tiptapEditor.chain().focus().deleteNode(selectedFigure.attributes.id).run();
      }
    }
  });
  const setAsHeader = {
    id: import_consts.PLUGIN_TOOLBAR_BUTTON_ID_SET_AS_BANNER,
    type: "toggle",
    icon: import_banner_icon.BannerIcon,
    tooltip: "Layouts_LayoutSetAsBanner_Tooltip",
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      visible: import_can_set_as_banner.canSetAsBanner
    },
    dataHook: import_data_hooks.PLUGIN_TOOLBAR_SET_AS_HEADER_BUTTON,
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(ImageSetAsHeaderButton, { toolbarItem }),
    command: ({ tiptapEditor, attributes: { selectedNode } }) => {
      const imageData = selectedNode.attrs.image;
      if (!imageData) {
        return;
      }
      const figures = tiptapEditor.$nodes(import_ricos_content.TIPTAP_FIGURE_TYPE);
      const selectedFigure = figures?.find((f) => {
        return f.firstChild?.node.attrs.id === selectedNode.attrs.id;
      });
      if (!selectedFigure) {
        return;
      }
      const backdropImage = {
        media: {
          src: imageData.src,
          width: imageData.width,
          height: imageData.height
        },
        opacity: 100,
        scaling: "COVER",
        position: "CENTER"
      };
      if (backdropImage.media?.src?.id) {
        const backgroundImage = new Image();
        backgroundImage.src = (0, import_shared.getImageUrl)(backdropImage.media);
      }
      const layoutContent = createImageBannerLayoutContent("TOP", backdropImage);
      const cardId = (0, import_shared.findParentCardId)(tiptapEditor.state.doc, selectedFigure.pos);
      replaceFigureWithBannerLayout({
        tiptapEditor,
        figureId: selectedFigure.attributes.id,
        cardId,
        position: "TOP",
        layoutContent
      });
    }
  };
  const setAsFooter = {
    id: import_consts.PLUGIN_TOOLBAR_BUTTON_ID_SET_AS_FOOTER,
    type: "toggle",
    icon: import_footer_icon.FooterIcon,
    tooltip: "Layouts_LayoutSetAsFooter_Tooltip",
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      visible: import_can_set_as_footer.canSetAsFooter
    },
    dataHook: import_data_hooks.PLUGIN_TOOLBAR_SET_AS_FOOTER_BUTTON,
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(ImageSetAsFooterButton, { toolbarItem }),
    command: ({ tiptapEditor, attributes: { selectedNode } }) => {
      const imageData = selectedNode.attrs.image;
      if (!imageData) {
        return;
      }
      const figures = tiptapEditor.$nodes(import_ricos_content.TIPTAP_FIGURE_TYPE);
      const selectedFigure = figures?.find((f) => {
        return f.firstChild?.node.attrs.id === selectedNode.attrs.id;
      });
      if (!selectedFigure) {
        return;
      }
      const backdropImage = {
        media: {
          src: imageData.src,
          width: imageData.width,
          height: imageData.height
        },
        opacity: 100,
        scaling: "COVER",
        position: "CENTER"
      };
      if (backdropImage.media?.src?.id) {
        const backgroundImage = new Image();
        backgroundImage.src = (0, import_shared.getImageUrl)(backdropImage.media);
      }
      const layoutContent = createImageBannerLayoutContent("BOTTOM", backdropImage);
      const cardId = (0, import_shared.findParentCardId)(tiptapEditor.state.doc, selectedFigure.pos);
      replaceFigureWithBannerLayout({
        tiptapEditor,
        figureId: selectedFigure.attributes.id,
        cardId,
        position: "BOTTOM",
        layoutContent
      });
    }
  };
  if (services.context.environment === "wixel") {
    return [
      settings,
      setAsHeader,
      setAsFooter,
      link,
      replace,
      nodeSize,
      nodeAlignment,
      deleteButton
    ];
  }
  return [
    settings,
    separator,
    setAsHeader,
    setAsFooter,
    nodeSize,
    nodeAlignment,
    link,
    replace,
    imageEditor,
    separator,
    deleteButton
  ];
};
const getIsEditImageVisibleResolver = (config, services, isMobile) => () => config.imageEditor && Boolean(services.wixInstance()?.uid) && !isMobile;
const ImageSetAsHeaderButton = ({ toolbarItem }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext) || {};
  const onClick = toolbarItem.commands.click;
  const tooltip = toolbarItem.presentation.tooltip;
  const tooltipKey = typeof tooltip === "function" ? tooltip({ isActive: false }) : tooltip;
  return /* @__PURE__ */ import_react.default.createElement(
    import_toolbar_button.ToolbarButton,
    {
      prefix: import_banner_icon.BannerIcon,
      onClick: () => onClick?.(),
      dataHook: import_data_hooks.PLUGIN_TOOLBAR_SET_AS_HEADER_BUTTON,
      label: t(tooltipKey),
      tooltip: t(tooltipKey),
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
const ImageSetAsFooterButton = ({ toolbarItem }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext) || {};
  const onClick = toolbarItem.commands.click;
  const tooltip = toolbarItem.presentation.tooltip;
  const tooltipKey = typeof tooltip === "function" ? tooltip({ isActive: false }) : tooltip;
  return /* @__PURE__ */ import_react.default.createElement(
    import_toolbar_button.ToolbarButton,
    {
      prefix: import_footer_icon.FooterIcon,
      onClick: () => onClick?.(),
      dataHook: import_data_hooks.PLUGIN_TOOLBAR_SET_AS_FOOTER_BUTTON,
      label: t(tooltipKey),
      tooltip: t(tooltipKey),
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
//# sourceMappingURL=get-toolbar-buttons.js.map
