"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("react");
const fileKindTemplateAnalyzer = require("../../fileKinds/fileKindTemplateAnalyzer.cjs");
const reactBinding = require("@contember/react-binding");
const reactUtils = require("@contember/react-utils");
const useResolvedSingleKindFileSelection = require("./useResolvedSingleKindFileSelection.cjs");
const createEntityConnector = require("../../fileHandler/utils/createEntityConnector.cjs");
const DiscriminatedSelectFileDialog = require("./DiscriminatedSelectFileDialog.cjs");
const DiscriminatedFileHandler = require("../../fileHandler/DiscriminatedFileHandler.cjs");
const getFileSelection = (props, environment) => {
  if (!("discriminationField" in props)) {
    return createSingleKindFileSelection(props, environment);
  }
  return createDiscriminatedFileKinds(props, environment);
};
const useFileSelection = (unstableProps) => {
  const environment = reactBinding.useEnvironment();
  const props = reactUtils.useObjectMemo(unstableProps);
  return react.useMemo(
    () => getFileSelection(props, environment),
    [environment, props]
  );
};
const createSingleKindFileSelection = ({ fileSelectionComponent, fileSelectionProps, baseEntity }, environment) => {
  return useResolvedSingleKindFileSelection.getResolvedSingleKindFileSelection({
    fileSelectionComponent,
    fileSelectionProps
  }, () => createEntityConnector.createEntityConnectorFactory(environment, baseEntity));
};
const createDiscriminatedFileKinds = (props, environment) => {
  const { children, baseEntity, fileSelectionComponent, fileSelectionProps, discriminationField } = props;
  if (fileSelectionComponent) {
    return useResolvedSingleKindFileSelection.getResolvedSingleKindFileSelection({
      fileSelectionComponent,
      fileSelectionProps
    }, () => createEntityConnector.createEntityConnectorFactory(environment, baseEntity));
  }
  const processed = fileKindTemplateAnalyzer.fileKindTemplateAnalyzer.processChildren(children, environment);
  const fileKinds = processed.map((node) => node.value);
  const components = [];
  for (const {
    fileSelection: { fileSelectionComponent: fileSelectionComponent2, fileSelectionLabel, fileSelectionProps: fileSelectionProps2 },
    baseEntity: kindBaseEntity,
    discriminateBy
  } of fileKinds) {
    if (!fileSelectionComponent2) {
      continue;
    }
    const label = fileSelectionLabel ?? reactBinding.VariableInputTransformer.transformValue(discriminateBy, environment);
    const connectorFactory = createEntityConnector.createEntityConnectorFactory(environment, baseEntity, kindBaseEntity);
    components.push({
      label: String(label),
      innerComponent: fileSelectionComponent2,
      innerProps: fileSelectionProps2,
      connectorFactory: (selected) => {
        const innerConnector = connectorFactory(selected);
        const connector = (parent) => {
          DiscriminatedFileHandler.DiscriminatedFileHandler.fillDiscriminateBy(parent, baseEntity, discriminateBy, discriminationField);
          innerConnector(parent);
        };
        connector.entity = selected;
        return connector;
      }
    });
  }
  if (components.length === 0) {
    return void 0;
  }
  return {
    dialogComponent: DiscriminatedSelectFileDialog.DiscriminatedSelectFileDialog,
    dialogProps: {
      components
    }
  };
};
exports.createDiscriminatedFileKinds = createDiscriminatedFileKinds;
exports.createSingleKindFileSelection = createSingleKindFileSelection;
exports.getFileSelection = getFileSelection;
exports.useFileSelection = useFileSelection;
//# sourceMappingURL=useResolvedFileSelection.cjs.map
