import { createElement, memo, forwardRef } from "react";
import { Component, useMutationState, BindingError } from "@contember/binding";
import { FieldContainer, SelectCreateNewWrapper, Select, flipValue } from "@contember/ui";
import { useLabelMiddleware } from "../../environment/LabelMiddleware.js";
import { ChoiceField } from "../ChoiceField/ChoiceField.js";
const NativeSelectField = Component((props) => /* @__PURE__ */ createElement(ChoiceField, {
  ...props
}, (choiceProps) => /* @__PURE__ */ createElement(NativeSelectFieldInner, {
  ...props,
  ...choiceProps
})), "SelectField");
const NativeSelectFieldInner = memo(forwardRef((props, ref) => {
  const isMutating = useMutationState();
  const labelMiddleware = useLabelMiddleware();
  const options = props.data.map((it) => {
    if (typeof it.label !== "string") {
      throw new BindingError(`The labels of <SelectField /> items must be strings!`);
    }
    return {
      disabled: false,
      value: it,
      label: it.label
    };
  });
  return /* @__PURE__ */ createElement(FieldContainer, {
    ...props,
    label: labelMiddleware(props.label)
  }, /* @__PURE__ */ createElement(SelectCreateNewWrapper, {
    onClick: props.onAddNew
  }, /* @__PURE__ */ createElement(Select, {
    ref,
    required: props.required,
    notNull: props.notNull ?? flipValue(props.allowNull),
    value: props.currentValue,
    placeholder: props.placeholder,
    onChange: (value) => {
      if (value) {
        props.onSelect(value);
      } else {
        props.onClear();
      }
    },
    options,
    loading: isMutating
  })));
}));
export { NativeSelectField, NativeSelectFieldInner };
//# sourceMappingURL=NativeSelectField.js.map
