"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, {
  pluginHtml: () => pluginHtml
});
module.exports = __toCommonJS(editor_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_html_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/html.defaults.json.js"));
var import_next_schema = require("./../ricos-schema/next-schema/index.js");
var import_ricos_types = require("./../ricos-types/index.js");
var import_tiptap = require("./../tiptap/index.js");
var import_wix_rich_content_common = require("./../wix-rich-content-common/index.js");
var import_data_hooks = require("./data-hooks.js");
var import_defaults = require("./defaults.js");
var import_get_add_buttons = require("./get-add-buttons.js");
var import_get_toolbar_buttons = require("./get-toolbar-buttons.js");
var import_html_converter = require("./html-converter.js");
var import_insert_ai_widget_block = require("./insert-ai-widget-block.js");
var import_tiptap2 = require("./tiptap/tiptap.js");
var import_types = require("./types.js");
const pluginHtml = (config) => {
  const pluginConfig = {
    type: "html",
    ...import_defaults.DEFAULTS_CONFIG,
    ...config
  };
  return {
    config: pluginConfig,
    type: import_types.HTML_TYPE,
    pmMarkConverters: [],
    pmNodeConverters: [import_html_converter.htmlConverter],
    tiptapExtensions: import_tiptap2.tiptapExtensions,
    getAddButtons: (config2, services) => (0, import_get_add_buttons.getAddButtons)(config2, services),
    toolbar: {
      names: [import_ricos_types.RicosPluginToolbarNames.HTML],
      getButtons: (config2, services) => (0, import_get_toolbar_buttons.getToolbarButtons)(config2, services),
      resolveReferenceElement: (tiptapEditor) => {
        const selectedDOM = tiptapEditor.view.nodeDOM(tiptapEditor.state.selection.from);
        const firstNodeChild = getFirstNodeChildElement(selectedDOM);
        const aiWidgetHtmlViewer = firstNodeChild?.querySelector?.((0, import_wix_rich_content_common.dataHook)(import_data_hooks.AI_WIDGET_HTML_VIEWER));
        if (aiWidgetHtmlViewer) {
          return {
            getBoundingClientRect: () => {
              const currentHtmlViewer = firstNodeChild?.querySelector(
                (0, import_wix_rich_content_common.dataHook)(import_data_hooks.AI_WIDGET_HTML_VIEWER)
              );
              const htmlViewerRect = currentHtmlViewer?.getBoundingClientRect();
              if (!htmlViewerRect) {
                return new DOMRect(0, 0, 0, 0);
              }
              const floatingContainer = document.querySelector(
                (0, import_wix_rich_content_common.dataHook)(import_data_hooks.FLOATING_CONTAINER_AI_WIDGET)
              );
              const floatingRect = floatingContainer?.getBoundingClientRect();
              const isFloatingVisible = floatingContainer && floatingRect && floatingRect.width > 0 && floatingRect.height > 0;
              if (!isFloatingVisible || !floatingRect) {
                return htmlViewerRect;
              }
              const top = Math.min(htmlViewerRect.top, floatingRect.top);
              const left = Math.min(htmlViewerRect.left, floatingRect.left);
              const right = Math.max(htmlViewerRect.right, floatingRect.right);
              const bottom = Math.max(htmlViewerRect.bottom, floatingRect.bottom);
              return new DOMRect(left, top, right - left, bottom - top);
            },
            // Provide context element to help with positioning calculations
            contextElement: aiWidgetHtmlViewer
          };
        }
        if (firstNodeChild) {
          return firstNodeChild;
        }
        return {
          getBoundingClientRect: () => {
            const { state, view } = tiptapEditor;
            const { from, to } = state.selection;
            return (0, import_tiptap.posToDOMRect)(view, from, to);
          }
        };
      }
    },
    getQuickActions: ({ pluginServices }) => {
      const isMobile = pluginServices?.context.isMobile;
      const aiWidget = {
        id: "aiWidget",
        title: "QuickActionsPlugin_ActionTitle_AiWidget",
        icon: isMobile ? import_wix_ui_icons_common.Code : import_wix_ui_icons_common.CodeSmall,
        section: import_ricos_types.quickActionsModalSections.Embed,
        tags: [
          "code",
          "html",
          "embed",
          "iframe",
          "script",
          "ai widget",
          "gpt",
          "chatgpt",
          "create"
        ],
        command: ({ modalService, publicCommands, editor }) => {
          void (0, import_insert_ai_widget_block.insertAiWidgetBlockAndOpenPrompt)({
            publicCommands,
            editor,
            modals: modalService,
            useWixelWidgets: pluginConfig?.useWixelWidgets,
            getColorPalette: pluginConfig?.getColorPalette,
            canProceed: pluginConfig?.htmlAiGeneration?.canProceed
          });
          return true;
        }
      };
      return [
        {
          id: "adSense",
          title: "QuickActionsPlugin_ActionTitle_AdSense",
          icon: isMobile ? import_wix_ui_icons_common.GoogleAds : import_wix_ui_icons_common.GoogleAdsSmall,
          section: import_ricos_types.quickActionsModalSections.Embed,
          tags: [
            "advertisement",
            "google ads",
            "google",
            "monetization",
            "ad widget",
            "banner ad",
            "adsense"
          ],
          command: ({ publicCommands }) => {
            publicCommands.insertBlockWithBlankLines(import_types.HTML_TYPE, {
              ...import_html_defaults_json.default,
              source: import_next_schema.HTMLData_Source.ADSENSE,
              state: {
                status: "SUCCESS"
              }
            });
          }
        },
        {
          id: "htmlEmbed",
          title: "QuickActionsPlugin_ActionTitle_HtmlEmbed",
          icon: isMobile ? import_wix_ui_icons_common.Code : import_wix_ui_icons_common.CodeSmall,
          section: import_ricos_types.quickActionsModalSections.Embed,
          tags: ["code", "html", "embed", "iframe", "script"],
          command: ({ publicCommands }) => {
            publicCommands.insertBlockWithBlankLines(import_types.HTML_TYPE, {
              ...import_html_defaults_json.default,
              state: {
                status: "SUCCESS"
              }
            });
          }
        },
        ...config?.enableAiMode ? [aiWidget] : []
      ];
    },
    exports: config?.enableAiMode ? {
      modals: {
        AI_WIDGET: (editor, services) => {
          void (0, import_insert_ai_widget_block.insertAiWidgetBlockAndOpenPrompt)({
            publicCommands: services.publicCommands,
            editor,
            modals: services.modals,
            useWixelWidgets: pluginConfig?.useWixelWidgets,
            getColorPalette: pluginConfig?.getColorPalette,
            canProceed: pluginConfig?.htmlAiGeneration?.canProceed
          });
        }
      }
    } : void 0
  };
};
const getFirstNodeChildElement = (selectedDOM) => {
  if (!selectedDOM || !(selectedDOM instanceof HTMLElement)) {
    return null;
  }
  const ricosNodeFirstChild = selectedDOM?.querySelector?.("[data-hook~=ricos-node]")?.firstChild ?? null;
  if (!ricosNodeFirstChild || !(ricosNodeFirstChild instanceof HTMLElement)) {
    return null;
  }
  return ricosNodeFirstChild;
};
//# sourceMappingURL=editor.js.map
