import React, { useContext } from "react";
import {
  EditorContext,
  ModalServiceContext,
  UploadContext,
  useDevService
} from "./../../ricos-context/index.js";
import { handleFileUpload } from "./../../_shared/handle-file-upload.js";
import { useWixInstanceService } from "./../../_shared/request-service-provider/wix-instance-context.js";
import { useEditorProps } from "./../../_shared/some-editor-props-context.js";
import { HTML_TYPE } from "../types.js";
const useHandleFileUpload = () => {
  const { getPublicCommands } = React.useContext(EditorContext);
  const { uploadService } = useContext(UploadContext);
  const devService = useDevService();
  const wixInstance = useWixInstanceService();
  const editorProps = useEditorProps();
  const modals = React.useContext(ModalServiceContext);
  return async (nodeId) => await handleFileUpload({
    services: {
      uploadService,
      devService,
      wixInstance,
      editorProps,
      modals
    },
    callbacks: {
      onLoadingStart: (attachment) => {
        const publicCommands = getPublicCommands();
        const componentData = publicCommands?.getBlockComponentData(nodeId);
        const existingFileRefs = componentData.state?.prompt?.fileRefs || [];
        publicCommands?.setBlock(nodeId, HTML_TYPE, {
          ...componentData,
          state: createStateWithFileRefs(componentData.state, [...existingFileRefs, attachment])
        });
      },
      onSuccess: (attachmentId, attachment) => {
        const publicCommands = getPublicCommands();
        const componentData = publicCommands?.getBlockComponentData(nodeId);
        const existingFile = componentData?.state?.prompt?.fileRefs?.find(
          (ref) => ref.id === attachmentId
        );
        const updatedFileRefs = existingFile ? (componentData?.state?.prompt?.fileRefs || []).map(
          (ref) => ref.id === attachmentId ? attachment : ref
        ) : [...componentData?.state?.prompt?.fileRefs || [], attachment];
        publicCommands?.setBlock(nodeId, HTML_TYPE, {
          ...componentData,
          state: createStateWithFileRefs(componentData.state, updatedFileRefs)
        });
      },
      onError: (attachmentId, attachment) => {
        const publicCommands = getPublicCommands();
        const componentData = publicCommands?.getBlockComponentData(nodeId);
        const existingFile = componentData?.state?.prompt?.fileRefs?.find(
          (ref) => ref.id === attachmentId
        );
        const updatedFileRefs = existingFile ? (componentData?.state?.prompt?.fileRefs || []).map(
          (ref) => ref.id === attachmentId ? attachment : ref
        ) : [...componentData?.state?.prompt?.fileRefs || [], attachment];
        publicCommands?.setBlock(nodeId, HTML_TYPE, {
          ...componentData,
          state: createStateWithFileRefs(componentData.state, updatedFileRefs)
        });
      }
    },
    pluginId: HTML_TYPE
  });
};
const createStateWithFileRefs = (currentState, fileRefs) => {
  const status = currentState?.status || "INITIAL";
  const promptText = currentState?.prompt?.text || "";
  const selectedWidgetTypeId = currentState?.prompt?.selectedWidgetTypeId;
  if (status === "LOADING") {
    return {
      status: "LOADING",
      requestId: currentState?.requestId || "",
      prompt: { text: promptText, selectedWidgetTypeId, fileRefs }
    };
  }
  if (status === "ERROR") {
    return {
      status: "ERROR",
      error: currentState?.error || "",
      prompt: { text: promptText, selectedWidgetTypeId, fileRefs }
    };
  }
  return {
    status,
    prompt: { text: promptText, selectedWidgetTypeId, fileRefs }
  };
};
export {
  useHandleFileUpload
};
//# sourceMappingURL=use-handle-file-upload.js.map
