import { jsx } from "react/jsx-runtime";
import { useMutationState, useGetEntityByKey } from "@contember/react-binding";
import { useFileUpload } from "@contember/react-client";
import { useCallback, useMemo } from "react";
import { useMessageFormatter } from "@contember/react-i18n";
import { useAccessorErrorFormatter } from "@contember/react-binding-ui";
import { uploadDictionary } from "../uploadDictionary.js";
import { FileInput } from "./FileInput.js";
import { useConnectSelectedEntities } from "./hooks/useConnectSelectedEntities.js";
import { useNormalizedUploadState } from "./hooks/useNormalizedUploadState.js";
import { SingleFilePreview } from "./SingleFilePreview/SingleFilePreview.js";
import { SortableRepeaterItem } from "../../collections/Repeater/SortableRepeaterItem.js";
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 = 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__ */ jsx(
      SingleFilePreview,
      {
        resolvedEntity: entity,
        fileId: fileKey,
        formatMessage,
        removeFile: normalizedRemoveFile,
        uploadState: entityUploadState
      }
    );
    if (sortableBy === void 0) {
      previews.push(
        /* @__PURE__ */ jsx("div", { className: "fileInput-preview", children: preview }, entity.parentEntity.key)
      );
    } else {
      previews.push(
        /* @__PURE__ */ jsx(SortableRepeaterItem, { index: i, disabled: isMutating, children: /* @__PURE__ */ jsx("div", { "data-sortable": true, className: "fileInput-preview", children: preview }) }, entity.parentEntity.key)
      );
    }
  }
  const onSelectConfirm = useConnectSelectedEntities(createNewEntity);
  return /* @__PURE__ */ jsx(
    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
