"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 use_handle_file_upload_exports = {};
__export(use_handle_file_upload_exports, {
  useHandleFileUpload: () => useHandleFileUpload
});
module.exports = __toCommonJS(use_handle_file_upload_exports);
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_handle_file_upload = require("./../../_shared/handle-file-upload.js");
var import_wix_instance_context = require("./../../_shared/request-service-provider/wix-instance-context.js");
var import_some_editor_props_context = require("./../../_shared/some-editor-props-context.js");
var import_types = require("../types.js");
const useHandleFileUpload = () => {
  const { getPublicCommands } = import_react.default.useContext(import_ricos_context.EditorContext);
  const { uploadService } = (0, import_react.useContext)(import_ricos_context.UploadContext);
  const devService = (0, import_ricos_context.useDevService)();
  const wixInstance = (0, import_wix_instance_context.useWixInstanceService)();
  const editorProps = (0, import_some_editor_props_context.useEditorProps)();
  const modals = import_react.default.useContext(import_ricos_context.ModalServiceContext);
  return async (nodeId) => await (0, import_handle_file_upload.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, import_types.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, import_types.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, import_types.HTML_TYPE, {
          ...componentData,
          state: createStateWithFileRefs(componentData.state, updatedFileRefs)
        });
      }
    },
    pluginId: import_types.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 }
  };
};
//# sourceMappingURL=use-handle-file-upload.js.map
