import { useEffect, useMemo, useCallback, createElement } from "react";
import { Component, useEntity } from "@contember/binding";
import { uploadDictionary } from "../uploadDictionary.js";
import { FileInput } from "./FileInput.js";
import { useNormalizedUploadState } from "./hooks/useNormalizedUploadState.js";
import { useConnectSelectedEntities } from "./hooks/useConnectSelectedEntities.js";
import { useFileUpload } from "@contember/react-client";
import { useMessageFormatter } from "../../../../i18n/useMessageFormatter.js";
import { useAccessorErrorFormatter } from "../../errors/useAccessorErrors.js";
import { SingleFilePreview } from "./SingleFilePreview/SingleFilePreview.js";
const BareUploadField = Component(({ fileHandler, ...fileInputProps }) => {
  const parentEntity = useEntity();
  const formatMessage = useMessageFormatter(uploadDictionary);
  {
    useEffect(() => {
      if (!fileHandler.hasBaseEntity) {
        console.warn('To support all features like file removal, "baseEntity" prop should be set on a upload field component.');
      }
    }, [fileHandler.hasBaseEntity]);
  }
  const fileUpload = useFileUpload();
  const [, { purgeUpload }] = fileUpload;
  const resolvedEntity = useMemo(() => fileHandler.resolveEntity(parentEntity), [fileHandler, parentEntity]);
  const destroyFile = useMemo(() => {
    if (!resolvedEntity.isEmpty && !resolvedEntity.destroy) {
      return void 0;
    }
    return () => {
      purgeUpload([parentEntity.key]);
      resolvedEntity.destroy?.();
    };
  }, [parentEntity.key, purgeUpload, resolvedEntity]);
  const prepareEntityForNewFile = useCallback((initialize) => {
    destroyFile?.();
    parentEntity.batchUpdates(initialize);
  }, [parentEntity, destroyFile]);
  const { uploadState, dropzoneState } = useNormalizedUploadState({
    isMultiple: false,
    fileHandler,
    prepareEntityForNewFile,
    fileUpload
  });
  const fileUploadState = uploadState.get(parentEntity.key);
  const errorHolders = useMemo(() => resolvedEntity.getErrorHolders(), [resolvedEntity]);
  const errorFormatter = useAccessorErrorFormatter();
  const errors = useMemo(() => {
    return errorFormatter(errorHolders.flatMap((it) => it.errors?.errors ?? []));
  }, [errorFormatter, errorHolders]);
  const children = !resolvedEntity.isEmpty || fileUploadState !== void 0 ? /* @__PURE__ */ createElement("div", {
    className: "fileInput-preview"
  }, /* @__PURE__ */ createElement(SingleFilePreview, {
    resolvedEntity,
    fileId: parentEntity.key,
    formatMessage,
    removeFile: destroyFile,
    uploadState: uploadState.get(parentEntity.key)
  })) : void 0;
  const onSelectConfirm = useConnectSelectedEntities(prepareEntityForNewFile);
  return /* @__PURE__ */ createElement(FileInput, {
    ...fileInputProps,
    isMultiple: false,
    dropzoneState,
    formatMessage,
    errors,
    children,
    onSelectConfirm
  });
}, (props, environment) => {
  return props.fileHandler.staticRender(environment);
}, "BareUploadField");
export { BareUploadField };
//# sourceMappingURL=BareUploadField.js.map
