import { useMemo } from "react";
import { fileKindTemplateAnalyzer } from "../../fileKinds/fileKindTemplateAnalyzer.js";
import { useEnvironment, VariableInputTransformer } from "@contember/react-binding";
import { useObjectMemo } from "@contember/react-utils";
import { getResolvedSingleKindFileSelection } from "./useResolvedSingleKindFileSelection.js";
import { createEntityConnectorFactory } from "../../fileHandler/utils/createEntityConnector.js";
import { DiscriminatedSelectFileDialog } from "./DiscriminatedSelectFileDialog.js";
import { DiscriminatedFileHandler } from "../../fileHandler/DiscriminatedFileHandler.js";
const getFileSelection = (props, environment) => {
  if (!("discriminationField" in props)) {
    return createSingleKindFileSelection(props, environment);
  }
  return createDiscriminatedFileKinds(props, environment);
};
const useFileSelection = (unstableProps) => {
  const environment = useEnvironment();
  const props = useObjectMemo(unstableProps);
  return useMemo(
    () => getFileSelection(props, environment),
    [environment, props]
  );
};
const createSingleKindFileSelection = ({ fileSelectionComponent, fileSelectionProps, baseEntity }, environment) => {
  return getResolvedSingleKindFileSelection({
    fileSelectionComponent,
    fileSelectionProps
  }, () => createEntityConnectorFactory(environment, baseEntity));
};
const createDiscriminatedFileKinds = (props, environment) => {
  const { children, baseEntity, fileSelectionComponent, fileSelectionProps, discriminationField } = props;
  if (fileSelectionComponent) {
    return getResolvedSingleKindFileSelection({
      fileSelectionComponent,
      fileSelectionProps
    }, () => createEntityConnectorFactory(environment, baseEntity));
  }
  const processed = 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 ?? VariableInputTransformer.transformValue(discriminateBy, environment);
    const connectorFactory = createEntityConnectorFactory(environment, baseEntity, kindBaseEntity);
    components.push({
      label: String(label),
      innerComponent: fileSelectionComponent2,
      innerProps: fileSelectionProps2,
      connectorFactory: (selected) => {
        const innerConnector = connectorFactory(selected);
        const connector = (parent) => {
          DiscriminatedFileHandler.fillDiscriminateBy(parent, baseEntity, discriminateBy, discriminationField);
          innerConnector(parent);
        };
        connector.entity = selected;
        return connector;
      }
    });
  }
  if (components.length === 0) {
    return void 0;
  }
  return {
    dialogComponent: DiscriminatedSelectFileDialog,
    dialogProps: {
      components
    }
  };
};
export {
  createDiscriminatedFileKinds,
  createSingleKindFileSelection,
  getFileSelection,
  useFileSelection
};
//# sourceMappingURL=useResolvedFileSelection.js.map
