"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const reactI18n = require("@contember/react-i18n");
const uploadDictionary = require("../uploadDictionary.cjs");
const FileInput = require("./FileInput.cjs");
const useNormalizedUploadState = require("./hooks/useNormalizedUploadState.cjs");
const useConnectSelectedEntities = require("./hooks/useConnectSelectedEntities.cjs");
const reactClient = require("@contember/react-client");
const reactBindingUi = require("@contember/react-binding-ui");
const SingleFilePreview = require("./SingleFilePreview/SingleFilePreview.cjs");
const BareUploadField = reactBinding.Component(
  ({ fileHandler, ...fileInputProps }) => {
    const parentEntity = reactBinding.useEntity();
    const formatMessage = reactI18n.useMessageFormatter(uploadDictionary.uploadDictionary);
    {
      react.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 = reactClient.useFileUpload();
    const [, { purgeUpload }] = fileUpload;
    const resolvedEntity = react.useMemo(() => fileHandler.resolveEntity(parentEntity), [fileHandler, parentEntity]);
    const destroyFile = react.useMemo(() => {
      if (!resolvedEntity.isEmpty && !resolvedEntity.destroy) {
        return void 0;
      }
      return () => {
        purgeUpload([parentEntity.key]);
        resolvedEntity.destroy?.();
      };
    }, [parentEntity.key, purgeUpload, resolvedEntity]);
    const prepareEntityForNewFile = react.useCallback(
      (initialize) => {
        destroyFile?.();
        parentEntity.batchUpdates(initialize);
      },
      [parentEntity, destroyFile]
    );
    const { uploadState, dropzoneState } = useNormalizedUploadState.useNormalizedUploadState({
      isMultiple: false,
      fileHandler,
      prepareEntityForNewFile,
      fileUpload
    });
    const fileUploadState = uploadState.get(parentEntity.key);
    const errorHolders = react.useMemo(() => resolvedEntity.getErrorHolders(), [resolvedEntity]);
    const errorFormatter = reactBindingUi.useAccessorErrorFormatter();
    const errors = react.useMemo(() => {
      return errorFormatter(errorHolders.flatMap((it) => it.errors?.errors ?? []));
    }, [errorFormatter, errorHolders]);
    const children = !resolvedEntity.isEmpty || fileUploadState !== void 0 ? /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { className: "fileInput-preview", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      SingleFilePreview.SingleFilePreview,
      {
        resolvedEntity,
        fileId: parentEntity.key,
        formatMessage,
        removeFile: destroyFile,
        uploadState: uploadState.get(parentEntity.key)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareUploadField.tsx",
        lineNumber: 74,
        columnNumber: 6
      },
      void 0
    ) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareUploadField.tsx",
      lineNumber: 73,
      columnNumber: 5
    }, void 0) : void 0;
    const onSelectConfirm = useConnectSelectedEntities.useConnectSelectedEntities(prepareEntityForNewFile);
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      FileInput.FileInput,
      {
        ...fileInputProps,
        isMultiple: false,
        dropzoneState,
        formatMessage,
        errors,
        children,
        onSelectConfirm
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareUploadField.tsx",
        lineNumber: 88,
        columnNumber: 4
      },
      void 0
    );
  },
  (props, environment) => {
    return props.fileHandler.staticRender(environment);
  },
  "BareUploadField"
);
exports.BareUploadField = BareUploadField;
//# sourceMappingURL=BareUploadField.cjs.map
