import { Link, Unlink } from "@wix/wix-ui-icons-common";
import lodash from "lodash";
import React from "react";
import { TIPTAP_EMBED_TYPE, TIPTAP_LINK_PREVIEW_TYPE } from "./../ricos-content/index.js";
import { Node_Type } from "./../ricos-types/index.js";
import { createDeleteButton } from "./../_shared/plugin-toolbar/create-delete-button.js";
import { createNodeAlignmentButton } from "./../_shared/plugin-toolbar/create-node-alignment-button.js";
import { createSettingsButton } from "./../_shared/plugin-toolbar/create-settings-button.js";
import { createNodeSizeButton } from "./../_shared/plugin-toolbar/node-size-button/create-node-size-button.js";
import { createSeparator } from "./../_shared/plugin-toolbar/separator/create-separator.js";
import { createUrlLinkButton } from "./../_shared/plugin-toolbar/url-link-button/create-url-link-button.js";
import { getLinkPreviewLinkData } from "./../_shared/resolvers/get-link-prieview-link-data.js";
import { getNodeInSelection } from "./../_shared/resolvers/get-node-in-selection.js";
import { isEmbedInSelection } from "./../_shared/resolvers/is-embed-in-selection.js";
import { isLinkInLinkPreview } from "./../_shared/resolvers/is-link-in-link-preview.js";
import { isLinkPreviewInSelection } from "./../_shared/resolvers/is-link-preview-in-selection.js";
import { FLOATING_PLUGIN_TOOLBAR } from "./../toolbars-v3/ricos-toolbars/index.js";
import { linkPreviewModals } from "./consts.js";
import {
  PLUGIN_TOOLBAR_REPLACE_TO_LINK_BUTTON,
  PLUGIN_TOOLBAR_UNLINK_BUTTON
} from "./data-hooks.js";
import { SettingsModal } from "./modals/settings-modal.js";
import { DisplayAsUrlButton } from "./toolbar/disply-as-url-button.js";
import { UnlinkButton } from "./toolbar/unlink-button.js";
const isLinkPreviewSelectedResolver = (content) => {
  if (Array.isArray(content) && content.length > 0) {
    return content[0].type?.name === TIPTAP_LINK_PREVIEW_TYPE;
  }
  return false;
};
const getToolbarButtons = (config, services) => {
  const { modals } = services;
  const urlLinkButton = createUrlLinkButton({
    services,
    attributes: {
      selectedUrlLinkData: getLinkPreviewLinkData,
      visible: isLinkInLinkPreview,
      selectedNode: getNodeInSelection
    }
  });
  const separator1 = createSeparator({
    visible: isLinkInLinkPreview
  });
  const displayAsUrlButton = {
    id: `${Node_Type.LINK_PREVIEW}.replace`,
    type: "toggle",
    dataHook: PLUGIN_TOOLBAR_REPLACE_TO_LINK_BUTTON,
    icon: Link,
    tooltip: "LinkPreview_RemovePreview_Tooltip",
    command: ({ tiptapEditor, attributes: { selectedNode } }) => {
      const link = selectedNode.type.name === TIPTAP_EMBED_TYPE ? selectedNode.attrs.src : selectedNode.attrs.link.url;
      tiptapEditor.chain().focus().setMeta("allowTextReplace", true).insertContent(link + " ").run();
    },
    attributes: {
      selectedNode: getNodeInSelection
    },
    renderer: (toolbarItem) => /* @__PURE__ */ React.createElement(DisplayAsUrlButton, { toolbarItem })
  };
  const sizeButton = createNodeSizeButton({
    services,
    modalId: `${Node_Type.LINK_PREVIEW}.size`,
    options: ["SMALL", "CONTENT", "FULL_WIDTH"]
  });
  const alignmentButton = createNodeAlignmentButton({
    services,
    modalId: `${Node_Type.LINK_PREVIEW}.alignment`
  });
  const settingsButton = createSettingsButton({
    modalId: linkPreviewModals.settings,
    command: ({ referenceElement, attributes: { selectedNode } }) => {
      const isModalOpen = modals.isModalOpen(linkPreviewModals.settings);
      isModalOpen ? modals.closeModal(linkPreviewModals.settings) : modals.togglePluginPanel({
        id: linkPreviewModals.settings,
        Component: SettingsModal,
        componentProps: {
          referenceElement,
          nodeId: selectedNode.attrs.id,
          activeTab: "settings",
          config
        },
        positioning: {
          referenceElement: referenceElement?.closest(`[data-hook~=${FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
          placement: "right-start"
        },
        onClickOutside: {
          on: "pointerup",
          handler: lodash.noop
        },
        focusReturnElement: referenceElement,
        isMobile: services.context.isMobile
      });
    },
    attributes: { visible: isLinkPreviewSelectedResolver }
  });
  const separator2 = createSeparator({
    visible: isLinkPreviewInSelection
  });
  const unlinkButton = {
    id: `${Node_Type.LINK_PREVIEW}.unlink`,
    type: "toggle",
    dataHook: PLUGIN_TOOLBAR_UNLINK_BUTTON,
    tooltip: "LinkPreview_Unlink_Tooltip",
    icon: Unlink,
    command: ({ tiptapEditor, attributes: { selectedNode } }) => {
      const link = selectedNode.type.name === TIPTAP_EMBED_TYPE ? selectedNode.attrs.src : selectedNode.attrs.link.url;
      const { state } = tiptapEditor;
      const { selection } = state;
      const from = selection.from;
      tiptapEditor.chain().focus().setMeta("allowTextReplace", true).insertContent(link + " ").setTextSelection({ from, to: from + link.length + 1 }).run();
      tiptapEditor.chain().unsetLink().run();
    },
    attributes: {
      selectedNode: getNodeInSelection,
      visible: isLinkPreviewInSelection
    },
    renderer: (toolbarItem) => /* @__PURE__ */ React.createElement(UnlinkButton, { toolbarItem })
  };
  const deleteButton = createDeleteButton({
    attributes: { visible: isEmbedInSelection }
  });
  return [
    urlLinkButton,
    separator1,
    displayAsUrlButton,
    sizeButton,
    alignmentButton,
    settingsButton,
    separator2,
    unlinkButton,
    deleteButton
  ];
};
export {
  getToolbarButtons
};
//# sourceMappingURL=get-toolbar-buttons.js.map
