import { useField, useEnvironment, VariableInputTransformer } from "@contember/binding";
import { useState, useMemo, useCallback } from "react";
import { useFuseFilteredOptions } from "./useFuseFilteredOptions.js";
import { useAccessorErrors } from "../../../errors/useAccessorErrors.js";
const useStaticSingleChoiceField = (props) => {
  const [input, setSearchInput] = useState("");
  const field = useField(props);
  const data = useNormalizedOptions(props.options);
  const filteredData = useFuseFilteredOptions(props, data, input);
  const currentValue = useMemo(() => data.find((it) => field.hasValue(it.value)) ?? null, [data, field]);
  const errors = useAccessorErrors(field);
  const onSelect = useCallback((value) => {
    field.updateValue(value.value);
  }, [field]);
  const onClear = useCallback(() => {
    field.updateValue(null);
  }, [field]);
  return {
    currentValue,
    data: filteredData,
    onSelect,
    onClear,
    errors,
    onSearch: setSearchInput
  };
};
const useNormalizedOptions = (options) => {
  const environment = useEnvironment();
  return useMemo(() => options.map(({ label, description, value, searchKeywords }, i) => {
    const transformValue = VariableInputTransformer.transformValue(value, environment);
    return {
      key: i.toString(),
      searchKeywords: searchKeywords ?? `${label} ${transformValue}`,
      label,
      description,
      value: transformValue
    };
  }), [environment, options]);
};
export { useStaticSingleChoiceField };
//# sourceMappingURL=useStaticSingleChoiceField.js.map
