import { WixDesignSystemProvider } from "@wix/design-system";
import { StatusCompleteSmall } from "@wix/wix-ui-icons-common";
import React, { useContext } from "react";
import { ModalServiceContext, RicosContext } from "./../../../ricos-context/index.js";
import { AiPlusButton } from "./../../ai-plus-button/ai-plus-button.js";
import { AiContentModal } from "../../ai-content-modal/ai-content-modal.js";
import { AI_EXPORTS_CAPTION_TRIGGER_BUTTON } from "../../data-hooks.js";
import { AI_MODAL_ID } from "../../types.js";
import { getImageUrl } from "../../utils/get-image-url.js";
const CaptionTrigger = ({ node, onCaptionChange, appDefId, openDiscardModal }) => {
  const modalsService = useContext(ModalServiceContext);
  const { localeContent, isMobile } = useContext(RicosContext);
  const [buttonRef, setButtonRef] = React.useState(null);
  const openAiModal = () => {
    const initialPrompt = {
      type: "prompt",
      title: "AIPlugin_Prompt_GenerateCaption",
      promptSlug: "generate-caption",
      promptParams: {
        imageUrl: getImageUrl(node.imageData),
        siteLanguage: localeContent
      },
      additionalTags: "image caption generation"
    };
    modalsService.togglePluginPanel({
      id: AI_MODAL_ID,
      Component: AiContentModal,
      componentProps: {
        closeModal: () => modalsService.closeModal(AI_MODAL_ID),
        openDiscardModal: () => openDiscardModal(modalsService, buttonRef),
        config: {
          type: "ai",
          configurePrompts: () => ({ selectionPrompts: [], floatingToolbarButtonPrompts: [] }),
          appDefId
        },
        feedUserEntryFormatter: (prompt, t) => {
          if (typeof prompt === "object" && "promptSlug" in prompt && prompt.promptSlug === initialPrompt.promptSlug) {
            return prompt.promptParams?.context ?? t(prompt.title);
          }
          return typeof prompt === "string" ? prompt : t(prompt.title);
        },
        initialPromptOptions: {
          emptyStateTitle: "AIPlugin_ImagePlugin_NoSelection_Title",
          emptyStateText: "AIPlugin_ImagePlugin_NoSelection_Text",
          placeholder: "AIPlugin_ImagePlugin_Input_Placeholder_Text",
          contextualResultActions: [
            {
              name: "insert",
              type: "action",
              title: "AIPlugin_Action_Insert",
              icon: StatusCompleteSmall,
              onClick: async (entry) => {
                onCaptionChange(entry.response.plainContent);
                modalsService.closeModal(AI_MODAL_ID);
              }
            }
          ],
          prompt: initialPrompt,
          autoTrigger: false,
          promptPayloadFormatter: (prompt, textualPrompt) => {
            return {
              ...prompt,
              promptParams: {
                ...prompt.promptParams ?? {},
                context: textualPrompt
              }
            };
          },
          isSubmitButtonVisible: () => true
        }
      },
      positioning: {
        referenceElement: buttonRef,
        placement: "bottom-end"
      },
      onClickOutside: () => {
      },
      entryType: "image-caption",
      isMobile
    });
  };
  return /* @__PURE__ */ React.createElement(WixDesignSystemProvider, { features: { newColorsBranding: true } }, /* @__PURE__ */ React.createElement(
    AiPlusButton,
    {
      ref: setButtonRef,
      onClick: openAiModal,
      dataHook: AI_EXPORTS_CAPTION_TRIGGER_BUTTON
    }
  ));
};
const createCaptionTrigger = (appDefId, openDiscardModal) => {
  return (props) => /* @__PURE__ */ React.createElement(CaptionTrigger, { ...props, appDefId, openDiscardModal });
};
export {
  createCaptionTrigger
};
//# sourceMappingURL=caption-trigger.js.map
