import React, { useContext } from "react";
import {
  EditorContext,
  ModalServiceContext,
  UploadContext,
  useDevService
} from "./../../ricos-context/index.js";
import { useWixInstanceService } from "./../../_shared/request-service-provider/wix-instance-context.js";
import { useEditorProps } from "./../../_shared/some-editor-props-context.js";
import { IMAGE_EDITOR_MODAL_ID } from "../consts.js";
import { ImagePluginContext } from "../context.js";
import { handleImageUpload } from "../handle-image-upload.js";
import { setupImageEditor } from "../imageStudio/image-editor-logic.js";
const ROOT_ELEMENT_ID = "imageEditor";
const ImageEditorModal = ({ nodeId, config, src }) => {
  const modalService = useContext(ModalServiceContext) || {};
  const { uploadService } = useContext(UploadContext) || {};
  const { getPublicCommands } = useContext(EditorContext);
  const imagePluginContext = useContext(ImagePluginContext);
  const devService = useDevService();
  const editorProps = useEditorProps();
  const wixInstance = useWixInstanceService();
  React.useEffect(() => {
    const onClose = () => {
      modalService.closeModal(IMAGE_EDITOR_MODAL_ID);
    };
    const onSave = (file) => {
      handleImageUpload({
        services: {
          uploadService,
          devService,
          editorProps,
          wixInstance,
          modals: modalService
        },
        config,
        publicCommands: getPublicCommands(),
        nodeId,
        files: [file]
      });
      onClose();
    };
    const initializeImageEditor = async () => {
      const imageEditorSettings = await imagePluginContext?.getImageEditorSettings();
      setupImageEditor(
        imageEditorSettings ?? {},
        ROOT_ELEMENT_ID,
        src,
        onSave,
        onClose
      );
    };
    initializeImageEditor();
  }, [imagePluginContext]);
  return /* @__PURE__ */ React.createElement("div", { id: ROOT_ELEMENT_ID, nofocus: "true" });
};
export {
  ImageEditorModal
};
//# sourceMappingURL=image-editor-modal.js.map
