import { useMutationState, useGetEntityByKey } from "@contember/binding";
import { useFileUpload } from "@contember/react-client";
import { useCallback, useMemo } from "react";
import { uploadDictionary } from "../uploadDictionary.js";
import { FileInput } from "./FileInput.js";
import { useNormalizedUploadState } from "./hooks/useNormalizedUploadState.js";
import { useConnectSelectedEntities } from "./hooks/useConnectSelectedEntities.js";
import { jsxDEV } from "react/jsx-dev-runtime";
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";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/BareFileRepeaterContainer.tsx";
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__ */ jsxDEV(SingleFilePreview, {
      resolvedEntity: entity,
      fileId: fileKey,
      formatMessage,
      removeFile: normalizedRemoveFile,
      uploadState: entityUploadState
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 87,
      columnNumber: 4
    }, void 0);
    if (sortableBy === void 0) {
      previews.push(/* @__PURE__ */ jsxDEV("div", {
        className: "fileInput-preview",
        children: preview
      }, entity.parentEntity.key, false, {
        fileName: _jsxFileName,
        lineNumber: 98,
        columnNumber: 5
      }, void 0));
    } else {
      previews.push(/* @__PURE__ */ jsxDEV(SortableRepeaterItem, {
        index: i,
        disabled: isMutating,
        children: /* @__PURE__ */ jsxDEV("div", {
          className: "fileInput-preview view-sortable",
          children: preview
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 105,
          columnNumber: 6
        }, void 0)
      }, entity.parentEntity.key, false, {
        fileName: _jsxFileName,
        lineNumber: 104,
        columnNumber: 5
      }, void 0));
    }
  }
  const onSelectConfirm = useConnectSelectedEntities(createNewEntity);
  return /* @__PURE__ */ jsxDEV(FileInput, {
    ...fileInputProps,
    isMultiple: true,
    label,
    dropzoneState,
    formatMessage,
    errors,
    children: isEmpty && !previews.length ? void 0 : previews,
    onSelectConfirm
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 114,
    columnNumber: 3
  }, void 0);
};
BareFileRepeaterContainer.displayName = "BareFileRepeaterContainer";
export {
  BareFileRepeaterContainer
};
//# sourceMappingURL=BareFileRepeaterContainer.js.map
