import { Code, CodeSmall, GoogleAds, GoogleAdsSmall } from "@wix/wix-ui-icons-common";
import htmlDataDefaults from "./../ricos-schema/legacy/statics/html.defaults.json.js";
import { HTMLData_Source } from "./../ricos-schema/next-schema/index.js";
import { RicosPluginToolbarNames, quickActionsModalSections } from "./../ricos-types/index.js";
import { posToDOMRect } from "./../tiptap/index.js";
import { dataHook } from "./../wix-rich-content-common/index.js";
import { AI_WIDGET_HTML_VIEWER, FLOATING_CONTAINER_AI_WIDGET } from "./data-hooks.js";
import { DEFAULTS_CONFIG } from "./defaults.js";
import { getAddButtons } from "./get-add-buttons.js";
import { getToolbarButtons } from "./get-toolbar-buttons.js";
import { htmlConverter } from "./html-converter.js";
import { insertAiWidgetBlockAndOpenPrompt } from "./insert-ai-widget-block.js";
import { tiptapExtensions } from "./tiptap/tiptap.js";
import { HTML_TYPE } from "./types.js";
const pluginHtml = (config) => {
  const pluginConfig = {
    type: "html",
    ...DEFAULTS_CONFIG,
    ...config
  };
  return {
    config: pluginConfig,
    type: HTML_TYPE,
    pmMarkConverters: [],
    pmNodeConverters: [htmlConverter],
    tiptapExtensions,
    getAddButtons: (config2, services) => getAddButtons(config2, services),
    toolbar: {
      names: [RicosPluginToolbarNames.HTML],
      getButtons: (config2, services) => getToolbarButtons(config2, services),
      resolveReferenceElement: (tiptapEditor) => {
        const selectedDOM = tiptapEditor.view.nodeDOM(tiptapEditor.state.selection.from);
        const firstNodeChild = getFirstNodeChildElement(selectedDOM);
        const aiWidgetHtmlViewer = firstNodeChild?.querySelector?.(dataHook(AI_WIDGET_HTML_VIEWER));
        if (aiWidgetHtmlViewer) {
          return {
            getBoundingClientRect: () => {
              const currentHtmlViewer = firstNodeChild?.querySelector(
                dataHook(AI_WIDGET_HTML_VIEWER)
              );
              const htmlViewerRect = currentHtmlViewer?.getBoundingClientRect();
              if (!htmlViewerRect) {
                return new DOMRect(0, 0, 0, 0);
              }
              const floatingContainer = document.querySelector(
                dataHook(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 posToDOMRect(view, from, to);
          }
        };
      }
    },
    getQuickActions: ({ pluginServices }) => {
      const isMobile = pluginServices?.context.isMobile;
      const aiWidget = {
        id: "aiWidget",
        title: "QuickActionsPlugin_ActionTitle_AiWidget",
        icon: isMobile ? Code : CodeSmall,
        section: quickActionsModalSections.Embed,
        tags: [
          "code",
          "html",
          "embed",
          "iframe",
          "script",
          "ai widget",
          "gpt",
          "chatgpt",
          "create"
        ],
        command: ({ modalService, publicCommands, editor }) => {
          void 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 ? GoogleAds : GoogleAdsSmall,
          section: quickActionsModalSections.Embed,
          tags: [
            "advertisement",
            "google ads",
            "google",
            "monetization",
            "ad widget",
            "banner ad",
            "adsense"
          ],
          command: ({ publicCommands }) => {
            publicCommands.insertBlockWithBlankLines(HTML_TYPE, {
              ...htmlDataDefaults,
              source: HTMLData_Source.ADSENSE,
              state: {
                status: "SUCCESS"
              }
            });
          }
        },
        {
          id: "htmlEmbed",
          title: "QuickActionsPlugin_ActionTitle_HtmlEmbed",
          icon: isMobile ? Code : CodeSmall,
          section: quickActionsModalSections.Embed,
          tags: ["code", "html", "embed", "iframe", "script"],
          command: ({ publicCommands }) => {
            publicCommands.insertBlockWithBlankLines(HTML_TYPE, {
              ...htmlDataDefaults,
              state: {
                status: "SUCCESS"
              }
            });
          }
        },
        ...config?.enableAiMode ? [aiWidget] : []
      ];
    },
    exports: config?.enableAiMode ? {
      modals: {
        AI_WIDGET: (editor, services) => {
          void 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;
};
export {
  pluginHtml
};
//# sourceMappingURL=editor.js.map

import { injectCss } from "./viewer-styles-inject.js";
injectCss();
