"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 editor_exports = {};
__export(editor_exports, {
  pluginLink: () => pluginLink
});
module.exports = __toCommonJS(editor_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../ricos-content/index.js");
var import_ricos_types = require("./../ricos-types/index.js");
var import_open_link_modal = require("./../_shared/link-modal/open-link-modal.js");
var import_toggle_link_modal = require("./../_shared/link-modal/toggle-link-modal.js");
var import_is_always_visible = require("./../_shared/resolvers/is-always-visible.js");
var import_is_link_or_anchor_in_text = require("./../_shared/resolvers/is-link-or-anchor-in-text.js");
var import_is_node_selected = require("./../_shared/resolvers/is-node-selected.js");
var import_toolbars_v3 = require("./../toolbars-v3/index.js");
var import_anchor_converter = require("./anchor-converter.js");
var import_autolink_extension = require("./autolink-extension.js");
var import_data_hooks = require("./data-hooks.js");
var import_defaults = require("./defaults.js");
var import_get_toolbar_buttons = require("./get-toolbar-buttons.js");
var import_link_converter = require("./link-converter.js");
var import_types = require("./types.js");
const onSave = (publicCommands) => (linkData) => {
  publicCommands.insertDecoration(linkData.anchor ? import_ricos_content.RICOS_ANCHOR_TYPE : import_ricos_content.RICOS_LINK_TYPE, linkData);
};
const onSaveMultiple = (publicCommands) => (anchors) => {
  publicCommands.insertMultipleAnchors(anchors);
};
const onUnlink = (publicCommands) => (linkData) => {
  publicCommands.deleteDecoration(linkData.anchor ? import_ricos_content.RICOS_ANCHOR_TYPE : import_ricos_content.RICOS_LINK_TYPE);
};
const pluginLink = (config) => {
  const pluginConfig = { type: "link", ...import_defaults.DEFAULTS.config, ...config };
  const { enabledLinkTypes } = pluginConfig;
  return {
    config: pluginConfig,
    type: import_types.LINK_TYPE,
    tiptapExtensions: [import_autolink_extension.autolinkExtension],
    pmNodeConverters: [],
    pmMarkConverters: [import_link_converter.linkConverter, import_anchor_converter.anchorConverter],
    shortcuts: [
      (0, import_ricos_types.createShortcut)({
        name: import_ricos_types.Decoration_Type.LINK,
        description: "ShortcutsSheet_Formatting_Link",
        group: "ShortcutsSheet_Section_Formatting",
        keys: {
          macOs: "Meta+K",
          windows: "Ctrl+K"
        },
        enabled: true,
        showInShortcutSheet: true,
        command: ({ publicCommands, modalService, editor }) => {
          (0, import_open_link_modal.openLinkModal)({
            editor,
            initialValue: publicCommands.getLinkDataInSelection(),
            modalService,
            onSave: onSave(publicCommands),
            onSaveMultiple: onSaveMultiple(publicCommands),
            onUnlink: onUnlink(publicCommands),
            placement: "bottom-start",
            enabledLinkTypes,
            returnFocusToEditor: true
          });
          return "continue";
        }
      })
    ],
    toolbar: {
      names: [import_ricos_types.RicosPluginToolbarNames.LINK],
      getButtons: import_get_toolbar_buttons.getToolbarButtons,
      isVisible: (selection, editor) => {
        const isSelectionCollapsed = (selection2) => {
          const { from, to } = selection2;
          return to - from <= 0;
        };
        const isCollapsed = isSelectionCollapsed(selection);
        const isLinkInSelection = editor.getPublicCommands().hasLinkInSelection();
        return isCollapsed && isLinkInSelection;
      }
    },
    textButtons: [
      {
        id: import_ricos_types.Decoration_Type.LINK,
        type: "modal",
        presentation: {
          dataHook: import_data_hooks.TEXT_BUTTON_LINK,
          tooltip: "TextLinkButton_Tooltip",
          icon: import_wix_ui_icons_common.Link,
          renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_toolbars_v3.LinkButton, { toolbarItem })
        },
        attributes: {
          visible: import_is_always_visible.isAlwaysVisible,
          active: import_is_link_or_anchor_in_text.isLinkOrAnchorInText,
          disabled: import_is_node_selected.isNodeSelected
        },
        Component: import_toolbars_v3.LinkButton,
        command: ({ publicCommands, modalService, referenceElement }) => () => {
          (0, import_toggle_link_modal.toggleLinkModal)({
            initialValue: publicCommands.getLinkDataInSelection(),
            modalService,
            onSave: onSave(publicCommands),
            onSaveMultiple: onSaveMultiple(publicCommands),
            onUnlink: onUnlink(publicCommands),
            referenceElement,
            enabledLinkTypes,
            returnFocusToEditor: true
          });
          return true;
        }
      }
    ],
    getQuickActions: ({ pluginServices }) => [
      {
        id: "link",
        title: "QuickActionsPlugin_ActionTitle_Link",
        icon: pluginServices?.context.isMobile ? import_wix_ui_icons_common.Link : import_wix_ui_icons_common.LinkSmall,
        section: import_ricos_types.quickActionsModalSections.FormatLayout,
        tags: ["link", "anchor", "url", "hyperlink"],
        command: ({ publicCommands, modalService, editor }) => {
          (0, import_open_link_modal.openLinkModal)({
            editor,
            modalService,
            onSaveMultiple: onSaveMultiple(publicCommands),
            onSave: onSave(publicCommands),
            onUnlink: onUnlink(publicCommands),
            placement: "bottom-start",
            enabledLinkTypes,
            returnFocusToEditor: true
          });
        }
      }
    ]
  };
};
//# sourceMappingURL=editor.js.map
