import { useCallback, useMemo, createElement } from "react";
import { useMutationState, useGetEntityByKey } from "@contember/binding";
import { useFileUpload } from "@contember/react-client";
import { uploadDictionary } from "../uploadDictionary.js";
import { FileInput } from "./FileInput.js";
import { useNormalizedUploadState } from "./hooks/useNormalizedUploadState.js";
import { useConnectSelectedEntities } from "./hooks/useConnectSelectedEntities.js";
import { SingleFilePreview } from "./SingleFilePreview/SingleFilePreview.js";
import { useMessageFormatter } from "../../../../i18n/useMessageFormatter.js";
import { useAccessorErrorFormatter } from "../../errors/useAccessorErrors.js";
import { SortableRepeaterItem } from "../../collections/Repeater/SortableRepeaterItem.js";
const BareFileRepeaterContainer = ({
  accessor,
  entities,
  isEmpty,
  label,
  fileHandler,
  createNewEntity,
  sortableBy,
  children,
  formatMessage: _,
  ...fileInputProps
}) => {
  const isMutating = useMutationState();
  const getEntityByKey = useGetEntityByKey();
  const formatMessage = useMessageFormatter(uploadDictionary);
  const fileUpload = useFileUpload();
  const [, { purgeUpload }] = fileUpload;
  const { uploadState, dropzoneState } = useNormalizedUploadState({
    isMultiple: true,
    fileHandler,
    prepareEntityForNewFile: createNewEntity,
    fileUpload
  });
  const normalizedRemoveFile = useCallback((entityKey) => {
    purgeUpload([entityKey]);
    getEntityByKey(entityKey.toString()).deleteEntity();
  }, [getEntityByKey, purgeUpload]);
  const resolvedEntities = useMemo(() => {
    return Object.values(entities).map((it) => fileHandler.resolveEntity(it));
  }, [entities, fileHandler]);
  const fileErrorsHolders = useMemo(() => {
    return [accessor, ...resolvedEntities.flatMap((it) => it.getErrorHolders())];
  }, [accessor, resolvedEntities]);
  const errorFormatter = useAccessorErrorFormatter();
  const errors = 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__ */ createElement(SingleFilePreview, {
      resolvedEntity: entity,
      fileId: fileKey,
      formatMessage,
      removeFile: normalizedRemoveFile,
      uploadState: entityUploadState
    });
    if (sortableBy === void 0) {
      previews.push(/* @__PURE__ */ createElement("div", {
        key: entity.parentEntity.key,
        className: "fileInput-preview"
      }, preview));
    } else {
      previews.push(/* @__PURE__ */ createElement(SortableRepeaterItem, {
        index: i,
        key: entity.parentEntity.key,
        disabled: isMutating
      }, /* @__PURE__ */ createElement("div", {
        className: "fileInput-preview view-sortable"
      }, preview)));
    }
  }
  const onSelectConfirm = useConnectSelectedEntities(createNewEntity);
  return /* @__PURE__ */ createElement(FileInput, {
    ...fileInputProps,
    isMultiple: true,
    label,
    dropzoneState,
    formatMessage,
    errors,
    children: isEmpty && !previews.length ? void 0 : previews,
    onSelectConfirm
  });
};
BareFileRepeaterContainer.displayName = "BareFileRepeaterContainer";
export { BareFileRepeaterContainer };
//# sourceMappingURL=BareFileRepeaterContainer.js.map
