import { Component, HasOne, HasMany, SugaredField } from "@contember/binding";
import { useDynamicMultipleChoiceField } from "./hooks/useDynamicMultipleChoiceField.js";
import { renderDynamicChoiceFieldStatic } from "./renderDynamicChoiceFieldStatic.js";
import { useDynamicMultipleChoiceWithConnectingEntityField } from "./hooks/useDynamicMultipleChoiceWithConnectingEntityField.js";
import { jsx, Fragment, jsxs } from "react/jsx-runtime";
const DynamicMultiChoiceField = Component((props) => {
  const choiceFieldMetadata = "connectingEntityField" in props && props.connectingEntityField ? useDynamicMultipleChoiceWithConnectingEntityField(props) : useDynamicMultipleChoiceField(props);
  return props.children(choiceFieldMetadata);
}, (props, environment) => {
  let {
    subTree,
    renderedOption
  } = renderDynamicChoiceFieldStatic(props, environment);
  let expectedMutation = "connectOrDisconnect";
  if ("connectingEntityField" in props && props.connectingEntityField) {
    expectedMutation = "anyMutation";
    const hasOneProps = typeof props.connectingEntityField === "string" ? {
      field: props.connectingEntityField
    } : props.connectingEntityField;
    renderedOption = /* @__PURE__ */ jsx(Fragment, {
      children: /* @__PURE__ */ jsx(HasOne, {
        ...hasOneProps,
        expectedMutation: "connectOrDisconnect",
        children: renderedOption
      })
    });
  }
  return /* @__PURE__ */ jsxs(Fragment, {
    children: [subTree, /* @__PURE__ */ jsxs(HasMany, {
      field: props.field,
      expectedMutation,
      initialEntityCount: 0,
      children: [props.sortableBy && /* @__PURE__ */ jsx(SugaredField, {
        field: props.sortableBy
      }), renderedOption]
    })]
  });
}, "DynamicMultiChoiceField");
export {
  DynamicMultiChoiceField
};
//# sourceMappingURL=DynamicMultiChoiceField.js.map
