import { jsx } from "react/jsx-runtime";
import { Component, useEntity } from "@contember/react-binding";
import { useMemo, useCallback } from "react";
import { useMessageFormatter } from "@contember/react-i18n";
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 { useAccessorErrorFormatter } from "@contember/react-binding-ui";
import { SingleFilePreview } from "./SingleFilePreview/SingleFilePreview.js";
const BareUploadField = Component(
  ({ fileHandler, ...fileInputProps }) => {
    const parentEntity = useEntity();
    const formatMessage = useMessageFormatter(uploadDictionary);
    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__ */ jsx("div", { className: "fileInput-preview", children: /* @__PURE__ */ jsx(
      SingleFilePreview,
      {
        resolvedEntity,
        fileId: parentEntity.key,
        formatMessage,
        removeFile: destroyFile,
        uploadState: uploadState.get(parentEntity.key)
      }
    ) }) : void 0;
    const onSelectConfirm = useConnectSelectedEntities(prepareEntityForNewFile);
    return /* @__PURE__ */ jsx(
      FileInput,
      {
        ...fileInputProps,
        isMultiple: false,
        dropzoneState,
        formatMessage,
        errors,
        children,
        onSelectConfirm
      }
    );
  },
  (props, environment) => {
    return props.fileHandler.staticRender(environment);
  },
  "BareUploadField"
);
export {
  BareUploadField
};
//# sourceMappingURL=BareUploadField.js.map
