"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactBinding = require("@contember/react-binding");
const react = require("react");
const reactUtils = require("@contember/react-utils");
const SingleKindFileHandler = require("./SingleKindFileHandler.cjs");
const DiscriminatedFileHandler = require("./DiscriminatedFileHandler.cjs");
const fileKindTemplateAnalyzer = require("../fileKinds/fileKindTemplateAnalyzer.cjs");
const getFileHandler = (props, environment, componentName) => {
  if (!("discriminationField" in props)) {
    return createSingleKindFileHandler(props);
  }
  return createDiscriminatedFileKinds(props, environment, componentName);
};
const useFileHandler = (unstableProps, componentName) => {
  const environment = reactBinding.useEnvironment();
  const props = reactUtils.useObjectMemo(unstableProps);
  return react.useMemo(
    () => getFileHandler(props, environment, componentName),
    [componentName, environment, props]
  );
};
const createSingleKindFileHandler = (props) => {
  const { hasUploadedFile, children, acceptFile, acceptMimeTypes, baseEntity, renderFilePreview, renderUploadedFile, extractors, uploader } = props;
  return new SingleKindFileHandler.SingleKindFileHandler(
    {
      acceptFile,
      acceptMimeTypes,
      baseEntity,
      children,
      extractors,
      renderFilePreview,
      renderUploadedFile,
      uploader
    },
    hasUploadedFile
  );
};
const createDiscriminatedFileKinds = (props, environment, componentName) => {
  const { discriminationField, children, baseEntity } = props;
  const processed = fileKindTemplateAnalyzer.fileKindTemplateAnalyzer.processChildren(children, environment);
  const fileKinds = processed.map((node) => node.value);
  const normalizedFileKinds = /* @__PURE__ */ new Map();
  for (const { acceptFile, acceptMimeTypes, baseEntity: baseEntity2, children: children2, extractors, renderFilePreview, renderUploadedFile, uploader, discriminateBy, childrenOutsideBaseEntity } of fileKinds) {
    const value = reactBinding.VariableInputTransformer.transformValue(discriminateBy, environment);
    normalizedFileKinds.set(value, {
      discriminateBy: value,
      datum: {
        acceptFile,
        acceptMimeTypes,
        baseEntity: baseEntity2,
        children: children2,
        extractors,
        renderFilePreview,
        renderUploadedFile,
        uploader,
        childrenOutsideBaseEntity
      }
    });
  }
  if (!normalizedFileKinds.size) {
    throw new reactBinding.BindingError(
      `${componentName}: having supplied the 'discriminationField' prop, you must specify at least one file kind!`
    );
  }
  return new DiscriminatedFileHandler.DiscriminatedFileHandler(
    discriminationField,
    baseEntity,
    normalizedFileKinds
  );
};
exports.createDiscriminatedFileKinds = createDiscriminatedFileKinds;
exports.createSingleKindFileHandler = createSingleKindFileHandler;
exports.getFileHandler = getFileHandler;
exports.useFileHandler = useFileHandler;
//# sourceMappingURL=useFileHandler.cjs.map
