import { jsxDEV } from "react/jsx-dev-runtime";
import { BindingError } from "@contember/react-binding";
import { FilePreview, ErrorList, Message, UploadProgress } from "@contember/ui";
import { useState, useRef, useEffect } from "react";
function InitializedFilePreview({
  fileKind,
  resolvedEntity,
  formatMessage,
  uploadState
}) {
  const [extractionState, setExtractionState] = useState({ name: "uninitialized" });
  const isMountedRef = useRef(true);
  useEffect(() => {
    const extractData = async () => {
      if (extractionState.name !== "uninitialized" || uploadState.readyState === "error") {
        return;
      }
      const dataPromises = fileKind.extractors.map(
        (extractor) => extractor.extractFileData ? extractor.extractFileData({
          file: uploadState.file,
          acceptArtifacts: uploadState.metadata,
          objectUrl: uploadState.previewUrl
        }) : Promise.resolve()
      );
      setExtractionState({ name: "working" });
      try {
        const data = await Promise.all(dataPromises);
        if (!isMountedRef.current) {
          return;
        }
        setExtractionState({
          name: "success",
          data
        });
      } catch (e) {
        setExtractionState({
          name: "error"
        });
      }
    };
    extractData();
  }, [extractionState.name, fileKind.extractors, uploadState]);
  useEffect(() => {
    if (uploadState.readyState !== "success" || extractionState.name !== "success") {
      return;
    }
    if (!resolvedEntity.fileEntity) {
      throw new BindingError();
    }
    resolvedEntity.fileEntity.batchUpdates((getEntity) => {
      for (let i = 0; i < fileKind.extractors.length; i++) {
        const extractor = fileKind.extractors[i];
        const extractedData = extractionState.data[i];
        extractor.populateFields({
          file: uploadState.file,
          objectUrl: uploadState.previewUrl,
          extractedData,
          uploadResult: uploadState.result,
          acceptArtifacts: uploadState.metadata,
          entity: getEntity()
        });
      }
    });
  }, [extractionState, fileKind.baseEntity, fileKind.extractors, resolvedEntity.fileEntity, uploadState]);
  useEffect(
    () => () => {
      isMountedRef.current = false;
    },
    []
  );
  const getOverlay = () => {
    if (uploadState.readyState === "error") {
      const endUserMessages = uploadState.errors ? uploadState.errors.filter((error) => !!error.options.endUserMessage).map((error) => ({ message: formatMessage(error.options.endUserMessage, "upload.fileState.failedUpload") })) : [];
      if (endUserMessages.length) {
        return /* @__PURE__ */ jsxDEV(ErrorList, { errors: endUserMessages }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
          lineNumber: 109,
          columnNumber: 12
        }, this);
      }
    }
    if (uploadState.readyState === "error" || extractionState.name === "error") {
      return /* @__PURE__ */ jsxDEV(Message, { intent: "danger", children: formatMessage("upload.fileState.failedUpload") }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
        lineNumber: 113,
        columnNumber: 11
      }, this);
    }
    if (uploadState.readyState === "success") {
      if (extractionState.name === "success") {
        return void 0;
      }
      return /* @__PURE__ */ jsxDEV(UploadProgress, { progress: formatMessage("upload.fileState.finalizing") }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
        lineNumber: 119,
        columnNumber: 11
      }, this);
    }
    if (uploadState.readyState === "uploading") {
      return /* @__PURE__ */ jsxDEV(UploadProgress, { progress: uploadState.progress }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
        lineNumber: 122,
        columnNumber: 11
      }, this);
    }
    return /* @__PURE__ */ jsxDEV(UploadProgress, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
      lineNumber: 124,
      columnNumber: 10
    }, this);
  };
  return /* @__PURE__ */ jsxDEV(FilePreview, { overlay: getOverlay(), children: fileKind.renderFilePreview({
    file: uploadState.file,
    objectUrl: uploadState.previewUrl,
    acceptArtifacts: uploadState.metadata
  }) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/InitializedFilePreview.tsx",
    lineNumber: 127,
    columnNumber: 3
  }, this);
}
export {
  InitializedFilePreview
};
//# sourceMappingURL=InitializedFilePreview.js.map
