"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactBinding = require("@contember/react-binding");
const ui = require("@contember/ui");
const react = require("react");
function InitializedFilePreview({
  fileKind,
  resolvedEntity,
  formatMessage,
  uploadState
}) {
  const [extractionState, setExtractionState] = react.useState({ name: "uninitialized" });
  const isMountedRef = react.useRef(true);
  react.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]);
  react.useEffect(() => {
    if (uploadState.readyState !== "success" || extractionState.name !== "success") {
      return;
    }
    if (!resolvedEntity.fileEntity) {
      throw new reactBinding.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]);
  react.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__ */ jsxRuntime.jsx(ui.ErrorList, { errors: endUserMessages });
      }
    }
    if (uploadState.readyState === "error" || extractionState.name === "error") {
      return /* @__PURE__ */ jsxRuntime.jsx(ui.Message, { intent: "danger", children: formatMessage("upload.fileState.failedUpload") });
    }
    if (uploadState.readyState === "success") {
      if (extractionState.name === "success") {
        return void 0;
      }
      return /* @__PURE__ */ jsxRuntime.jsx(ui.UploadProgress, { progress: formatMessage("upload.fileState.finalizing") });
    }
    if (uploadState.readyState === "uploading") {
      return /* @__PURE__ */ jsxRuntime.jsx(ui.UploadProgress, { progress: uploadState.progress });
    }
    return /* @__PURE__ */ jsxRuntime.jsx(ui.UploadProgress, {});
  };
  return /* @__PURE__ */ jsxRuntime.jsx(ui.FilePreview, { overlay: getOverlay(), children: fileKind.renderFilePreview({
    file: uploadState.file,
    objectUrl: uploadState.previewUrl,
    acceptArtifacts: uploadState.metadata
  }) });
}
exports.InitializedFilePreview = InitializedFilePreview;
//# sourceMappingURL=InitializedFilePreview.cjs.map
