"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const reactClient = require("@contember/react-client");
const react = require("react");
const reactI18n = require("@contember/react-i18n");
const reactBindingUi = require("@contember/react-binding-ui");
const uploadDictionary = require("../uploadDictionary.cjs");
const FileInput = require("./FileInput.cjs");
const useConnectSelectedEntities = require("./hooks/useConnectSelectedEntities.cjs");
const useNormalizedUploadState = require("./hooks/useNormalizedUploadState.cjs");
const SingleFilePreview = require("./SingleFilePreview/SingleFilePreview.cjs");
const SortableRepeaterItem = require("../../collections/Repeater/SortableRepeaterItem.cjs");
const BareFileRepeaterContainer = ({
  accessor,
  entities,
  isEmpty,
  label,
  fileHandler,
  createNewEntity,
  sortableBy,
  // These are here just to remove them from the spread below
  children,
  formatMessage: _,
  ...fileInputProps
}) => {
  const isMutating = reactBinding.useMutationState();
  const getEntityByKey = reactBinding.useGetEntityByKey();
  const formatMessage = reactI18n.useMessageFormatter(uploadDictionary.uploadDictionary);
  const fileUpload = reactClient.useFileUpload();
  const [, { purgeUpload }] = fileUpload;
  const { uploadState, dropzoneState } = useNormalizedUploadState.useNormalizedUploadState({
    isMultiple: true,
    fileHandler,
    prepareEntityForNewFile: createNewEntity,
    fileUpload
  });
  const normalizedRemoveFile = react.useCallback(
    (entityKey) => {
      purgeUpload([entityKey]);
      getEntityByKey(entityKey.toString()).deleteEntity();
    },
    [getEntityByKey, purgeUpload]
  );
  const resolvedEntities = react.useMemo(() => {
    return Object.values(entities).map((it) => fileHandler.resolveEntity(it));
  }, [entities, fileHandler]);
  const fileErrorsHolders = react.useMemo(() => {
    return [accessor, ...resolvedEntities.flatMap((it) => it.getErrorHolders())];
  }, [accessor, resolvedEntities]);
  const errorFormatter = reactBindingUi.useAccessorErrorFormatter();
  const errors = react.useMemo(() => {
    return errorFormatter(fileErrorsHolders.flatMap((it) => it.errors?.errors ?? []));
  }, [errorFormatter, fileErrorsHolders]);
  const previews = [];
  for (const [i, entity] of resolvedEntities.entries()) {
    const fileKey = entity.parentEntity.key;
    const entityUploadState = uploadState.get(fileKey);
    const preview = /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      SingleFilePreview.SingleFilePreview,
      {
        resolvedEntity: entity,
        fileId: fileKey,
        formatMessage,
        removeFile: normalizedRemoveFile,
        uploadState: entityUploadState
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx",
        lineNumber: 83,
        columnNumber: 4
      },
      void 0
    );
    if (sortableBy === void 0) {
      previews.push(
        /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { className: "fileInput-preview", children: preview }, entity.parentEntity.key, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx",
          lineNumber: 94,
          columnNumber: 5
        }, void 0)
      );
    } else {
      previews.push(
        /* @__PURE__ */ jsxDevRuntime.jsxDEV(SortableRepeaterItem.SortableRepeaterItem, { index: i, disabled: isMutating, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { "data-sortable": true, className: "fileInput-preview", children: preview }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx",
          lineNumber: 101,
          columnNumber: 6
        }, void 0) }, entity.parentEntity.key, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx",
          lineNumber: 100,
          columnNumber: 5
        }, void 0)
      );
    }
  }
  const onSelectConfirm = useConnectSelectedEntities.useConnectSelectedEntities(createNewEntity);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    FileInput.FileInput,
    {
      ...fileInputProps,
      isMultiple: true,
      label,
      dropzoneState,
      formatMessage,
      errors,
      children: isEmpty && !previews.length ? void 0 : previews,
      onSelectConfirm
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx",
      lineNumber: 110,
      columnNumber: 3
    },
    void 0
  );
};
BareFileRepeaterContainer.displayName = "BareFileRepeaterContainer";
exports.BareFileRepeaterContainer = BareFileRepeaterContainer;
//# sourceMappingURL=BareFileRepeaterContainer.cjs.map
